Search in sources :

Example 36 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 37 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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());
}
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 38 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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());
}
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 39 with TypeSolver

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

the class ContextTest method resolveReferenceToLambdaParam.

@Test
public void resolveReferenceToLambdaParam() 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 callToGetName = Navigator.findMethodCall(method, "getName").get();
    Expression referenceToT = callToGetName.getScope().get();
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    ResolvedType typeOfT = JavaParserFacade.get(typeSolver).getType(referenceToT);
    assertEquals("? super com.github.javaparser.ast.body.TypeDeclaration", typeOfT.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 40 with TypeSolver

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

the class ContextTest method resolveReferenceToLambdaParamBase.

@Test
public void resolveReferenceToLambdaParamBase() {
    CompilationUnit cu = parseSample("NavigatorSimplified");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    NameExpr refToT = Navigator.findNameExpression(method, "t").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType ref = javaParserFacade.getType(refToT);
    assertEquals("? super java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) NameExpr(com.github.javaparser.ast.expr.NameExpr) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) 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