Search in sources :

Example 51 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class FieldsResolutionTest method accessFieldThroughSuper.

@Test
public void accessFieldThroughSuper() {
    CompilationUnit cu = parseSample("AccessThroughSuper");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessThroughSuper.SubClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "fieldTest");
    ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0);
    Expression expression = returnStmt.getExpression().get();
    ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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 52 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType 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 53 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class JavaParserFacadeResolutionTest method solveMultiCatchType.

@Test
public void solveMultiCatchType() {
    String code = "class A {\n" + "        public void foo() {\n" + "            try {\n" + "                \n" + "            } catch (IllegalStateException | IllegalArgumentException e) {\n" + "                \n" + "            }\n" + "        }\n" + "    }";
    CompilationUnit cu = parseWithTypeSolver(code);
    CatchClause catchClause = Navigator.findNodeOfGivenClass(cu, CatchClause.class);
    Type jpType = catchClause.getParameter().getType();
    ResolvedType jssType = jpType.resolve();
    assertEquals(true, jssType instanceof ResolvedUnionType);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedUnionType(com.github.javaparser.resolution.types.ResolvedUnionType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Type(com.github.javaparser.ast.type.Type) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) CatchClause(com.github.javaparser.ast.stmt.CatchClause) ResolvedUnionType(com.github.javaparser.resolution.types.ResolvedUnionType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 54 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType 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 55 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType 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)

Aggregations

ResolvedType (com.github.javaparser.resolution.types.ResolvedType)119 Test (org.junit.Test)78 CompilationUnit (com.github.javaparser.ast.CompilationUnit)68 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)58 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)41 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)39 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)32 Expression (com.github.javaparser.ast.expr.Expression)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)22 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)21 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)20 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)18 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)18 MethodUsage (com.github.javaparser.resolution.MethodUsage)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)15 Context (com.github.javaparser.symbolsolver.core.resolution.Context)15 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)10 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)10 Collectors (java.util.stream.Collectors)10