Search in sources :

Example 36 with Node

use of com.github.javaparser.ast.Node in project zaproxy by zaproxy.

the class JFlexToRstaTokenMaker method processSource.

private void processSource(FileVisitDetails source, Path outputDir) {
    if (source.isDirectory()) {
        return;
    }
    CompilationUnit compilationUnit = parseJavaSource(source.getFile());
    TypeDeclaration<?> type = compilationUnit.getType(0);
    type.addConstructor(Modifier.Keyword.PUBLIC);
    removeMethod(type, "zzRefill", "yyreset", "yyResetPosition");
    type.getFieldByName("zzBuffer").ifPresent(JFlexToRstaTokenMaker::removeInitialisation);
    type.getFieldByName("ZZ_BUFFERSIZE").ifPresent(Node::remove);
    type.getMethodsByName("yylex").forEach(method -> {
        removeThrowsIoException(method);
        method.addSingleMemberAnnotation(SuppressWarnings.class, "\"fallthrough\"");
    });
    addOverrideAnnotation(type, "yybegin", "yyclose");
    Path outputFile = outputDir.resolve(source.getRelativePath().getPathString());
    try {
        Files.createDirectories(outputFile.getParent());
        Files.write(outputFile, compilationUnit.toString().getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new BuildException("Failed to save the processed source: " + e.getMessage(), e);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) Node(com.github.javaparser.ast.Node) IOException(java.io.IOException)

Example 37 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class CommonCodegenUtils method populateListInListGetter.

/**
 * Method to be used to populate a <code>List</code> inside a getter method meant to return only that <code>List</code>
 * @param toAdd
 * @param methodDeclaration
 * @param listName
 */
public static void populateListInListGetter(final List<? extends Expression> toAdd, final MethodDeclaration methodDeclaration, final String listName) {
    final BlockStmt body = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_BODY_IN_METHOD, methodDeclaration)));
    Optional<ReturnStmt> oldReturn = body.getStatements().parallelStream().filter(statement -> statement instanceof ReturnStmt).map(ReturnStmt.class::cast).findFirst();
    oldReturn.ifPresent(Node::remove);
    toAdd.forEach(expression -> {
        NodeList<Expression> arguments = NodeList.nodeList(expression);
        MethodCallExpr methodCallExpr = new MethodCallExpr();
        methodCallExpr.setScope(new NameExpr(listName));
        methodCallExpr.setName("add");
        methodCallExpr.setArguments(arguments);
        ExpressionStmt expressionStmt = new ExpressionStmt();
        expressionStmt.setExpression(methodCallExpr);
        body.addStatement(expressionStmt);
    });
    body.addStatement(getReturnStmt(listName));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 38 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class CommonCodegenUtilsTest method addMapPopulationExpression.

@Test
public void addMapPopulationExpression() {
    Map<String, Expression> inputMap = new HashMap<>();
    inputMap.put("one", new StringLiteralExpr("ONE"));
    inputMap.put("two", new IntegerLiteralExpr("2"));
    inputMap.put("three", new DoubleLiteralExpr("3.0"));
    BlockStmt inputBody = new BlockStmt();
    String inputMapName = "testMap";
    CommonCodegenUtils.addMapPopulationExpressions(inputMap, inputBody, inputMapName);
    NodeList<Statement> statements = inputBody.getStatements();
    assertEquals(inputMap.size(), statements.size());
    List<MethodCallExpr> methodCallExprs = new ArrayList<>(statements.size());
    for (Statement statement : statements) {
        assertTrue(statement instanceof ExpressionStmt);
        Expression expression = ((ExpressionStmt) statement).getExpression();
        assertTrue(expression instanceof MethodCallExpr);
        MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
        assertEquals(inputMapName, methodCallExpr.getScope().map(Node::toString).orElse(null));
        assertEquals("put", methodCallExpr.getName().asString());
        assertSame(2, methodCallExpr.getArguments().size());
        assertTrue(methodCallExpr.getArgument(0) instanceof StringLiteralExpr);
        methodCallExprs.add(methodCallExpr);
    }
    for (Map.Entry<String, Expression> inputEntry : inputMap.entrySet()) {
        assertEquals("Expected one and only one statement for key \"" + inputEntry.getKey() + "\"", 1, methodCallExprs.stream().filter(methodCallExpr -> {
            StringLiteralExpr arg0 = (StringLiteralExpr) methodCallExpr.getArgument(0);
            return arg0.asString().equals(inputEntry.getKey()) && methodCallExpr.getArgument(1).equals(inputEntry.getValue());
        }).count());
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) HashMap(java.util.HashMap) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayList(java.util.ArrayList) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) Map(java.util.Map) HashMap(java.util.HashMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 39 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class FlattenScopeTest method flattenFields.

@Test
public void flattenFields() {
    List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("Field.INT"));
    List<Node> expected = asList(new NameExpr("Field"), new SimpleName("INT"));
    compareArrays(actual, expected);
}
Also used : Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) NameExpr(com.github.javaparser.ast.expr.NameExpr) Test(org.junit.Test)

Example 40 with Node

use of com.github.javaparser.ast.Node in project drools by kiegroup.

the class FlattenScopeTest method flattenMethodCall.

@Test
public void flattenMethodCall() {
    List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("name.startsWith(\"M\")"));
    MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("name"), "startsWith", nodeList(new StringLiteralExpr("M")));
    methodCallExpr.setTypeArguments(NodeList.nodeList());
    List<Node> expected = asList(new NameExpr("name"), methodCallExpr);
    compareArrays(actual, expected);
}
Also used : Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6