source: java2python/core/src/test/java/javaparser/ArraysResolveTest.java

Last change on this file was 346, checked in by wouter, 2 years ago

version 0.1 of automatic java to python translator. Can translate some simple programs, lot of work remains to be done

File size: 5.2 KB
Line 
1package javaparser;
2
3import java.util.Arrays;
4import java.util.List;
5
6import org.junit.Ignore;
7import org.junit.Test;
8
9import com.github.javaparser.JavaParser;
10import com.github.javaparser.ParserConfiguration;
11import com.github.javaparser.StaticJavaParser;
12import com.github.javaparser.ast.CompilationUnit;
13import com.github.javaparser.ast.body.BodyDeclaration;
14import com.github.javaparser.ast.body.MethodDeclaration;
15import com.github.javaparser.ast.expr.MethodCallExpr;
16import com.github.javaparser.ast.expr.NameExpr;
17import com.github.javaparser.ast.expr.VariableDeclarationExpr;
18import com.github.javaparser.ast.stmt.ExpressionStmt;
19import com.github.javaparser.symbolsolver.JavaSymbolSolver;
20import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
21import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
22
23public class ArraysResolveTest {
24
25 public static void main(String[] args) {
26 Integer v = 3;
27 List<Integer> x = Arrays.asList(1, 2, v);
28 }
29
30 String CODE = "import java.util.Arrays;\n"
31 + "public class ArraysResolveTest {\n" + "\n"
32 + " public static void main(String[] args) {\n"
33 + " List<Integer> x = Arrays.asList(1, 2);\n"//
34 + " }\n" + "}";
35
36 @Ignore // nonworking version replicating issue in our code
37 @Test
38 public void testResolve() {
39 ParserConfiguration conf = new ParserConfiguration();
40 CombinedTypeSolver typeSolver = new CombinedTypeSolver();
41 typeSolver.add(new ReflectionTypeSolver(false));
42 // FIXME add sources of jars source code etc
43 // typeSolver.add(new JavaParserTypeSolver(new File(SOURCE_PATH)));
44 // typeSolver.add(new JarTypeSolver(jarSourceFile));
45 JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
46 conf.setSymbolResolver(symbolSolver);
47 JavaParser parser = new JavaParser(conf);
48
49 CompilationUnit res = parser.parse(CODE).getResult().get();
50
51 BodyDeclaration<?> mainmethod = res.getType(0).getMembers().get(0);
52 ExpressionStmt statement = (ExpressionStmt) ((MethodDeclaration) mainmethod)
53 .getBody().get().getStatements().get(0);
54 VariableDeclarationExpr exp = (VariableDeclarationExpr) statement
55 .getExpression();
56 MethodCallExpr initvalue = (MethodCallExpr) exp.getVariable(0)
57 .getInitializer().get();
58 NameExpr scope = (NameExpr) initvalue.getScope().get();
59 scope.resolve();
60 }
61
62 @Test
63 public void testResolveFixed() {
64 ParserConfiguration conf = new ParserConfiguration();
65 CombinedTypeSolver typeSolver = new CombinedTypeSolver();
66 typeSolver.add(new ReflectionTypeSolver(false));
67 // FIXME add sources of jars source code etc
68 // typeSolver.add(new JavaParserTypeSolver(new File(SOURCE_PATH)));
69 // typeSolver.add(new JarTypeSolver(jarSourceFile));
70 JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
71 conf.setSymbolResolver(symbolSolver);
72 JavaParser parser = new JavaParser(conf);
73
74 CompilationUnit res = parser.parse(CODE).getResult().get();
75
76 BodyDeclaration<?> mainmethod = res.getType(0).getMembers().get(0);
77 ExpressionStmt statement = (ExpressionStmt) ((MethodDeclaration) mainmethod)
78 .getBody().get().getStatements().get(0);
79 VariableDeclarationExpr exp = (VariableDeclarationExpr) statement
80 .getExpression();
81 MethodCallExpr initvalue = (MethodCallExpr) exp.getVariable(0)
82 .getInitializer().get();
83 NameExpr scope = (NameExpr) initvalue.getScope().get();
84 System.out.println(scope.calculateResolvedType());
85 }
86
87 /**
88 * Solution proposed by https://github.com/javaparser/javaparser/issues/3689
89 */
90 @Test
91 public void test3689() {
92 String CODE = "import java.util.Arrays;\n"
93 + "public class ArraysResolveTest {\n" + "\n"
94 + " public static void main(String[] args) {\n"
95 + " List<Integer> x = Arrays.asList(1, 2);\n"//
96 + " }\n" + "}";
97
98 ParserConfiguration configuration = new ParserConfiguration()
99 .setSymbolResolver(new JavaSymbolSolver(
100 new CombinedTypeSolver(new ReflectionTypeSolver())));
101 StaticJavaParser.setConfiguration(configuration);
102
103 CompilationUnit cu = StaticJavaParser.parse(CODE);
104
105 List<MethodCallExpr> mces = cu.findAll(MethodCallExpr.class);
106
107 for (MethodCallExpr mce : mces) {
108 System.out.println(mce.toString());
109 System.out.println(">>" + mce.resolve().toString());
110 // find the/ method declaration
111 System.out.println(">>"
112 + mce.getScope().get().calculateResolvedType().describe());
113 // find the type of the scope of the method call expression
114 }
115 }
116
117 @Test
118 public void modifiedTest() {
119 String CODE = "import java.util.Arrays;\n"
120 + "public class ArraysResolveTest {\n"
121 + " public static void main(String[] args) {\n"
122 + " Integer v = 3;\n"
123 + " List<Integer> x = Arrays.asList(1, 2, v);\n"///
124 + " }\n}";
125 ParserConfiguration configuration = new ParserConfiguration()
126 .setSymbolResolver(new JavaSymbolSolver(
127 new CombinedTypeSolver(new ReflectionTypeSolver())));
128 StaticJavaParser.setConfiguration(configuration);
129
130 CompilationUnit cu = StaticJavaParser.parse(CODE);
131
132 List<NameExpr> names = cu.findAll(NameExpr.class);
133
134 for (NameExpr ne : names) {
135 System.out.println(ne.toString());
136 String type = "???";
137 try {
138 type = ne.resolve().toString();
139 } catch (Exception e) {
140 }
141 System.out.println(">>" + type);
142 }
143 }
144
145}
Note: See TracBrowser for help on using the repository browser.