use of com.github.javaparser.ast.NodeList in project checker-framework by typetools.
the class WholeProgramInferenceJavaParserStorage method addExplicitReceiver.
/**
* Adds an explicit receiver type to a JavaParser method declaration.
*
* @param methodDeclaration declaration to add a receiver to
*/
private static void addExplicitReceiver(MethodDeclaration methodDeclaration) {
if (methodDeclaration.getReceiverParameter().isPresent()) {
return;
}
com.github.javaparser.ast.Node parent = methodDeclaration.getParentNode().get();
if (!(parent instanceof TypeDeclaration)) {
return;
}
TypeDeclaration<?> parentDecl = (TypeDeclaration<?>) parent;
ClassOrInterfaceType receiver = new ClassOrInterfaceType();
receiver.setName(parentDecl.getName());
if (parentDecl.isClassOrInterfaceDeclaration()) {
ClassOrInterfaceDeclaration parentClassDecl = parentDecl.asClassOrInterfaceDeclaration();
if (!parentClassDecl.getTypeParameters().isEmpty()) {
NodeList<Type> typeArgs = new NodeList<>();
for (TypeParameter typeParam : parentClassDecl.getTypeParameters()) {
ClassOrInterfaceType typeArg = new ClassOrInterfaceType();
typeArg.setName(typeParam.getNameAsString());
typeArgs.add(typeArg);
}
receiver.setTypeArguments(typeArgs);
}
}
methodDeclaration.setReceiverParameter(new ReceiverParameter(receiver, "this"));
}
use of com.github.javaparser.ast.NodeList in project checker-framework by typetools.
the class AnnotationMirrorToAnnotationExprConversion method convertAnnotationValues.
/**
* Converts a mapping of (annotation element → value) into a list of key-value pairs
* containing the JavaParser representations of the same values.
*
* @param values mapping of element values from an {@code AnnotationMirror}
* @return a list of the key-value pairs in {@code values} converted to their JavaParser
* representations
*/
private static NodeList<MemberValuePair> convertAnnotationValues(Map<? extends ExecutableElement, ? extends AnnotationValue> values) {
NodeList<MemberValuePair> convertedValues = new NodeList<>();
AnnotationValueConverterVisitor converter = new AnnotationValueConverterVisitor();
for (ExecutableElement valueName : values.keySet()) {
AnnotationValue value = values.get(valueName);
convertedValues.add(new MemberValuePair(valueName.getSimpleName().toString(), value.accept(converter, null)));
}
return convertedValues;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class SetNodeReferenceHandler method emitCode.
public void emitCode(StringBuilder builder) {
List<MethodDeclaration> allMethods = new ArrayList<>();
MethodDeclaration methodDeclaration = new MethodDeclaration(nodeList(Modifier.protectedModifier()), METHOD_NAME, new VoidType(), nodeParameter());
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> subNodes = partitionedNodes.get(i);
MethodDeclaration m = generateSwitchForSubNodes(i, subNodes, setNetworkNodeReference);
allMethods.add(m);
}
for (MethodDeclaration md : allMethods) {
builder.append(md.toString());
builder.append("\n");
}
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class SetNodeReferenceHandler method generateSwitchBody.
private void generateSwitchBody(BlockStmt switchBodyStatements, List<NetworkNode> subNodes) {
SwitchStmt switchStmt = new SwitchStmt();
switchStmt.setSelector(parseExpression("node.getId()"));
NodeList<SwitchEntry> entries = new NodeList<>();
for (NetworkNode n : subNodes) {
String assignStatementString;
if (n instanceof AlphaNode) {
assignStatementString = getVariableAssignmentStatementAlphaNode((AlphaNode) n);
} else {
assignStatementString = getVariableAssignmentStatement(n);
}
Statement assignStmt = parseStatement(assignStatementString);
SwitchEntry se = new SwitchEntry(nodeList(new IntegerLiteralExpr(n.getId())), SwitchEntry.Type.STATEMENT_GROUP, nodeList(assignStmt, new ReturnStmt(new BooleanLiteralExpr(true))));
entries.add(se);
}
switchStmt.setEntries(entries);
switchBodyStatements.addStatement(switchStmt);
switchBodyStatements.addStatement(new ReturnStmt(new BooleanLiteralExpr(false)));
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class SetNodeReferenceHandler method generateSwitchForSubNodes.
private MethodDeclaration generateSwitchForSubNodes(int partitionIndex, List<NetworkNode> subNodes, BlockStmt setNetworkNodeReferenceBody) {
String switchMethodName = "setNetworkNode" + partitionIndex;
BlockStmt callSwitchStatements = StaticJavaParser.parseBlock(switchStatementCall);
callSwitchStatements.findAll(SimpleName.class, ne -> ne.toString().equals("setNetworkResultN")).forEach(n -> n.replace(new SimpleName("setNetworkResult" + partitionIndex)));
callSwitchStatements.findAll(MethodCallExpr.class, mc -> mc.getNameAsString().equals("setNetworkNodeN")).forEach(n -> n.setName(new SimpleName("setNetworkNode" + partitionIndex)));
callSwitchStatements.getStatements().forEach(setNetworkNodeReferenceBody::addStatement);
MethodDeclaration switchMethod = new MethodDeclaration(nodeList(Modifier.privateModifier()), switchMethodName, PrimitiveType.booleanType(), nodeParameter());
BlockStmt switchBodyStatements = switchMethod.getBody().orElseThrow(() -> new RuntimeException("No"));
generateSwitchBody(switchBodyStatements, subNodes);
return switchMethod;
}
Aggregations