Search in sources :

Example 56 with TypeSolver

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

the class StatementContextResolutionTest method resolveMethodOnGenericClass.

@Test
public void resolveMethodOnGenericClass() {
    CompilationUnit cu = parseSample("LocalVariableInParent");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo5");
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 57 with TypeSolver

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

the class JavaParserTypeParameterResolutionTest method declaredOnMethodPositiveCase.

@Test
public void declaredOnMethodPositiveCase() {
    CompilationUnit cu = parseSample("MethodTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    for (ResolvedTypeParameterDeclaration tp : methodDeclaration.getTypeParameters()) {
        assertTrue(tp instanceof JavaParserTypeParameter);
        assertEquals("C", tp.getName());
        assertEquals(true, tp.declaredOnMethod());
        assertEquals(false, tp.declaredOnType());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 58 with TypeSolver

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

the class SymbolResolutionResolutionTest method conditionalExpressionExample.

@Test
public void conditionalExpressionExample() {
    CompilationUnit cu = parseSample("JreConditionalExpression");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 59 with TypeSolver

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

the class SymbolResolutionResolutionTest method getTypeOfFieldAccess.

@Test
public void getTypeOfFieldAccess() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field.getInitializer().get());
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 60 with TypeSolver

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

the class SymbolResolutionResolutionTest method getTypeOfField.

@Test
public void getTypeOfField() {
    CompilationUnit cu = parseSample("ReflectionFieldOfItself");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field);
    assertEquals("int", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) 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