Questions tagged [javapoet]

JavaPoet is a Java API for generating .java source files.

JavaPoet is a Java API for generating .java source files and is the successor to JavaWriter.

It is available on Github: https://github.com/square/javapoet

102 questions
11
votes
1 answer

JavaPoet + Android Studio "addModifiers(Modifier) cannot be applied to Modifier"

I've built an annotation processor for my Android project that builds a source file using JavaPoet. However, every time I need to call addModifiers on any JavaPoet object, Android Studio flags it as an error. It will say either Cannot resolve…
TBridges42
  • 1,709
  • 17
  • 27
10
votes
2 answers

javapoet - how to implement "extends" and "implements"

Using Javapoet, how to implement the following: class A extends class B class C implements Interface D In the javadoc, it is mentioned how to create interfaces.
KCMS
  • 187
  • 1
  • 13
10
votes
2 answers

JavaPoet Add Generic Parameter

How would I generate a method with the following signature? public void doSomething(T t) So far I have: MethodSpec.methodBuilder("doSomething") .addModifiers(Modifier.PUBLIC) …
Eliezer
  • 6,606
  • 9
  • 54
  • 100
10
votes
1 answer

How to add the 'Any Type' questionmark in JavaPoet?

I'm generating code with JavaPoet. Somewhere in the generated code I want to add a method which has the following argument. ... public B someMethod(final AbstractObjectBuilder builder) { ... } ... So my JavaPoet code…
Pieter Degraeuwe
  • 349
  • 4
  • 15
9
votes
1 answer

Generic Class with JavaPoet

Hey I'm trying to generate a class like this: public abstract class ResourceListAdapter extends RecyclerView.Adapter {} At the moment I can generate: public abstract class ResourceListAdapter extends…
Marcel Groß
  • 201
  • 1
  • 5
9
votes
1 answer

Javapoet superclass generic

Anyone know how I can do the following using javapoet public class MyClassGenerated extends MyMapper{ } My code of generation: TypeSpec generateClass() { return classBuilder("MyClassGenerated") .addModifiers(PUBLIC) …
Marco Giovanni
  • 277
  • 4
  • 15
8
votes
1 answer

Generation of switch statement with JavaPoet

I'm working on a annotation processor written in java. I'm using JavaPoet to generate some lines of code. I have to generate a 'switch' statement. Now i'm using the following code: MethodSpec.Builder methodBuilder =…
xcesco
  • 4,118
  • 3
  • 26
  • 53
7
votes
2 answers

how to generate symbol Class with javapoet

i want to generate a field like this: public static Map> ID_MAP = new HashMap>(); WildcardTypeName.subtypeOf(Object.class) can give '?' WildcardTypeName.subtypeOf(Class.class) can give 'Class'
dong sheng
  • 176
  • 1
  • 8
7
votes
1 answer

Javapoet/JavaWriter append to existing class

I have been experimenting with code generation in an annotation processor. Consider the following piece of code that adds a constructor that has a statement in it. private void addRegister(ExecutableElement el) { MethodSpec builder =…
Limnic
  • 1,596
  • 1
  • 13
  • 38
6
votes
0 answers

AnnotationProcessing - Generating Files at Each Round vs at Last Round

I was playing around with annotation processing and was unable to use generated files directly via an import in my code. Instead I had to prepend the generated class with its complete package. I posted a SO question error: package generated.schema…
Abbas
  • 2,875
  • 4
  • 32
  • 57
6
votes
1 answer

How to generate inner class in javapoet in java

Is there a any way to generate inner classes in javapoet library. I can generate classes with constructors and methods. But i can't figure out how to create inner classes
Damith
  • 1,866
  • 3
  • 27
  • 38
6
votes
1 answer

Generating annotations using JavaPoet

I am writing a code generator using JavaPoet and need to put an annotation on the class For example : package some.package import org.hibernate.annotations.CacheConcurrencyStrategy; import javax.persistence.Entity; import…
nvalada
  • 245
  • 1
  • 10
5
votes
1 answer

JavaPoet - Field of type inner class

I am trying to add an inner class (e.g. interface Listener{}) to a TypeSpec. Also I want to add a field of type Listener to my TypeSpec. How could i achieve something like that? TypeSpec outerClass = ...; TypeSpec innerClass =…
Saeed Entezari
  • 3,303
  • 1
  • 14
  • 35
5
votes
1 answer

How do I get JavaPoet to generate a class literal?

I want to use JavaPoet to generate an annotation with a type literal as value. For example: @AutoService(MyService.class) public class GeneratedClass implements MyService { } I've tried all the options I can think of, but none…
Jorn
  • 16,599
  • 15
  • 67
  • 103
5
votes
1 answer

Annotation Code Gen with JavaPoet

I am writing a code generator using JavaPoet and need to put an annotation on a class For example : @RequestMapping("/api") public class SomeResource { // rest of the code elided } I am able to get this far: TypeSpec spec = TypeSpec …
nvalada
  • 245
  • 1
  • 10
1
2 3 4 5 6 7