Search in sources :

Example 56 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class GenericsResolutionTest method typeParamOnReturnTypeStep3.

@Test
public void typeParamOnReturnTypeStep3() {
    CompilationUnit cu = parseSample("TypeParamOnReturnType");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType");
    MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals");
    MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(call);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.lang.Boolean", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 57 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class GenericsResolutionTest method classCastScope.

@Test
public void classCastScope() {
    CompilationUnit cu = parseSample("ClassCast");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassCast");
    MethodDeclaration method = Navigator.demandMethod(clazz, "getNodesByType");
    MethodCallExpr call = Navigator.findMethodCall(method, "cast").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    Expression scope = call.getScope().get();
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(scope);
    // System.out.println(typeUsage);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.lang.Class<N>", type.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) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 58 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class GenericsResolutionTest method classCast.

@Test
public void classCast() {
    CompilationUnit cu = parseSample("ClassCast");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassCast");
    MethodDeclaration method = Navigator.demandMethod(clazz, "getNodesByType");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(returnStmt.getExpression().get());
    assertEquals(true, type.isTypeVariable());
    assertEquals("N", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 59 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class GenericsResolutionTest method typeParamOnReturnTypeStep1.

@Test
public void typeParamOnReturnTypeStep1() {
    CompilationUnit cu = parseSample("TypeParamOnReturnType");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType");
    MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals");
    ThisExpr thisExpr = Navigator.findNodeOfGivenClass(method, ThisExpr.class);
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(thisExpr);
    assertEquals(false, type.isTypeVariable());
    assertEquals("TypeParamOnReturnType", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 60 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class GenericsResolutionTest method typeParamOnReturnTypeStep2.

@Test
public void typeParamOnReturnTypeStep2() {
    CompilationUnit cu = parseSample("TypeParamOnReturnType");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType");
    MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals");
    NameExpr n1 = Navigator.findNameExpression(method, "n1").get();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(n1);
    assertEquals(true, type.isTypeVariable());
    assertEquals("T", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)206 Test (org.junit.Test)165 CompilationUnit (com.github.javaparser.ast.CompilationUnit)128 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)81 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)77 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)75 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)57 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)57 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)52 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)47 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)41 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)31 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)28 Expression (com.github.javaparser.ast.expr.Expression)26 Before (org.junit.Before)26 Context (com.github.javaparser.symbolsolver.core.resolution.Context)25 MethodUsage (com.github.javaparser.resolution.MethodUsage)23 File (java.io.File)23 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)22 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)22