Skip to content

Commit

Permalink
Fix issue 1726 TypeResolver fails on method with args to static impor…
Browse files Browse the repository at this point in the history
…ted fields
  • Loading branch information
jlerbsc committed Oct 30, 2020
1 parent 482eba3 commit 27ee3f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

package com.github.javaparser.symbolsolver.resolution;

import java.util.List;
import java.util.Optional;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedEnumConstantDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
Expand All @@ -43,11 +47,9 @@
import com.github.javaparser.symbolsolver.model.resolution.Value;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionEnumDeclaration;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration;

import java.util.List;
import java.util.Optional;

/**
* @author Federico Tomassetti
*/
Expand Down Expand Up @@ -148,6 +150,10 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbolInType(Res
if (typeDeclaration instanceof ReflectionInterfaceDeclaration) {
return ((ReflectionInterfaceDeclaration) typeDeclaration).solveSymbol(name, typeSolver);
}
if (typeDeclaration instanceof ReflectionEnumDeclaration) {
ResolvedEnumConstantDeclaration red = ((ReflectionEnumDeclaration) typeDeclaration).getEnumConstant(name);
return SymbolReference.solved(red);
}
if (typeDeclaration instanceof JavassistClassDeclaration) {
return ((JavassistClassDeclaration) typeDeclaration).solveSymbol(name, typeSolver);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.javaparser.symbolsolver;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

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.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

public class Issue1726Test extends AbstractSymbolResolutionTest {

@Test
public void test() {

TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);

String s = "import static java.util.concurrent.TimeUnit.SECONDS; \n" +
"public class A {\n" +
" public static void main( String[] args )\n" +
" {\n" +
" System.out.println(SECONDS);\n" +
" }\n" +
"}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("void", (mce.calculateResolvedType().describe()));
assertEquals("java.util.concurrent.TimeUnit", (mce.getArgument(0).calculateResolvedType().describe()));

}

}

0 comments on commit 27ee3f5

Please sign in to comment.