Search in sources :

Example 1 with LambdaExprContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext in project javaparser by javaparser.

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInFieldDecl.

@Test
public void solveParameterOfLambdaInFieldDecl() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    VariableDeclarator field = Navigator.demandField(clazz, "functional");
    LambdaExpr lambdaExpr = (LambdaExpr) field.getInitializer().get();
    File src = new File("src/test/resources");
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    combinedTypeSolver.add(new JavaParserTypeSolver(adaptPath(src)));
    Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 2 with LambdaExprContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext in project javaparser by javaparser.

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInVarDecl.

@Test
public void solveParameterOfLambdaInVarDecl() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "testFunctionalVar");
    VariableDeclarator varDecl = Navigator.demandVariableDeclaration(method, "a").get();
    LambdaExpr lambdaExpr = (LambdaExpr) varDecl.getInitializer().get();
    File src = adaptPath(new File("src/test/resources"));
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    combinedTypeSolver.add(new JavaParserTypeSolver(src));
    Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 3 with LambdaExprContext

use of com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext in project javaparser by javaparser.

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInMethodCallExpr.

@Test
public void solveParameterOfLambdaInMethodCallExpr() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaMap");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    MethodCallExpr methodCallExpr = (MethodCallExpr) returnStmt.getExpression().get();
    LambdaExpr lambdaExpr = (LambdaExpr) methodCallExpr.getArguments().get(0);
    Context context = new LambdaExprContext(lambdaExpr, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("? super java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)3 Context (com.github.javaparser.symbolsolver.core.resolution.Context)3 LambdaExprContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext)3 Value (com.github.javaparser.symbolsolver.model.resolution.Value)3 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)3 Test (org.junit.Test)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)2 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)2 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)2 File (java.io.File)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1