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

104 questions
0
votes
1 answer

Android studio shows very specific incorrect errors with JavaPoet library

I've written an annotation processor using JavaPoet library and included it as a Java Library module to my project. It compiles and works fine. The problem is - Android Studio inline compiler shows me errors that shouldn't be there. For some reason…
A. Stef
  • 23
  • 1
  • 5
0
votes
0 answers

How to guarantee java file is created before call a function

I am using JavaPoet to generate code, here is the core part of my code snippet: ... TypeSpec typeSpec = createTypeSpec(); JavaFile javaFile = JavaFile.builder(myPackage, typeSpec).build(); // write to file javaFile.writeTo(filer); // I want to…
user842225
  • 5,445
  • 15
  • 69
  • 119
0
votes
1 answer

How to construct enum with custom type using JavaPoet

Is it possible to generate the following enum class using JavaPoet? public enum EnumName { import com.sth.sth.SomeClass1; import com.sth.sth.SomeClass2; ITEM1(new CustomType("string1", "string 2", SomeClass1.class)), ITEM2(new…
Juraj V.
  • 11
  • 3
0
votes
1 answer

How to generate code dynamically with javapoet? Change the method parameters

I'm trying to generate java code that depends on other variables. In this specific moment, I'm trying to generate the code depending on the protocol. So I can receive a CoapResponse object or a Response object form ( javax). I don't know if it is…
0
votes
0 answers

How does javapoet process the AnnotationValue of the annotate attribute value

Methods to get custom annotations using AbstractProcessor Gets the attribute name and value of the annotation The attribute value is of type AnnotationValue Generate code with javapoet AnnotationSpec.Builder(xx). AddMember (String…
0
votes
1 answer

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") …
0
votes
1 answer

error: package generated.schema does not exist

In my Android Application I have an annotation processor which generates files using JavaPoet and places them under the package generated.schema. The files are generating correctly. Whenever I use the generated file like…
Abbas
  • 3,529
  • 5
  • 36
  • 64
0
votes
2 answers

Write a java file using JavaPoet in annotation processor

I am trying to use processingEnv.getFiler() to create a source file. But I don't see any source file getting created. Below is the code that I am using: public void javaPoetEg() { Filer filer = super.processingEnv.getFiler(); MethodSpec main =…
Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
0
votes
1 answer

Run Lint/Sonarqube anaysis on generated code

I'm working on an annotation processing library that generates java code using JavaPoet. It is composed by three modules An annotation processor A library that defines the annotations and some other helper code A test app that uses both The whole…
Francesco Rigoni
  • 923
  • 7
  • 20
0
votes
1 answer

Generate a call to a method with a variable number of arguments

I'm using JavaPoet to generate classes given a list of fields. Generating a constructor that takes this list of fields and turns it into a list of parameters is fairly straightforward with something like the following: val constructor =…
stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
0
votes
0 answers

JavaPoet : I have created a .java file and want to add to the existing jar file at runtime. How do I do that?

I am trying to create a installer for the java project. I have project say codeGenerator, on making installer the resulting jar would be codeGenrerator.jar I am trying to use javapoet to create a .java file, but I am not able to write the file to…
Priya K
  • 23
  • 4
0
votes
1 answer

Using an Android SDK class in a custom annotation processor

I am trying to implement an annotation processor to fill views with a foreground color (nothing fancy for the moment). I have implemented my processor to process every Type annotation with my custom annotation. The process is going straight so far…
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
0
votes
1 answer

Generate a field as anonymous class with JavaPoet

I'm working on an annotation processor library and I'm using JavaPoet to generate som Java code. I need to generate a class with a field declared with an anonymous class like the following code: public class Dummy { private final…
xcesco
  • 4,690
  • 4
  • 34
  • 65
0
votes
1 answer

JavaPoet - Writing a call to a static generic method

I want to write a CodeBlock which is basically calling a static generic method, but the output I get with JavaPoet is generating compiler errors. For example given the following auto value class: @AutoValue public abstract class Test { public…
smac89
  • 39,374
  • 15
  • 132
  • 179
0
votes
1 answer

How to add type constraints to parameterSpec for method

I'm trying to do exactly what the title says -- I'd like to generate a method spec that looks something like: public void doSomethingWithThis( Container argument ) { //1. Collect UnderPants //2. ... //3. Profit } I…
lscoughlin
  • 2,327
  • 16
  • 23