BayMax-Yi
BayMax-Yi

Reputation: 1

How to add parameters Activity by JavaPoet?

I want to generate code like

public void inject(MainActivity activity){
  ......
}

by JavaPoet in javaLibrary ,I add the code in AbstractProcess.process(...) like:

MethodSpec methodSpec = MethodSpec.methodBuilder("inject")
                .addModifiers(Modifier.PUBLIC)

       .addParameter(Class.forName(element.getQualifiedName().toString()), "activity")
                .returns(void.class)
                //.addStatement()
                .build();

then I got the exception ClassNotFoundExeption when I build project.How shoud I addParameter MainActivity? Thank you!

Upvotes: 0

Views: 237

Answers (1)

n3k0
n3k0

Reputation: 579

Maybe you can send a String param that represents the qualified name of your class, in combo with the static method bestGuess of the ClassName class.

MethodSpec methodSpec = MethodSpec.methodBuilder("inject")
   .addModifiers(Modifier.PUBLIC)
   .addParameter(ClassName.bestGuess("qualified.name.of.your.Class"), "activity")
            .returns(void.class) //you can delete this, javapoet uses this as default
            //.addStatement(....code...)
            .build();

Upvotes: 0

Related Questions