use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class JavaParserFacadeResolutionTest method parseWithTypeSolver.
private CompilationUnit parseWithTypeSolver(String code) {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(typeSolver));
JavaParser javaParser = new JavaParser(parserConfiguration);
return javaParser.parse(ParseStart.COMPILATION_UNIT, new StringProvider(code)).getResult().get();
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class MethodsResolutionTest method solveMethodInInterfaceParent.
@Test
public void solveMethodInInterfaceParent() {
CompilationUnit cu = parseSample("MethodCalls");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
MethodDeclaration method = Navigator.demandMethod(clazz, "inheritedInterfaceMethod");
MethodCallExpr expression = Navigator.findMethodCall(method, "toString").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodUsage call1 = javaParserFacade.solveMethodAsUsage(expression);
assertEquals("java.lang.Object.toString()", call1.getQualifiedSignature());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class StatementContextResolutionTest method resolveLocalAndSeveralAnnidatedLevels.
@Test
public void resolveLocalAndSeveralAnnidatedLevels() {
CompilationUnit cu = parseSample("LocalVariableInParent");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo4");
MethodCallExpr call = Navigator.findMethodCall(method, "add").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
assertTrue(ref.isSolved());
assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
assertEquals("add", methodUsage.getName());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ContextTest method resolveReferenceToMethodWithLambda.
@Test
public void resolveReferenceToMethodWithLambda() {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr methodCallExpr = Navigator.findMethodCall(method, "filter").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(methodCallExpr);
assertEquals("java.util.stream.Stream<java.lang.String>", ref.describe());
assertEquals(1, ref.asReferenceType().typeParametersValues().size());
assertEquals("java.lang.String", ref.asReferenceType().typeParametersValues().get(0).describe());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ContextTest method resolveReferenceToClassesInTheSamePackage.
@Test
public void resolveReferenceToClassesInTheSamePackage() {
CompilationUnit cu = parseSample("Navigator3");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(0);
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("my.packagez.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("my.packagez.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param);
assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("my.packagez.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
Aggregations