Search in sources :

Example 81 with ReflectionTypeSolver

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

the class JavaParserFacadeResolutionTest method solveTryWithResourceVariable.

// See issue 119
@Test
public void solveTryWithResourceVariable() {
    String code = "import java.util.Scanner; class A { void foo() { try (Scanner sc = new Scanner(System.in)) {\n" + "    sc.nextLine();\n" + "} } }";
    CompilationUnit cu = JavaParser.parse(code);
    MethodCallExpr methodCallExpr = Navigator.findMethodCall(cu, "nextLine").get();
    Expression scope = methodCallExpr.getScope().get();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(scope);
    assertEquals(true, type.isReferenceType());
    assertEquals("java.util.Scanner", type.asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) 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 82 with ReflectionTypeSolver

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

the class JavaParserFacadeResolutionTest method classToResolvedTypeViaReflection.

@Test
public void classToResolvedTypeViaReflection() {
    Class<?> clazz = this.getClass();
    ReflectionTypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
    JavaParserFacade facade = JavaParserFacade.get(reflectionTypeSolver);
    ResolvedType resolvedType = facade.classToResolvedType(clazz);
    assertNotNull(resolvedType);
    assertTrue(resolvedType.isReferenceType());
    assertEquals(clazz.getCanonicalName(), resolvedType.asReferenceType().getQualifiedName());
}
Also used : JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 83 with ReflectionTypeSolver

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

the class JavaParserFacadeResolutionTest method typeDeclarationSuperClassImplicitlyIncludeObject.

@Test
public void typeDeclarationSuperClassImplicitlyIncludeObject() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
    ResolvedTypeDeclaration typeDeclaration = JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(clazz);
    ResolvedReferenceType superclass = typeDeclaration.asClass().getSuperClass();
    assertEquals(Object.class.getCanonicalName(), superclass.getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 84 with ReflectionTypeSolver

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

the class LambdaResolutionTest method lambdaReduce.

@Test
public void lambdaReduce() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "reduce");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expr = returnStmt.getExpression().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type1 = javaParserFacade.getType(expr);
    assertEquals("java.util.Optional<java.lang.Integer>", type1.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) 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 85 with ReflectionTypeSolver

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

the class LambdaResolutionTest method lambdaBlockExplicitReturn.

@Test
public void lambdaBlockExplicitReturn() {
    CompilationUnit cu = parseSample("LambdaMulti");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaSingleReturn");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(expression);
    assertEquals("java.lang.String", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) 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)

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