#[javapoet] how to create custom @Builder annotation ?

1 messages · Page 1 of 1 (latest)

echo elbow
#

hey, this is my first time to process a annotation with the Source retention, and this is the annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Builder {
}

and this is the class

@Builder
public final class Article {
    public int id;

    @Nonnull
    public String title;

    @Nonnull
    public String markdownContent;

    @Nonnull
    public String htmlContent;

    @Nonnull
    public String summary;

    @Nullable
    public Category category;

    @Nonnull
    public User author;

    @Nonnull
    public java.sql.Timestamp publishDate;

    @Nonnull
    public java.sql.Timestamp editTime;

    @Nonnull
    public ArticleStatus status;

    @Nonnull
    public List<Tag> tags;

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

I have upload the file of annotation processor
and I found an error occurs here

// BuilderProcessor.java at line 168
try {
    javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "generate code faild: `%s`".formatted(e.getMessage()));
}

the error is "generate code faild: Attempt to recreate a file for type com.steiner.backend.model.article.Article"

so I think I should remove the Article.class file first, and then compile again

so what is the correct way to handle this ?
ps: this code is from GPT
ps again: this javapoet is from spring.core

halcyon turtleBOT
#

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

halcyon turtleBOT
echo elbow
#

by the way, I have modify the build.gradle.kts to enable annotation-processor working

 dependencies {
    implementation(project(":common"))
    implementation(project(":annotation-processor"))
    annotationProcessor(project(":annotation-processor"))
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<JavaCompile> {
    options.compilerArgs.add("-proc:only")
}

is this correct ?

halcyon turtleBOT