#[Question] how can I write a annotation processor ?

1 messages · Page 1 of 1 (latest)

finite mural
#

hey guys, for some reason, I need to define a annotation with the retention of source, and you can check the source code I have uploaded, these file is in the annotation-processor module

in another module, I use the Builder annotation in this way

@Builder
public class Article {
    public int id;

    @Nonnull
    public String title;

    @Nonnull
    public String markdownContent;

    @Nonnull
    public String htmlContent;

    @Nonnull
    public String summary;

    @Nonnull
    public java.sql.Timestamp publishDate;

    @Nonnull
    public java.sql.Timestamp editTime;

    private Article() {
        this.id = -1;
        this.title = null;
        this.markdownContent = null;
        this.htmlContent = null;
        this.summary = null;
        this.publishDate = null;
        this.editTime = null;
    }
}

when build this module,the error occurs

4 actionable tasks: 3 executed, 1 up-to-date
错误: [IOException] generate code failed: `Attempt to recreate a file for type com.steiner.source_retention.model.Article`
1 个错误

so this is the error, I don't know how to fix it, and I don't know how can I process the generate content(file content) ,
how does the @Builder in lombok process, may I ask ?

knotty yewBOT
#

<@&987246399047479336> please have a look, thanks.

cedar venture
#

i think a very common misconception that lombok proliferates is the idea that an annotation processor can edit code

#

it cannot

#

lombok is breaking the rules of annotation processors when it does so

#

All you can do is generate new files

#

You can't change existing ones

#

Now there isn't going to be a way to generate a new source file that does what you want - you can have an unnested ArticleBuilder, but not an Article.Builder

#

You could use the class file api to make a new class file and that could work though

finite mural
#

@cedar venture ok I got it
but how can I get the Builder instance from Article ?
what can I do is to insert a static method called builder , which return the Builder instance (using javapoet, that means change the existing file, it will failed anyway)

cedar venture
#

static methods are "inherited" and classes you extend from are technically new files

#

I do that here

#

where if you have

#
@MagicBean(allArgsStaticFactory = true)
public final class Person extends PersonOps {
    String name;
    int age;
}
#

it will generate

#
sealed abstract class PersonOps extends java.lang.Object permits Person {

    private Person self() {
        return (switch (this) { case Person __ -> __; });
    }

    /**
     * Creates an instance of Person.
     */
    public static Person of(
        java.lang.String name,
        int age
    ) {
        var o$ = new Person();
        o$.setName(name);
        o$.setAge(age);
        return o$;
    }

    /**
     * Get the current value for name.
     */
    public java.lang.String getName() {
        return self().name;
    }

    /**
     * Set the current value for name.
     */
    public void setName(java.lang.String name) {
        self().name = name;
    }

    /**
     * Get the current value for age.
     */
    public int getAge() {
        return self().age;
    }

    /**
     * Set the current value for age.
     */
    public void setAge(int age) {
        self().age = age;
    }

}
#

and that of static method is inherited

#

so it pops up when you use the class