-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Issue1491Test.java
105 lines (86 loc) · 4.3 KB
/
Issue1491Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException;
import static com.github.javaparser.StaticJavaParser.parse;
class Issue1491Test {
@Test
void verifyIssue1491SolvingClassInSameFile() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(new VoidVisitorAdapter() {
public void visit(NameExpr n, Object arg) {
ResolvedType type = JavaParserFacade.get(localCts)
.getType(n);
super.visit(n, arg);
}
}, null);
}
@Test
void verifyIssue1491ResolvingStaticMethodCalls() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(new VoidVisitorAdapter() {
public void visit(MethodCallExpr n, Object arg) {
ResolvedMethodDeclaration decl = JavaParserFacade.get(localCts).solve(n).getCorrespondingDeclaration();
super.visit(n, arg);
}
}, null);
}
@Test
void verifyIssue1491Combined() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(new VoidVisitorAdapter<Void>() {
public void visit(NameExpr n, Void arg) {
try {
ResolvedType type = JavaParserFacade.get(localCts).getType(n);
} catch (UnsolvedSymbolException e) {
throw new RuntimeException("Unable to solve name expr at " + n.getRange(), e);
}
super.visit(n, arg);
}
public void visit(MethodCallExpr n, Void arg) {
ResolvedMethodDeclaration decl = JavaParserFacade.get(localCts).solve(n).getCorrespondingDeclaration();
super.visit(n, arg);
}
}, null);
}
}