How to add modifiers to methodspec in javapoet?

420 views Asked by At

I try to add some modifiers to a methodspec but i'm stuck at the parameters. The parameters are from the type Modifier. The tutorial says that you can just pass Modifier.PUBLICbut Modifier.PUBLIC is an Integer value. Am i missing something here? This is my (equal to the tutorial on github) code:

public void generateCode(){
    MethodSpec main = MethodSpec.methodBuilder("main")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(void.class)
            .addParameter(String[].class, "args")
            .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
            .build();
}
1

There are 1 answers

1
Hugues M. On BEST ANSWER

You are passing java.lang.reflect.Modifier.PUBLIC, which is indeed an int, but JavaPoet wants a javax.lang.model.element.Modifier, which is an enum.