Skip to content

Commit b811a9c

Browse files
Remove unnecessary resolution attempts in modules when solving symbols as values
1 parent 60b1960 commit b811a9c

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/javaparsermodel/contexts/ClassOrInterfaceDeclarationContext.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String na
6767
return SymbolReference.solved(this.getDeclaration().getVisibleField(name));
6868
}
6969

70-
// Compact classes implicitly import the java.base module. To avoid having to add this import explicitly,
71-
// first check if the class is compact and then try to solve the given type in the java.base module.
72-
if (wrappedNode.isCompact()) {
73-
SymbolReference<ResolvedValueDeclaration> maybeSolved = solveInJavaBaseModule(name);
74-
if (maybeSolved.isSolved()) {
75-
return maybeSolved;
76-
}
77-
}
78-
7970
// then to parent
8071
return solveSymbolInParentContext(name);
8172
}
@@ -88,33 +79,10 @@ public Optional<Value> solveSymbolAsValue(String name) {
8879
return Optional.of(Value.from(this.getDeclaration().getField(name)));
8980
}
9081

91-
// Compact classes implicitly import the java.base module. To avoid having to add this import explicitly,
92-
// first check if the class is compact and then try to solve the given type in the java.base module.
93-
if (wrappedNode.isCompact()) {
94-
SymbolReference<ResolvedValueDeclaration> maybeSolved = solveInJavaBaseModule(name);
95-
if (maybeSolved.isSolved()) {
96-
return Optional.of(Value.from(maybeSolved.getCorrespondingDeclaration()));
97-
}
98-
}
99-
10082
// then to parent
10183
return solveSymbolAsValueInParentContext(name);
10284
}
10385

104-
/**
105-
* Compact classes implicitly import the java.base module, so when resolving a type in a compact class
106-
* this needs to be checked.
107-
*/
108-
private SymbolReference<ResolvedValueDeclaration> solveInJavaBaseModule(String name) {
109-
SymbolReference<ResolvedReferenceTypeDeclaration> maybeSolved =
110-
typeSolver.tryToSolveTypeInModule(JAVA_BASE_MODULE_NAME, name);
111-
if (maybeSolved.isSolved() && maybeSolved.getDeclaration().get() instanceof ResolvedValueDeclaration) {
112-
return SymbolReference.solved(
113-
(ResolvedValueDeclaration) maybeSolved.getDeclaration().get());
114-
}
115-
return SymbolReference.unsolved();
116-
}
117-
11886
@Override
11987
public Optional<ResolvedType> solveGenericType(String name) {
12088
// First check if the method-like declaration has type parameters defined.

javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/javaparsermodel/declarations/JavaParserVariableDeclarationTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.github.javaparser.ParserConfiguration;
3131
import com.github.javaparser.ast.CompilationUnit;
3232
import com.github.javaparser.ast.Node;
33+
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
3334
import com.github.javaparser.ast.body.VariableDeclarator;
3435
import com.github.javaparser.ast.expr.NameExpr;
3536
import com.github.javaparser.resolution.declarations.AssociableToAST;
@@ -170,4 +171,35 @@ void javaBaseTypeFromImplicitCompactClassImport() {
170171
"java.util.List<java.lang.String>",
171172
declarator.getType().resolve().describe());
172173
}
174+
175+
@Test
176+
@EnabledForJreRange(min = JRE.JAVA_9)
177+
void javaBaseTypeFromImplicitCompactClassImportSolvedAsSymbol() {
178+
String code = "List<String> l; void main() { l = new ArrayList<>(); }";
179+
180+
ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
181+
ParserConfiguration parserConfiguration = new ParserConfiguration()
182+
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_25)
183+
.setSymbolResolver(new JavaSymbolSolver(typeSolver));
184+
JavaParser parser = new JavaParser(parserConfiguration);
185+
186+
ParseResult<CompilationUnit> parseResult = parser.parse(code);
187+
188+
assertTrue(parseResult.isSuccessful());
189+
190+
CompilationUnit cu = parseResult.getResult().get();
191+
192+
ClassOrInterfaceDeclaration compactClass =
193+
cu.findFirst(ClassOrInterfaceDeclaration.class).get();
194+
195+
JavaParserClassDeclaration resolvedClass = (JavaParserClassDeclaration) compactClass.resolve();
196+
197+
assertEquals(
198+
"java.util.List<java.lang.String>",
199+
resolvedClass
200+
.solveSymbol("l", typeSolver)
201+
.getCorrespondingDeclaration()
202+
.getType()
203+
.describe());
204+
}
173205
}

0 commit comments

Comments
 (0)