Search in sources :

Example 41 with NodeList

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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) HashMap(java.util.HashMap) AlphaNode(org.drools.core.reteoo.AlphaNode) ArrayList(java.util.ArrayList) AbstractCompilerHandler.getVariableName(org.drools.ancompiler.AbstractCompilerHandler.getVariableName) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VoidType(com.github.javaparser.ast.type.VoidType) NetworkNode(org.drools.core.common.NetworkNode) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) Collectors(java.util.stream.Collectors) Modifier(com.github.javaparser.ast.Modifier) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) VoidType(com.github.javaparser.ast.type.VoidType) 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)

Example 42 with NodeList

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");
    }
}
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 43 with NodeList

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));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Endpoint(dev.hilla.Endpoint) NodeList(com.github.javaparser.ast.NodeList) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Test(org.junit.Test)

Example 44 with NodeList

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);
    }
}
Also used : Optional(java.util.Optional) NodeList(com.github.javaparser.ast.NodeList)

Example 45 with NodeList

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);
    }
}
Also used : Node(com.github.javaparser.ast.Node) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Modifier(com.github.javaparser.ast.Modifier) NodeList(com.github.javaparser.ast.NodeList) Printable(com.github.javaparser.printer.Printable)

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