Search in sources :

Example 6 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr 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 7 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr 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 8 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.

the class StatementContext method solveSymbolAsValue.

@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    // if we're in a multiple Variable declaration line (for ex: double a=0, b=a;)
    SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(wrappedNode, typeSolver);
    Optional<Value> symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
    if (symbolReference.isPresent()) {
        return symbolReference;
    }
    // we should look in all the statements preceding, treating them as SymbolDeclarators
    if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof IfStmt) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    if (!(requireParentNode(wrappedNode) instanceof NodeWithStatements)) {
        return getParent().solveSymbolAsValue(name, typeSolver);
    }
    NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) requireParentNode(wrappedNode);
    int position = -1;
    for (int i = 0; i < nodeWithStmt.getStatements().size(); i++) {
        if (nodeWithStmt.getStatements().get(i).equals(wrappedNode)) {
            position = i;
        }
    }
    if (position == -1) {
        throw new RuntimeException();
    }
    for (int i = position - 1; i >= 0; i--) {
        symbolDeclarator = JavaParserFactory.getSymbolDeclarator(nodeWithStmt.getStatements().get(i), typeSolver);
        symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
        if (symbolReference.isPresent()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    Context parentContext = getParent();
    return parentContext.solveSymbolAsValue(name, typeSolver);
}
Also used : Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Value(com.github.javaparser.symbolsolver.model.resolution.Value)

Example 9 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.

the class StatementContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    // if we're in a multiple Variable declaration line (for ex: double a=0, b=a;)
    SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(wrappedNode, typeSolver);
    SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
    if (symbolReference.isSolved()) {
        return symbolReference;
    }
    // we should look in all the statements preceding, treating them as SymbolDeclarators
    if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
        return getParent().solveSymbol(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.ConstructorDeclaration) {
        return getParent().solveSymbol(name, typeSolver);
    }
    if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
        return getParent().solveSymbol(name, typeSolver);
    }
    if (!(requireParentNode(wrappedNode) instanceof NodeWithStatements)) {
        return getParent().solveSymbol(name, typeSolver);
    }
    NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) requireParentNode(wrappedNode);
    int position = -1;
    for (int i = 0; i < nodeWithStmt.getStatements().size(); i++) {
        if (nodeWithStmt.getStatements().get(i).equals(wrappedNode)) {
            position = i;
        }
    }
    if (position == -1) {
        throw new RuntimeException();
    }
    for (int i = position - 1; i >= 0; i--) {
        symbolDeclarator = JavaParserFactory.getSymbolDeclarator(nodeWithStmt.getStatements().get(i), typeSolver);
        symbolReference = solveWith(symbolDeclarator, name);
        if (symbolReference.isSolved()) {
            return symbolReference;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr)

Example 10 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.

the class JavaParserSymbolDeclaration method getType.

@Override
public ResolvedType getType() {
    if (wrappedNode instanceof Parameter) {
        Parameter parameter = (Parameter) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
            int pos = getParamPos(parameter);
            ResolvedType lambdaType = JavaParserFacade.get(typeSolver).getType(requireParentNode(wrappedNode));
            // MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
            throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
        } else {
            final ResolvedType rawType;
            if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
                rawType = ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) parameter.getType()).getType().name());
            } else {
                rawType = JavaParserFacade.get(typeSolver).convertToUsage(parameter.getType(), wrappedNode);
            }
            if (parameter.isVarArgs()) {
                return new ResolvedArrayType(rawType);
            }
            return rawType;
        }
    } else if (wrappedNode instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        } else if (requireParentNode(wrappedNode) instanceof FieldDeclaration) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        }
    }
    throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedPrimitiveType(com.github.javaparser.resolution.types.ResolvedPrimitiveType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)12 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 Test (org.junit.Test)7 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)4 Context (com.github.javaparser.symbolsolver.core.resolution.Context)4 Value (com.github.javaparser.symbolsolver.model.resolution.Value)4 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)4 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)4 Parameter (com.github.javaparser.ast.body.Parameter)3 LambdaExprContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)2 NodeWithStatements (com.github.javaparser.ast.nodeTypes.NodeWithStatements)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)2 SymbolDeclarator (com.github.javaparser.symbolsolver.resolution.SymbolDeclarator)2 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)2