use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class ContextTest method resolveReferenceToImportedType.
@Test
public void resolveReferenceToImportedType() {
CompilationUnit cu = parseSample("Navigator");
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("com.github.javaparser.ast.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("com.github.javaparser.ast.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("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class ContextTest method resolveTypeUsageOfCascadeMethodInGenericClass.
@Test
public void resolveTypeUsageOfCascadeMethodInGenericClass() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToFilter = Navigator.findMethodCall(method, "filter").get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage filterUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToFilter);
assertEquals("java.util.stream.Stream<com.github.javaparser.ast.body.TypeDeclaration>", filterUsage.returnType().describe());
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class ContextTest method resolveTypeUsageOfFirstMethodInGenericClass.
@Test
public void resolveTypeUsageOfFirstMethodInGenericClass() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr callToGetTypes = Navigator.findMethodCall(method, "getTypes").get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage filterUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToGetTypes);
assertEquals("java.util.List<com.github.javaparser.ast.body.TypeDeclaration>", filterUsage.returnType().describe());
assertEquals(1, filterUsage.returnType().asReferenceType().typeParametersValues().size());
assertEquals("com.github.javaparser.ast.body.TypeDeclaration", filterUsage.returnType().asReferenceType().typeParametersValues().get(0).describe());
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class ContextTest method resolveReferenceToOverloadMethodFindStricter.
@Test
public void resolveReferenceToOverloadMethodFindStricter() {
CompilationUnit cu = parseSample("OverloadedMethods");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
MethodDeclaration method = Navigator.demandMethod(clazz, "m2");
MethodCallExpr call = Navigator.findMethodCall(method, "overloaded").get();
ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
MethodUsage ref = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
assertEquals("overloaded", ref.getName());
assertEquals(1, ref.getNoParams());
assertEquals("java.lang.String", ref.getParamTypes().get(0).describe());
}
use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.
the class ContextTest method resolveParameterReference.
@Test
public void resolveParameterReference() {
CompilationUnit cu = parseSample("ReferencesToParameter");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferenceToParameter");
MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "aMethod");
NameExpr foo = Navigator.findNameExpression(method1, "foo").get();
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference symbolReference = symbolSolver.solveSymbol("foo", foo);
assertEquals(true, symbolReference.isSolved());
assertEquals("foo", symbolReference.getCorrespondingDeclaration().getName());
assertEquals(true, symbolReference.getCorrespondingDeclaration().isParameter());
}
Aggregations