Search in sources :

Example 21 with LambdaExpr

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

the class CloneVisitor method visit.

@Override
public Node visit(LambdaExpr _n, Object _arg) {
    List<Parameter> lambdaParameters = visit(_n.getParameters(), _arg);
    Statement body = cloneNodes(_n.getBody(), _arg);
    return new LambdaExpr(_n.getRange(), lambdaParameters, body, _n.isParametersEnclosed());
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Parameter(com.github.javaparser.ast.body.Parameter) TypeParameter(com.github.javaparser.ast.type.TypeParameter)

Example 22 with LambdaExpr

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

the class Issue186Test method lambdaPrimitivesIssue.

@Test
void lambdaPrimitivesIssue() {
    CompilationUnit cu = parseSample("Issue186");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
    List<LambdaExpr> lambdas = methodDeclaration.findAll(LambdaExpr.class);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    assertEquals("java.util.function.Predicate<? super java.lang.String>", javaParserFacade.getType(lambdas.get(0)).describe());
    assertEquals("java.util.function.Function<? super java.lang.String, ? extends java.lang.Integer>", javaParserFacade.getType(lambdas.get(1)).describe());
    assertEquals("java.util.function.Predicate<? super java.lang.Integer>", javaParserFacade.getType(lambdas.get(2)).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.jupiter.api.Test)

Example 23 with LambdaExpr

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

the class Issue3272Test method test0.

@Test
void test0() {
    // Source code
    String sourceCode = "import java.util.function.Consumer;" + "class A {" + "   Consumer<Integer> consumer = item -> {};" + "}";
    // Setup symbol solver
    ParserConfiguration configuration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new CombinedTypeSolver(new ReflectionTypeSolver())));
    // Setup parser
    JavaParser parser = new JavaParser(configuration);
    CompilationUnit cu = parser.parse(sourceCode).getResult().get();
    // Test
    LambdaExpr expr = Navigator.demandNodeOfGivenClass(cu, LambdaExpr.class);
    ResolvedType type = expr.calculateResolvedType();
    assertEquals("java.util.function.Consumer<java.lang.Integer>", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParser(com.github.javaparser.JavaParser) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.jupiter.api.Test)

Example 24 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class RuleSetNodeVisitor method handleDecision.

private MethodCallExpr handleDecision(RuleSetNode.RuleType.Decision ruleType) {
    StringLiteralExpr namespace = new StringLiteralExpr(ruleType.getNamespace());
    StringLiteralExpr model = new StringLiteralExpr(ruleType.getModel());
    Expression decision = ruleType.getDecision() == null ? new NullLiteralExpr() : new StringLiteralExpr(ruleType.getDecision());
    // app.get(org.kie.kogito.decision.DecisionModels.class).getDecisionModel(namespace, model)
    MethodCallExpr decisionModels = new MethodCallExpr(new NameExpr("app"), "get").addArgument(new ClassExpr().setType(DecisionModels.class.getCanonicalName()));
    MethodCallExpr decisionModel = new MethodCallExpr(decisionModels, "getDecisionModel").addArgument(namespace).addArgument(model);
    BlockStmt actionBody = new BlockStmt();
    LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), "()"), actionBody);
    actionBody.addStatement(new ReturnStmt(decisionModel));
    return new MethodCallExpr(METHOD_DECISION).addArgument(namespace).addArgument(model).addArgument(decision).addArgument(lambda);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 25 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class RuleSetNodeVisitor method handleRuleFlowGroup.

private MethodCallExpr handleRuleFlowGroup(RuleSetNode.RuleType ruleType) {
    // build supplier for rule runtime
    BlockStmt actionBody = new BlockStmt();
    LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), "()"), actionBody);
    // app.config().get(org.kie.kogito.rules.RuleConfig.class)
    MethodCallExpr ruleConfig = new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "get").addArgument(new ClassExpr().setType(RuleConfig.class.getCanonicalName()));
    VariableDeclarationExpr configVar = new VariableDeclarationExpr(new ClassOrInterfaceType(null, RuleConfig.class.getCanonicalName()), "ruleConfig");
    actionBody.addStatement(new AssignExpr(configVar, ruleConfig, AssignExpr.Operator.ASSIGN));
    MethodCallExpr ruleRuntimeSupplier = new MethodCallExpr(new NameExpr("org.drools.project.model.ProjectRuntime.INSTANCE"), "newKieSession", NodeList.nodeList(new StringLiteralExpr("defaultStatelessKieSession")));
    VariableDeclarationExpr sessionVar = new VariableDeclarationExpr(new ClassOrInterfaceType(null, KieSession.class.getCanonicalName()), "ksession");
    actionBody.addStatement(new AssignExpr(sessionVar, ruleRuntimeSupplier, AssignExpr.Operator.ASSIGN));
    actionBody.addStatement(new JavaParser().parseStatement("ruleConfig.ruleEventListeners().agendaListeners().forEach(ksession::addEventListener);").getResult().get());
    actionBody.addStatement(new JavaParser().parseStatement("ruleConfig.ruleEventListeners().ruleRuntimeListeners().forEach(ksession::addEventListener);").getResult().get());
    actionBody.addStatement(new ReturnStmt("ksession"));
    return new MethodCallExpr("ruleFlowGroup").addArgument(new StringLiteralExpr(ruleType.getName())).addArgument(lambda);
}
Also used : JavaParser(com.github.javaparser.JavaParser) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) Parameter(com.github.javaparser.ast.body.Parameter) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)70 Parameter (com.github.javaparser.ast.body.Parameter)37 NameExpr (com.github.javaparser.ast.expr.NameExpr)30 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)29 Expression (com.github.javaparser.ast.expr.Expression)27 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)25 UnknownType (com.github.javaparser.ast.type.UnknownType)24 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)19 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)15 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)12 CompilationUnit (com.github.javaparser.ast.CompilationUnit)10 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 Test (org.junit.jupiter.api.Test)10 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)9 CastExpr (com.github.javaparser.ast.expr.CastExpr)8 ArrayList (java.util.ArrayList)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8