use of com.github.javaparser.ast.type.VoidType in project javaparser by javaparser.
the class ManipulationSteps method whenAStaticMethodCalledReturningIsAddedToClassInTheCompilationUnit.
@When("a public static method called \"$methodName\" returning void is added to class $position in the compilation unit")
public void whenAStaticMethodCalledReturningIsAddedToClassInTheCompilationUnit(String methodName, int position) {
CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
TypeDeclaration<?> type = compilationUnit.getType(position - 1);
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration method = new MethodDeclaration(modifiers, new VoidType(), methodName);
modifiers.add(Modifier.STATIC);
method.setModifiers(modifiers);
type.addMember(method);
state.put("cu1", compilationUnit);
}
use of com.github.javaparser.ast.type.VoidType 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;
}
Aggregations