package javaparser; import java.util.Arrays; import java.util.List; import org.junit.Ignore; import org.junit.Test; import com.github.javaparser.JavaParser; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.StaticJavaParser; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.body.BodyDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.VariableDeclarationExpr; import com.github.javaparser.ast.stmt.ExpressionStmt; import com.github.javaparser.symbolsolver.JavaSymbolSolver; import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver; import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; public class ArraysResolveTest { public static void main(String[] args) { Integer v = 3; List x = Arrays.asList(1, 2, v); } String CODE = "import java.util.Arrays;\n" + "public class ArraysResolveTest {\n" + "\n" + " public static void main(String[] args) {\n" + " List x = Arrays.asList(1, 2);\n"// + " }\n" + "}"; @Ignore // nonworking version replicating issue in our code @Test public void testResolve() { ParserConfiguration conf = new ParserConfiguration(); CombinedTypeSolver typeSolver = new CombinedTypeSolver(); typeSolver.add(new ReflectionTypeSolver(false)); // FIXME add sources of jars source code etc // typeSolver.add(new JavaParserTypeSolver(new File(SOURCE_PATH))); // typeSolver.add(new JarTypeSolver(jarSourceFile)); JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver); conf.setSymbolResolver(symbolSolver); JavaParser parser = new JavaParser(conf); CompilationUnit res = parser.parse(CODE).getResult().get(); BodyDeclaration mainmethod = res.getType(0).getMembers().get(0); ExpressionStmt statement = (ExpressionStmt) ((MethodDeclaration) mainmethod) .getBody().get().getStatements().get(0); VariableDeclarationExpr exp = (VariableDeclarationExpr) statement .getExpression(); MethodCallExpr initvalue = (MethodCallExpr) exp.getVariable(0) .getInitializer().get(); NameExpr scope = (NameExpr) initvalue.getScope().get(); scope.resolve(); } @Test public void testResolveFixed() { ParserConfiguration conf = new ParserConfiguration(); CombinedTypeSolver typeSolver = new CombinedTypeSolver(); typeSolver.add(new ReflectionTypeSolver(false)); // FIXME add sources of jars source code etc // typeSolver.add(new JavaParserTypeSolver(new File(SOURCE_PATH))); // typeSolver.add(new JarTypeSolver(jarSourceFile)); JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver); conf.setSymbolResolver(symbolSolver); JavaParser parser = new JavaParser(conf); CompilationUnit res = parser.parse(CODE).getResult().get(); BodyDeclaration mainmethod = res.getType(0).getMembers().get(0); ExpressionStmt statement = (ExpressionStmt) ((MethodDeclaration) mainmethod) .getBody().get().getStatements().get(0); VariableDeclarationExpr exp = (VariableDeclarationExpr) statement .getExpression(); MethodCallExpr initvalue = (MethodCallExpr) exp.getVariable(0) .getInitializer().get(); NameExpr scope = (NameExpr) initvalue.getScope().get(); System.out.println(scope.calculateResolvedType()); } /** * Solution proposed by https://github.com/javaparser/javaparser/issues/3689 */ @Test public void test3689() { String CODE = "import java.util.Arrays;\n" + "public class ArraysResolveTest {\n" + "\n" + " public static void main(String[] args) {\n" + " List x = Arrays.asList(1, 2);\n"// + " }\n" + "}"; ParserConfiguration configuration = new ParserConfiguration() .setSymbolResolver(new JavaSymbolSolver( new CombinedTypeSolver(new ReflectionTypeSolver()))); StaticJavaParser.setConfiguration(configuration); CompilationUnit cu = StaticJavaParser.parse(CODE); List mces = cu.findAll(MethodCallExpr.class); for (MethodCallExpr mce : mces) { System.out.println(mce.toString()); System.out.println(">>" + mce.resolve().toString()); // find the/ method declaration System.out.println(">>" + mce.getScope().get().calculateResolvedType().describe()); // find the type of the scope of the method call expression } } @Test public void modifiedTest() { String CODE = "import java.util.Arrays;\n" + "public class ArraysResolveTest {\n" + " public static void main(String[] args) {\n" + " Integer v = 3;\n" + " List x = Arrays.asList(1, 2, v);\n"/// + " }\n}"; ParserConfiguration configuration = new ParserConfiguration() .setSymbolResolver(new JavaSymbolSolver( new CombinedTypeSolver(new ReflectionTypeSolver()))); StaticJavaParser.setConfiguration(configuration); CompilationUnit cu = StaticJavaParser.parse(CODE); List names = cu.findAll(NameExpr.class); for (NameExpr ne : names) { System.out.println(ne.toString()); String type = "???"; try { type = ne.resolve().toString(); } catch (Exception e) { } System.out.println(">>" + type); } } }