use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class ClassCreator method createCU.
/**
* creates the compilation unit
*/
private static CompilationUnit createCU() {
CompilationUnit cu = new CompilationUnit();
// set the package
cu.setPackageDeclaration(new PackageDeclaration(parseName("java.parser.test")));
// or a shortcut
cu.setPackageDeclaration("java.parser.test");
// create the type declaration
ClassOrInterfaceDeclaration type = cu.addClass("GeneratedClass");
// create a method
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration method = new MethodDeclaration(modifiers, new VoidType(), "main");
modifiers.add(Modifier.STATIC);
method.setModifiers(modifiers);
type.addMember(method);
// or a shortcut
MethodDeclaration main2 = type.addMethod("main2", Modifier.PUBLIC, Modifier.STATIC);
// add a parameter to the method
Parameter param = new Parameter(parseClassOrInterfaceType("String"), "args");
param.setVarArgs(true);
method.addParameter(param);
// or a shortcut
main2.addAndGetParameter(String.class, "args").setVarArgs(true);
// add a body to the method
BlockStmt block = new BlockStmt();
method.setBody(block);
// add a statement to the method body
NameExpr clazz = new NameExpr("System");
FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
MethodCallExpr call = new MethodCallExpr(field, "println");
call.addArgument(new StringLiteralExpr("Hello World!"));
block.addStatement(call);
return cu;
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class MainConstructorGenerator method generateNode.
@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
if (nodeMetaModel.is(Node.class)) {
return;
}
ConstructorDeclaration constructor = new ConstructorDeclaration().setPublic(true).setName(nodeCoid.getNameAsString()).addParameter(TokenRange.class, "tokenRange").setJavadocComment("\n * This constructor is used by the parser and is considered private.\n ");
BlockStmt body = constructor.getBody();
SeparatedItemStringBuilder superCall = new SeparatedItemStringBuilder("super(", ", ", ");");
superCall.append("tokenRange");
for (PropertyMetaModel parameter : nodeMetaModel.getConstructorParameters()) {
constructor.addParameter(parameter.getTypeNameForSetter(), parameter.getName());
if (nodeMetaModel.getDeclaredPropertyMetaModels().contains(parameter)) {
body.addStatement(f("%s(%s);", parameter.getSetterMethodName(), parameter.getName()));
} else {
superCall.append(parameter.getName());
}
}
body.getStatements().addFirst(parseExplicitConstructorInvocationStmt(superCall.toString()));
body.addStatement("customInitialization();");
addOrReplaceWhenSameSignature(nodeCoid, constructor);
nodeCu.addImport(TokenRange.class);
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class GenericListVisitorAdapterGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
body.addStatement("List<R> result = new ArrayList<>();");
body.addStatement("List<R> tmp;");
final String resultCheck = "if (tmp != null) result.addAll(tmp);";
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
body.addStatement(f("if (n.%s.isPresent()) {" + " tmp = n.%s.get().accept(this, arg);" + " %s" + "}", getter, getter, resultCheck));
} else {
body.addStatement(f("{ tmp = n.%s.accept(this, arg); %s }", getter, resultCheck));
}
}
}
body.addStatement("return result;");
Arrays.stream(new Class<?>[] { List.class, ArrayList.class }).filter(c -> compilationUnit.getImports().stream().noneMatch(i -> c.getName().equals(i.getName().asString()))).forEach(compilationUnit::addImport);
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class HashCodeVisitorGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
final BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("return ", "* 31 +", ";");
final List<PropertyMetaModel> propertyMetaModels = node.getAllPropertyMetaModels();
if (propertyMetaModels.isEmpty()) {
builder.append("0");
} else {
for (PropertyMetaModel field : propertyMetaModels) {
final String getter = field.getGetterMethodName() + "()";
// Is this field another AST node? Visit it.
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
builder.append("(n.%s.isPresent()? n.%s.get().accept(this, arg):0)", getter, getter);
} else {
builder.append("(n.%s.accept(this, arg))", getter);
}
} else {
Class<?> type = field.getType();
if (type.equals(boolean.class)) {
builder.append("(n.%s?1:0)", getter);
} else if (type.equals(int.class)) {
builder.append("n.%s", getter);
} else {
builder.append("(n.%s.hashCode())", getter);
}
}
}
}
body.addStatement(parseStatement(builder.toString()));
}
use of com.github.javaparser.ast.stmt.BlockStmt in project javaparser by javaparser.
the class NoCommentEqualsVisitorGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
if (!(node.equals(JavaParserMetaModel.lineCommentMetaModel) || node.equals(JavaParserMetaModel.blockCommentMetaModel) || node.equals(JavaParserMetaModel.javadocCommentMetaModel))) {
body.addStatement(f("final %s n2 = (%s) arg;", node.getTypeName(), node.getTypeName()));
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.equals(JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel))
continue;
if (field.getNodeReference().isPresent()) {
if (field.isNodeList()) {
body.addStatement(f("if (!nodesEquals(n.%s, n2.%s)) return false;", getter, getter));
} else {
body.addStatement(f("if (!nodeEquals(n.%s, n2.%s)) return false;", getter, getter));
}
} else {
body.addStatement(f("if (!objEquals(n.%s, n2.%s)) return false;", getter, getter));
}
}
if (body.getStatements().size() == 1) {
// Only the cast line was added, but nothing is using it, so remove it again.
body.getStatements().clear();
}
}
body.addStatement("return true;");
}
Aggregations