Search in sources :

Example 36 with NodeList

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"));
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) Type(com.github.javaparser.ast.type.Type) TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ReceiverParameter(com.github.javaparser.ast.body.ReceiverParameter) NodeList(com.github.javaparser.ast.NodeList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 37 with NodeList

use of com.github.javaparser.ast.NodeList in project checker-framework by typetools.

the class AnnotationMirrorToAnnotationExprConversion method convertAnnotationValues.

/**
 * Converts a mapping of (annotation element &rarr; 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;
}
Also used : MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) NodeList(com.github.javaparser.ast.NodeList) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 38 with NodeList

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");
    }
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NetworkNode(org.drools.core.common.NetworkNode)

Example 39 with NodeList

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)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) NodeList(com.github.javaparser.ast.NodeList) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) NetworkNode(org.drools.core.common.NetworkNode) AlphaNode(org.drools.core.reteoo.AlphaNode) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 40 with NodeList

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;
}
Also used : SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) AlphaNode(org.drools.core.reteoo.AlphaNode) ArrayList(java.util.ArrayList) AbstractCompilerHandler.getVariableName(org.drools.ancompiler.AbstractCompilerHandler.getVariableName) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Sink(org.drools.core.reteoo.Sink) AbstractCompilerHandler.getVariableType(org.drools.ancompiler.AbstractCompilerHandler.getVariableType) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VoidType(com.github.javaparser.ast.type.VoidType) NetworkNode(org.drools.core.common.NetworkNode) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Statement(com.github.javaparser.ast.stmt.Statement) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SimpleName(com.github.javaparser.ast.expr.SimpleName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14