Java Code Examples for com.github.javaparser.ast.body.MethodDeclaration#setBody()
The following examples show how to use
com.github.javaparser.ast.body.MethodDeclaration#setBody() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodDeclarationMerger.java From dolphin with Apache License 2.0 | 6 votes |
@Override public MethodDeclaration doMerge(MethodDeclaration first, MethodDeclaration second) { MethodDeclaration md = new MethodDeclaration(); md.setName(first.getName()); md.setType(mergeSingle(first.getType(), second.getType())); md.setJavaDoc(mergeSingle(first.getJavaDoc(), second.getJavaDoc())); md.setModifiers(mergeModifiers(first.getModifiers(), second.getModifiers())); md.setDefault(first.isDefault() || second.isDefault()); md.setArrayCount(Math.max(first.getArrayCount(), second.getArrayCount())); md.setAnnotations(mergeCollections(first.getAnnotations(), second.getAnnotations())); md.setThrows(mergeListNoDuplicate(first.getThrows(), second.getThrows(), false)); md.setParameters(mergeCollectionsInOrder(first.getParameters(), second.getParameters())); md.setTypeParameters(mergeCollectionsInOrder(first.getTypeParameters(), second.getTypeParameters())); md.setBody(mergeSingle(first.getBody(), second.getBody())); return md; }
Example 2
Source File: MessageProducerGenerator.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public String generate() { CompilationUnit clazz = parse( this.getClass().getResourceAsStream("/class-templates/MessageProducerTemplate.java")); clazz.setPackageDeclaration(process.getPackageName()); ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).get(); template.setName(resourceClazzName); template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, trigger.getDataType())); template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("produce")).forEach(md -> md.getParameters().stream().filter(p -> p.getNameAsString().equals(EVENT_DATA_VAR)).forEach(p -> p.setType(trigger.getDataType()))); template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("configure")).forEach(md -> md.addAnnotation("javax.annotation.PostConstruct")); template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("marshall")).forEach(md -> { md.getParameters().stream().filter(p -> p.getNameAsString().equals(EVENT_DATA_VAR)).forEach(p -> p.setType(trigger.getDataType())); md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataEventType$", messageDataEventClassName))); }); if (useInjection()) { annotator.withApplicationComponent(template); FieldDeclaration emitterField = template.findFirst(FieldDeclaration.class).filter(fd -> fd.getVariable(0).getNameAsString().equals("emitter")).get(); annotator.withInjection(emitterField); annotator.withOutgoingMessage(emitterField, trigger.getName()); emitterField.getVariable(0).setType(annotator.emitterType("String")); MethodDeclaration produceMethod = template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("produce")).findFirst().orElseThrow(() -> new IllegalStateException("Cannot find produce methos in MessageProducerTemplate")); BlockStmt body = new BlockStmt(); MethodCallExpr sendMethodCall = new MethodCallExpr(new NameExpr("emitter"), "send"); annotator.withMessageProducer(sendMethodCall, trigger.getName(), new MethodCallExpr(new ThisExpr(), "marshall").addArgument(new NameExpr("pi")).addArgument(new NameExpr(EVENT_DATA_VAR))); body.addStatement(sendMethodCall); produceMethod.setBody(body); template.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("useCloudEvents")).forEach(fd -> annotator.withConfigInjection(fd, "kogito.messaging.as-cloudevents")); } template.getMembers().sort(new BodyDeclarationComparator()); return clazz.toString(); }
Example 3
Source File: QueryEndpointGenerator.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private void addMonitoringToResource(CompilationUnit cu, MethodDeclaration[] methods, String nameURL) { cu.addImport(new ImportDeclaration(new Name("org.kie.kogito.monitoring.system.metrics.SystemMetricsCollector"), false, false)); for (MethodDeclaration md : methods) { BlockStmt body = md.getBody().orElseThrow(() -> new NoSuchElementException("A method declaration doesn't contain a body!")); NodeList<Statement> statements = body.getStatements(); ReturnStmt returnStmt = body.findFirst(ReturnStmt.class).orElseThrow(() -> new NoSuchElementException("A method declaration doesn't contain a return statement!")); statements.addFirst(parseStatement("double startTime = System.nanoTime();")); statements.addBefore(parseStatement("double endTime = System.nanoTime();"), returnStmt); statements.addBefore(parseStatement("SystemMetricsCollector.registerElapsedTimeSampleMetrics(\"" + nameURL + "\", endTime - startTime);"), returnStmt); md.setBody(wrapBodyAddingExceptionLogging(body, nameURL)); } }
Example 4
Source File: JavaparserTest.java From dolphin with Apache License 2.0 | 5 votes |
@Before public void setup(){ CompilationUnit cu = new CompilationUnit(); // set the package cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test"))); // create the type declaration ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass"); ASTHelper.addTypeDeclaration(cu, type); // create a method MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main"); method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC)); ASTHelper.addMember(type, method); // add a parameter to the method Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args"); param.setVarArgs(true); ASTHelper.addParameter(method, param); // add a body to the method BlockStmt block = new BlockStmt(); method.setBody(block); // add a statement do the method body NameExpr clazz = new NameExpr("System"); FieldAccessExpr field = new FieldAccessExpr(clazz, "out"); MethodCallExpr call = new MethodCallExpr(field, "println"); ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!")); ASTHelper.addStmt(block, call); unit = cu; }
Example 5
Source File: KieModuleModelMethod.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public String toGetKieModuleModelMethod() { MethodDeclaration methodDeclaration = new MethodDeclaration(nodeList(publicModifier()), new ClassOrInterfaceType(null, kieModuleModelCanonicalName), "getKieModuleModel"); methodDeclaration.setBody(stmt); methodDeclaration.addAnnotation( "Override" ); return methodDeclaration.toString(); }
Example 6
Source File: ProcessVisitor.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public void visitProcess(WorkflowProcess process, MethodDeclaration processMethod, ProcessMetaData metadata) { BlockStmt body = new BlockStmt(); ClassOrInterfaceType processFactoryType = new ClassOrInterfaceType(null, RuleFlowProcessFactory.class.getSimpleName()); // create local variable factory and assign new fluent process to it VariableDeclarationExpr factoryField = new VariableDeclarationExpr(processFactoryType, FACTORY_FIELD_NAME); MethodCallExpr assignFactoryMethod = new MethodCallExpr(new NameExpr(processFactoryType.getName().asString()), "createProcess"); assignFactoryMethod.addArgument(new StringLiteralExpr(process.getId())); body.addStatement(new AssignExpr(factoryField, assignFactoryMethod, AssignExpr.Operator.ASSIGN)); // item definitions Set<String> visitedVariables = new HashSet<>(); VariableScope variableScope = (VariableScope) ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE); visitVariableScope(variableScope, body, visitedVariables); visitSubVariableScopes(process.getNodes(), body, visitedVariables); visitInterfaces(process.getNodes(), body); // the process itself body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_NAME, new StringLiteralExpr(process.getName()))) .addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_PACKAGE_NAME, new StringLiteralExpr(process.getPackageName()))) .addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_DYNAMIC, new BooleanLiteralExpr(((org.jbpm.workflow.core.WorkflowProcess) process).isDynamic()))) .addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VERSION, new StringLiteralExpr(getOrDefault(process.getVersion(), DEFAULT_VERSION)))) .addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VISIBILITY, new StringLiteralExpr(getOrDefault(process.getVisibility(), WorkflowProcess.PUBLIC_VISIBILITY)))); visitMetaData(process.getMetaData(), body, FACTORY_FIELD_NAME); visitHeader(process, body); List<Node> processNodes = new ArrayList<>(); for (org.kie.api.definition.process.Node procNode : process.getNodes()) { processNodes.add((org.jbpm.workflow.core.Node) procNode); } visitNodes(processNodes, body, variableScope, metadata); visitConnections(process.getNodes(), body); body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VALIDATE)); MethodCallExpr getProcessMethod = new MethodCallExpr(new NameExpr(FACTORY_FIELD_NAME), "getProcess"); body.addStatement(new ReturnStmt(getProcessMethod)); processMethod.setBody(body); }
Example 7
Source File: JavaInterfaceBuilderImpl.java From chrome-devtools-java-client with Apache License 2.0 | 4 votes |
@Override public void addMethod( String name, String description, List<MethodParam> methodParams, String returnType) { MethodDeclaration methodDeclaration = declaration.addMethod(name); methodDeclaration.setBody(null); if (StringUtils.isNotEmpty(description)) { methodDeclaration.setJavadocComment( JavadocUtils.createJavadocComment(description, INDENTATION_TAB)); } if (StringUtils.isNotEmpty(returnType)) { methodDeclaration.setType(returnType); } if (CollectionUtils.isNotEmpty(methodParams)) { for (MethodParam methodParam : methodParams) { Parameter parameter = methodDeclaration.addAndGetParameter(methodParam.getType(), methodParam.getName()); if (CollectionUtils.isNotEmpty(methodParam.getAnnotations())) { for (MethodParam.Annotation annotation : methodParam.getAnnotations()) { Optional<AnnotationExpr> currentAnnotation = parameter.getAnnotationByName(annotation.getName()); if (!currentAnnotation.isPresent()) { if (StringUtils.isNotEmpty(annotation.getValue())) { parameter.addSingleMemberAnnotation( annotation.getName(), new StringLiteralExpr(annotation.getValue())); } else { parameter.addMarkerAnnotation(annotation.getName()); } if (!DEPRECATED_ANNOTATION.equals(annotation.getName())) { addImport(annotationsPackage, annotation.getName()); } } } } } } }