use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class InlineFieldReferenceInitHandler method generateInitMethodForPartition.
private MethodDeclaration generateInitMethodForPartition(int partitionIndex, List<NetworkNode> nodeInPartition, BlockStmt setNetworkNodeReferenceBody) {
String initWithIndex = "initNode" + partitionIndex;
CompilationUnit initialisationCompilationUnit = new CompilationUnit();
initialisationCompilationUnit.setPackageDeclaration(ObjectTypeNodeCompiler.PACKAGE_NAME);
initialisationCompilationUnit.addClass(ucFirst(initWithIndex));
partitionedNodeInitialisationClasses.put(partitionIndex, initialisationCompilationUnit);
BlockStmt setFieldStatementCall = StaticJavaParser.parseBlock(statementCall);
setFieldStatementCall.findAll(MethodCallExpr.class, mc -> mc.getNameAsString().equals("initNodeN")).forEach(n -> n.setName(new SimpleName(initWithIndex)));
setFieldStatementCall.getStatements().forEach(setNetworkNodeReferenceBody::addStatement);
MethodDeclaration initMethodWithIndex = new MethodDeclaration(nodeList(Modifier.publicModifier()), new VoidType(), initWithIndex);
BlockStmt initBlockPerPartition = initMethodWithIndex.getBody().orElseThrow(() -> new RuntimeException("No"));
generateInitBody(initBlockPerPartition, nodeInPartition, partitionIndex);
return initMethodWithIndex;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class InlineFieldReferenceInitHandler method emitCode.
public void emitCode(StringBuilder builder) {
List<MethodDeclaration> allMethods = new ArrayList<>();
MethodDeclaration methodDeclaration = new MethodDeclaration(nodeList(Modifier.publicModifier()), new VoidType(), METHOD_NAME);
allMethods.add(methodDeclaration);
BlockStmt setNetworkNodeReference = methodDeclaration.getBody().orElseThrow(() -> new RuntimeException("No block statement"));
List<List<NetworkNode>> partitionedNodes = ListUtils.partition(nodes, 20);
for (int i = 0; i < partitionedNodes.size(); i++) {
List<NetworkNode> nodesInPartition = partitionedNodes.get(i);
MethodDeclaration m = generateInitMethodForPartition(i, nodesInPartition, setNetworkNodeReference);
allMethods.add(m);
}
for (MethodDeclaration md : allMethods) {
builder.append(md.toString());
builder.append("\n");
}
}
use of com.github.javaparser.ast.NodeList in project flow by vaadin.
the class GeneratorUtilsTest method should_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationAndTheImport.
@Test
public void should_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationAndTheImport() {
NodeWithAnnotations<?> declarationWithEndpointAnnotation = Mockito.mock(NodeWithAnnotations.class);
CompilationUnit compilationUnitWithVaadinEndpointImport = Mockito.mock(CompilationUnit.class);
AnnotationExpr endpointAnnotation = Mockito.mock(AnnotationExpr.class);
Mockito.doReturn(Optional.of(endpointAnnotation)).when(declarationWithEndpointAnnotation).getAnnotationByClass(Endpoint.class);
NodeList<ImportDeclaration> imports = new NodeList<>();
ImportDeclaration importDeclaration = Mockito.mock(ImportDeclaration.class);
Mockito.doReturn(Endpoint.class.getName()).when(importDeclaration).getNameAsString();
imports.add(importDeclaration);
Mockito.doReturn(imports).when(compilationUnitWithVaadinEndpointImport).getImports();
Assert.assertTrue("A class with a Vaadin Endpoint should be considered as an Endpoint", GeneratorUtils.hasAnnotation(declarationWithEndpointAnnotation, compilationUnitWithVaadinEndpointImport, Endpoint.class));
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ListReplacementChange method getValue.
@Override
public Object getValue(ObservableProperty property, Node node) {
if (property == observableProperty) {
NodeList nodeList = new NodeList();
Object currentRawValue = new NoChange().getValue(property, node);
if (currentRawValue instanceof Optional) {
Optional optional = (Optional) currentRawValue;
currentRawValue = optional.orElseGet(null);
}
if (!(currentRawValue instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + currentRawValue.getClass().getCanonicalName());
}
NodeList currentNodeList = (NodeList) currentRawValue;
nodeList.addAll(currentNodeList);
nodeList.set(index, newValue);
return nodeList;
} else {
return new NoChange().getValue(property, node);
}
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class LexicalDifferenceCalculator method calculatedSyntaxModelForNode.
private void calculatedSyntaxModelForNode(CsmElement csm, Node node, List<CsmElement> elements, Change change) {
if (csm instanceof CsmSequence) {
CsmSequence csmSequence = (CsmSequence) csm;
csmSequence.getElements().forEach(e -> calculatedSyntaxModelForNode(e, node, elements, change));
} else if (csm instanceof CsmComment) {
// nothing to do
} else if (csm instanceof CsmSingleReference) {
CsmSingleReference csmSingleReference = (CsmSingleReference) csm;
Node child;
if (change instanceof PropertyChange && ((PropertyChange) change).getProperty() == csmSingleReference.getProperty()) {
child = (Node) ((PropertyChange) change).getNewValue();
} else {
child = csmSingleReference.getProperty().getValueAsSingleReference(node);
}
if (child != null) {
elements.add(new CsmChild(child));
}
} else if (csm instanceof CsmNone) {
// nothing to do
} else if (csm instanceof CsmToken) {
elements.add(csm);
} else if (csm instanceof CsmOrphanCommentsEnding) {
// nothing to do
} else if (csm instanceof CsmList) {
CsmList csmList = (CsmList) csm;
if (csmList.getProperty().isAboutNodes()) {
Object rawValue = change.getValue(csmList.getProperty(), node);
NodeList nodeList;
if (rawValue instanceof Optional) {
Optional optional = (Optional) rawValue;
if (optional.isPresent()) {
if (!(optional.get() instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + optional.get().getClass().getCanonicalName());
}
nodeList = (NodeList) optional.get();
} else {
nodeList = new NodeList();
}
} else {
if (!(rawValue instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + rawValue.getClass().getCanonicalName());
}
nodeList = (NodeList) rawValue;
}
if (!nodeList.isEmpty()) {
calculatedSyntaxModelForNode(csmList.getPreceeding(), node, elements, change);
for (int i = 0; i < nodeList.size(); i++) {
if (i != 0) {
calculatedSyntaxModelForNode(csmList.getSeparatorPre(), node, elements, change);
}
elements.add(new CsmChild(nodeList.get(i)));
if (i != (nodeList.size() - 1)) {
calculatedSyntaxModelForNode(csmList.getSeparatorPost(), node, elements, change);
}
}
calculatedSyntaxModelForNode(csmList.getFollowing(), node, elements, change);
}
} else {
Collection collection = (Collection) change.getValue(csmList.getProperty(), node);
if (!collection.isEmpty()) {
calculatedSyntaxModelForNode(csmList.getPreceeding(), node, elements, change);
boolean first = true;
for (Iterator it = collection.iterator(); it.hasNext(); ) {
if (!first) {
calculatedSyntaxModelForNode(csmList.getSeparatorPre(), node, elements, change);
}
Object value = it.next();
if (value instanceof Modifier) {
Modifier modifier = (Modifier) value;
elements.add(new CsmToken(toToken(modifier)));
} else {
throw new UnsupportedOperationException(it.next().getClass().getSimpleName());
}
if (it.hasNext()) {
calculatedSyntaxModelForNode(csmList.getSeparatorPost(), node, elements, change);
}
first = false;
}
calculatedSyntaxModelForNode(csmList.getFollowing(), node, elements, change);
}
}
} else if (csm instanceof CsmConditional) {
CsmConditional csmConditional = (CsmConditional) csm;
boolean satisfied = change.evaluate(csmConditional, node);
if (satisfied) {
calculatedSyntaxModelForNode(csmConditional.getThenElement(), node, elements, change);
} else {
calculatedSyntaxModelForNode(csmConditional.getElseElement(), node, elements, change);
}
} else if (csm instanceof CsmIndent) {
elements.add(csm);
} else if (csm instanceof CsmUnindent) {
elements.add(csm);
} else if (csm instanceof CsmAttribute) {
CsmAttribute csmAttribute = (CsmAttribute) csm;
Object value = change.getValue(csmAttribute.getProperty(), node);
String text = value.toString();
if (value instanceof Printable) {
text = ((Printable) value).asString();
}
elements.add(new CsmToken(csmAttribute.getTokenType(node, value.toString()), text));
} else if ((csm instanceof CsmString) && (node instanceof StringLiteralExpr)) {
elements.add(new CsmToken(GeneratedJavaParserConstants.STRING_LITERAL, "\"" + ((StringLiteralExpr) node).getValue() + "\""));
} else if (csm instanceof CsmMix) {
CsmMix csmMix = (CsmMix) csm;
List<CsmElement> mixElements = new LinkedList<>();
csmMix.getElements().forEach(e -> calculatedSyntaxModelForNode(e, node, mixElements, change));
elements.add(new CsmMix(mixElements));
} else {
throw new UnsupportedOperationException(csm.getClass().getSimpleName() + " " + csm);
}
}
Aggregations