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 (getParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (getParentNode(wrappedNode) instanceof LambdaExpr) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (getParentNode(wrappedNode) instanceof IfStmt) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (!(getParentNode(wrappedNode) instanceof NodeWithStatements)) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) getParentNode(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);
}
use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.
the class StatementContext method solveSymbol.
@Override
public SymbolReference<? extends ValueDeclaration> 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 ValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
if (symbolReference.isSolved()) {
return symbolReference;
}
// we should look in all the statements preceding, treating them as SymbolDeclarators
if (getParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
return getParent().solveSymbol(name, typeSolver);
}
if (getParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.ConstructorDeclaration) {
return getParent().solveSymbol(name, typeSolver);
}
if (getParentNode(wrappedNode) instanceof LambdaExpr) {
return getParent().solveSymbol(name, typeSolver);
}
if (!(getParentNode(wrappedNode) instanceof NodeWithStatements)) {
return getParent().solveSymbol(name, typeSolver);
}
NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) getParentNode(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);
}
use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.
the class JavaParserSymbolDeclaration method getType.
@Override
public Type getType() {
if (wrappedNode instanceof Parameter) {
Parameter parameter = (Parameter) wrappedNode;
if (getParentNode(wrappedNode) instanceof LambdaExpr) {
int pos = getParamPos(parameter);
Type lambdaType = JavaParserFacade.get(typeSolver).getType(getParentNode(wrappedNode));
// MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
} else {
Type rawType = null;
if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
rawType = PrimitiveType.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 ArrayType(rawType);
} else {
return rawType;
}
}
} else if (wrappedNode instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
if (getParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
Type type = JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
return type;
} else if (getParentNode(wrappedNode) instanceof FieldDeclaration) {
return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
} else {
throw new UnsupportedOperationException(getParentNode(wrappedNode).getClass().getCanonicalName());
}
} else {
throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
}
use of com.github.javaparser.ast.expr.LambdaExpr in project javaparser by javaparser.
the class ContextTest method parametersExposedToChildForLambda.
@Test
void parametersExposedToChildForLambda() {
LambdaExpr lambda = (LambdaExpr) parse("Object myLambda = (myParam) -> myParam * 2;", ParseStart.STATEMENT).asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariables().get(0).getInitializer().get();
assertOneParamExposedToChildInContextNamed(lambda, lambda.getBody(), "myParam");
assertNoParamsExposedToChildInContextNamed(lambda, lambda.getParameter(0), "myParam");
}
use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.
the class ProcessGenerator method internalConfigure.
private MethodDeclaration internalConfigure(ProcessMetaData processMetaData) {
BlockStmt body = new BlockStmt();
MethodDeclaration internalConfigure = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(targetTypeName).setName("configure").setBody(body);
// always call super.configure
body.addStatement(new MethodCallExpr(new SuperExpr(), "configure"));
if (!processMetaData.getGeneratedHandlers().isEmpty()) {
processMetaData.getGeneratedHandlers().forEach((name, descriptor) -> {
CompilationUnit handler = descriptor.generateHandlerClassForService();
ClassOrInterfaceDeclaration clazz = handler.findFirst(ClassOrInterfaceDeclaration.class).get();
if (useInjection()) {
boolean tracingAvailable = context.getBuildContext().hasClassAvailable("org.eclipse.microprofile.opentracing.Traced");
if (tracingAvailable) {
FieldDeclaration tracerField = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "io.automatiko.engine.service.tracing.TracingAdds"), "tracer"));
annotator.withInjection(tracerField);
clazz.addMember(tracerField);
clazz.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("executeWorkItem")).forEach(md -> {
// add Traced nnotation on method level
md.addAnnotation("org.eclipse.microprofile.opentracing.Traced");
// next update method body to include extra tags
BlockStmt mbody = md.getBody().get();
MethodCallExpr tracer = new MethodCallExpr(new NameExpr("tracer"), "addTags").addArgument(new MethodCallExpr(new NameExpr("workItem"), "getProcessInstance"));
BlockStmt updatedBody = new BlockStmt();
updatedBody.addStatement(tracer);
mbody.getStatements().forEach(s -> updatedBody.addStatement(s));
md.setBody(updatedBody);
});
}
boolean faultToleranceAvailable = context.getBuildContext().hasClassAvailable("io.automatiko.addons.fault.tolerance.CircuitClosedEvent");
if (faultToleranceAvailable) {
Boolean disabled = Boolean.parseBoolean(descriptor.metadata("faultToleranceDisabled", "false").toString());
if (!disabled) {
Long timeout = Long.valueOf(descriptor.metadata("timeout", "-1").toString());
Long delay = Long.valueOf(descriptor.metadata("delay", "5000").toString());
Integer threshold = Integer.valueOf(descriptor.metadata("requestThreshold", "20").toString());
Double ratio = Double.valueOf(descriptor.metadata("failureRatio", ".50").toString());
NormalAnnotationExpr circuitBrakerAnnotation = new NormalAnnotationExpr(new Name("org.eclipse.microprofile.faulttolerance.CircuitBreaker"), NodeList.nodeList(new MemberValuePair("delay", new LongLiteralExpr(delay)), new MemberValuePair("requestVolumeThreshold", new IntegerLiteralExpr(threshold)), new MemberValuePair("failureRatio", new DoubleLiteralExpr(ratio)), new MemberValuePair("skipOn", new NameExpr("io.automatiko.engine.api.workflow.HandledServiceExecutionError.class"))));
clazz.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("executeWorkItem")).forEach(md -> {
// add CircuitBreaker annotation on method level
md.addAnnotation(circuitBrakerAnnotation);
md.addSingleMemberAnnotation("io.smallrye.faulttolerance.api.CircuitBreakerName", new StringLiteralExpr(name));
if (timeout > 0) {
md.addSingleMemberAnnotation("org.eclipse.microprofile.faulttolerance.Timeout", new LongLiteralExpr(timeout));
}
});
context.setApplicationProperty("quarkus.arc.selected-alternatives", context.getApplicationProperty("quarkus.arc.selected-alternatives").filter(s -> !s.contains("io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache")).map(s -> s + ",io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache").orElse("io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache"));
}
}
annotator.withApplicationComponent(clazz);
} else {
String packageName = handler.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = clazz.getName().toString();
MethodCallExpr workItemManager = new MethodCallExpr(new NameExpr("services"), "getWorkItemManager");
MethodCallExpr registerHandler = new MethodCallExpr(workItemManager, "registerWorkItemHandler").addArgument(new StringLiteralExpr(name)).addArgument(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, packageName + "." + clazzName), NodeList.nodeList()));
body.addStatement(registerHandler);
}
// annotate for injection or add constructor for initialization
handler.findAll(FieldDeclaration.class, fd -> !fd.getVariable(0).getNameAsString().equals("tracer")).forEach(fd -> {
if (useInjection()) {
annotator.withInjection(fd);
}
if (descriptor.implementation().equalsIgnoreCase("##webservice") && !fd.getVariable(0).getNameAsString().equals("completionHandler") && annotator != null) {
annotator.withRestClientInjection(fd);
} else if (!descriptor.implementation().equalsIgnoreCase("##webservice") && !fd.getVariable(0).getNameAsString().equals("completionHandler")) {
BlockStmt constructorBody = new BlockStmt();
AssignExpr assignExpr = new AssignExpr(new FieldAccessExpr(new ThisExpr(), fd.getVariable(0).getNameAsString()), new ObjectCreationExpr().setType(fd.getVariable(0).getType().toString()), AssignExpr.Operator.ASSIGN);
constructorBody.addStatement(assignExpr);
clazz.addConstructor(Keyword.PUBLIC).setBody(constructorBody);
}
});
additionalClasses.add(handler);
});
if (useInjection()) {
BlockStmt actionBody = new BlockStmt();
LambdaExpr forachBody = new LambdaExpr(new Parameter(new UnknownType(), "h"), actionBody);
MethodCallExpr forachHandler = new MethodCallExpr(new NameExpr("handlers"), "forEach");
forachHandler.addArgument(forachBody);
MethodCallExpr workItemManager = new MethodCallExpr(new NameExpr("services"), "getWorkItemManager");
MethodCallExpr registerHandler = new MethodCallExpr(workItemManager, "registerWorkItemHandler").addArgument(new MethodCallExpr(new NameExpr("h"), "getName")).addArgument(new NameExpr("h"));
actionBody.addStatement(registerHandler);
body.addStatement(forachHandler);
}
}
if (!processMetaData.getGeneratedListeners().isEmpty()) {
processMetaData.getGeneratedListeners().forEach(listener -> {
ClassOrInterfaceDeclaration clazz = listener.findFirst(ClassOrInterfaceDeclaration.class).get();
String packageName = listener.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
String clazzName = clazz.getName().toString();
MethodCallExpr eventSupport = new MethodCallExpr(new NameExpr("services"), "getEventSupport");
MethodCallExpr registerListener = new MethodCallExpr(eventSupport, "addEventListener").addArgument(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, packageName + "." + clazzName), NodeList.nodeList()));
body.addStatement(registerListener);
additionalClasses.add(listener);
});
}
body.addStatement(new ReturnStmt(new ThisExpr()));
return internalConfigure;
}
Aggregations