Search in sources :

Example 21 with JavaParserFacade

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

the class LambdaResolutionTest method typeOfVoidLambda.

@Test
public void typeOfVoidLambda() {
    CompilationUnit cu = parseSample("LambdaVoid");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaEmpty");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    Expression expression = returnStmt.getExpression().get();
    LambdaExpr lambdaExpr = Navigator.findNodeOfGivenClass(expression, LambdaExpr.class);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(lambdaExpr);
    assertEquals("void", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) 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 22 with JavaParserFacade

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

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

the class Widget method main.

public static void main(String[] args) throws IOException, ParseException {
    File src = new File(JAVA_ROOT);
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver(true));
    combinedTypeSolver.add(new JavaParserTypeSolver(src));
    CompilationUnit compilationUnit = JavaParser.parse(new File(CLASS));
    JavaParserFacade parserFacade = JavaParserFacade.get(combinedTypeSolver);
    MethodDeclaration methodDeclaration = compilationUnit.getNodesByType(MethodDeclaration.class).stream().filter(node -> node.getName().equals("doSomething")).findAny().orElse(null);
    methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 24 with JavaParserFacade

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

the class Issue232 method issue232.

@Test
public void issue232() {
    CompilationUnit cu = parseSample("Issue232");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    Context context = JavaParserFactory.getContext(cls, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> reference = context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>", typeSolver);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 25 with JavaParserFacade

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

the class Issue251 method testSolveStaticallyImportedMemberType.

@Test
public void testSolveStaticallyImportedMemberType() {
    CompilationUnit cu = parseSample("Issue251");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodDeclaration m = Navigator.demandMethod(cls, "bar");
    ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(1);
    MethodCallExpr expression = (MethodCallExpr) stmt.getExpression();
    Assert.assertNotNull(javaParserFacade.solve(expression));
}
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) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) 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