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);
}
}
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));
}
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());
}
}
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);
}
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);
}
Aggregations