Search in sources :

Example 16 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade 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 17 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade 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 18 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade 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)

Example 19 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class LambdaResolutionTest method lambdaMap.

@Test
public void lambdaMap() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration m1 = Navigator.demandMethod(clazz, "lambdaMap");
    MethodDeclaration m2 = Navigator.demandMethod(clazz, "lambdaMap2");
    ReturnStmt returnStmt1 = Navigator.findReturnStmt(m1);
    ReturnStmt returnStmt2 = Navigator.findReturnStmt(m2);
    Expression e1 = returnStmt1.getExpression().get();
    Expression e2 = returnStmt2.getExpression().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type1 = javaParserFacade.getType(e1);
    ResolvedType type2 = javaParserFacade.getType(e2);
    assertEquals("java.util.stream.Stream<java.lang.String>", type1.describe());
    assertEquals("java.util.stream.Stream<java.util.stream.IntStream>", type2.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 20 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class LambdaResolutionTest method lambdaBlockMultiLineReturn.

@Test
public void lambdaBlockMultiLineReturn() {
    CompilationUnit cu = parseSample("LambdaMulti");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "multiLineReturn");
    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

JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 CompilationUnit (com.github.javaparser.ast.CompilationUnit)44 Test (org.junit.Test)42 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)40 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)34 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)27 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)23 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)23 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)21 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)19 Expression (com.github.javaparser.ast.expr.Expression)18 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)14 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)8 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)8 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)7 MethodUsage (com.github.javaparser.resolution.MethodUsage)6 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 File (java.io.File)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 JavaParserTypeParameter (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter)2