Search in sources :

Example 41 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ContextTest method resolveCascadeOfReferencesToMethod.

@Test
public void resolveCascadeOfReferencesToMethod() throws ParseException, IOException {
    CompilationUnit cu = parseSample("Navigator");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
    MethodCallExpr callToStream = Navigator.findMethodCall(method, "stream").get();
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToJar), new ReflectionTypeSolver(true));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    MethodUsage ref = symbolSolver.solveMethod("stream", Collections.emptyList(), callToStream);
    assertEquals("stream", ref.getName());
    assertEquals("java.util.Collection", ref.declaringType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 42 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ContextTest method resolveReferenceToMethod.

@Test
public void resolveReferenceToMethod() throws ParseException, IOException {
    CompilationUnit cu = parseSample("Navigator");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "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 JarTypeSolver(pathToJar), new ReflectionTypeSolver(true));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    MethodUsage ref = symbolSolver.solveMethod("getTypes", Collections.emptyList(), callToGetTypes);
    assertEquals("getTypes", ref.getName());
    assertEquals("com.github.javaparser.ast.CompilationUnit", ref.declaringType().getQualifiedName());
// verify(typeSolver);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 43 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ContextTest method resolveLambdaType.

@Test
public void resolveLambdaType() 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();
    Expression lambdaExpr = callToFilter.getArguments().get(0);
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    ResolvedType typeOfLambdaExpr = JavaParserFacade.get(typeSolver).getType(lambdaExpr);
    assertEquals("java.util.function.Predicate<? super com.github.javaparser.ast.body.TypeDeclaration>", typeOfLambdaExpr.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 44 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ContextTest method resolveInheritedMethodFromInterface.

@Test
public void resolveInheritedMethodFromInterface() {
    CompilationUnit cu = parseSample("InterfaceInheritance");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Test");
    MethodDeclaration method = Navigator.demandMethod(clazz, "test");
    MethodCallExpr call = Navigator.findMethodCall(method, "foobar").get();
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(call);
    assertEquals("double", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) File(java.io.File) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 45 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ContextTest method resolveReferenceToMethodCalledOnArrayAccess.

@Test
public void resolveReferenceToMethodCalledOnArrayAccess() {
    CompilationUnit cu = parseSample("ArrayAccess");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ArrayAccess");
    MethodDeclaration method = Navigator.demandMethod(clazz, "access");
    MethodCallExpr callToTrim = Navigator.findMethodCall(method, "trim").get();
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    MethodUsage ref = symbolSolver.solveMethod("trim", Collections.emptyList(), callToTrim);
    assertEquals("trim", ref.getName());
    assertEquals("java.lang.String", ref.declaringType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) File(java.io.File) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)114 Test (org.junit.Test)89 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)76 CompilationUnit (com.github.javaparser.ast.CompilationUnit)59 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)46 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)40 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)33 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)32 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)30 MethodUsage (com.github.javaparser.resolution.MethodUsage)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)25 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)21 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)20 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)18 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)18 Collectors (java.util.stream.Collectors)17 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)16 ResolvedClassDeclaration (com.github.javaparser.resolution.declarations.ResolvedClassDeclaration)13 java.util (java.util)13 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)12