-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
InternalClassInInterfaceTest.java
114 lines (85 loc) · 5.65 KB
/
InternalClassInInterfaceTest.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
106
107
108
109
110
111
112
113
114
package com.github.javaparser.symbolsolver.resolution;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
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.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class InternalClassInInterfaceTest {
@Test
public void resolveFieldOfEnumAsInternalClassOfInterfaceUnqualifiedSamePackage() throws FileNotFoundException {
File src = new File("src/test/resources/internalClassInInterface");
File aClass = new File(src.getPath() + File.separator + "foo" + File.separator+ "bar"
+ File.separator + "AClass.java");
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(src));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
JavaParser.setStaticConfiguration(parserConfiguration);
CompilationUnit cu = JavaParser.parse(aClass);
Optional<FieldAccessExpr> fae = cu.findFirst(FieldAccessExpr.class, n -> n.toString().equals("AnInterface.ListChangeType.ADDITION") && n.getRange().get().begin.line == 4);
assertTrue(fae.isPresent());
assertEquals("foo.bar.AnInterface.ListChangeType", fae.get().resolve().getType().describe());
assertEquals("ADDITION", fae.get().resolve().getName());
}
@Test
public void resolveFieldOfEnumAsInternalClassOfInterfaceQualifiedSamePackage() throws FileNotFoundException {
File src = new File("src/test/resources/internalClassInInterface");
File aClass = new File(src.getPath() + File.separator + "foo" + File.separator+ "bar"
+ File.separator + "AClass.java");
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(src));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
JavaParser.setStaticConfiguration(parserConfiguration);
CompilationUnit cu = JavaParser.parse(aClass);
Optional<FieldAccessExpr> fae = cu.findFirst(FieldAccessExpr.class, n -> n.toString().equals("foo.bar.AnInterface.ListChangeType.ADDITION") && n.getRange().get().begin.line == 5);
assertTrue(fae.isPresent());
assertEquals("foo.bar.AnInterface.ListChangeType", fae.get().resolve().getType().describe());
assertEquals("ADDITION", fae.get().resolve().getName());
}
@Test
public void resolveFieldOfEnumAsInternalClassOfInterfaceUnqualifiedDifferentPackage() throws FileNotFoundException {
File src = new File("src/test/resources/internalClassInInterface");
File aClass = new File(src.getPath() + File.separator + "foo" + File.separator+ "bar"
+ File.separator + "differentpackage" + File.separator + "AClass2.java");
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(src));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
JavaParser.setStaticConfiguration(parserConfiguration);
CompilationUnit cu = JavaParser.parse(aClass);
Optional<FieldAccessExpr> fae = cu.findFirst(FieldAccessExpr.class, n -> n.toString().equals("AnInterface.ListChangeType.ADDITION") && n.getRange().get().begin.line == 6);
assertTrue(fae.isPresent());
assertEquals("foo.bar.AnInterface.ListChangeType", fae.get().resolve().getType().describe());
assertEquals("ADDITION", fae.get().resolve().getName());
}
@Test
public void resolveFieldOfEnumAsInternalClassOfInterfaceQualifiedDifferentPackage() throws FileNotFoundException {
File src = new File("src/test/resources/internalClassInInterface");
File aClass = new File(src.getPath() + File.separator + "foo" + File.separator+ "bar"
+ File.separator + "differentpackage" + File.separator + "AClass2.java");
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(src));
ParserConfiguration parserConfiguration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
JavaParser.setStaticConfiguration(parserConfiguration);
CompilationUnit cu = JavaParser.parse(aClass);
Optional<FieldAccessExpr> fae = cu.findFirst(FieldAccessExpr.class, n -> n.toString().equals("foo.bar.AnInterface.ListChangeType.ADDITION") && n.getRange().get().begin.line == 7);
assertTrue(fae.isPresent());
assertEquals("foo.bar.AnInterface.ListChangeType", fae.get().resolve().getType().describe());
assertEquals("ADDITION", fae.get().resolve().getName());
}
}