Search in sources :

Example 1 with VoidType

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) VoidType(com.github.javaparser.ast.type.VoidType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Modifier(com.github.javaparser.ast.Modifier) When(org.jbehave.core.annotations.When)

Example 2 with VoidType

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;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) VoidType(com.github.javaparser.ast.type.VoidType) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Parameter(com.github.javaparser.ast.body.Parameter) Modifier(com.github.javaparser.ast.Modifier) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 Modifier (com.github.javaparser.ast.Modifier)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 VoidType (com.github.javaparser.ast.type.VoidType)2 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 When (org.jbehave.core.annotations.When)1