Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for '*' in @Builder.className #39

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,22 @@ public final class MyValueClass {
}
```

If you want to change the name of the generated Builder class with a meta-annotation,
you typically need that name to depend on the name of the class being built,
not just be a constant string
(since that's likely to cause conflicts if the meta-annotation is used more than once).
Because of that, similarly to `@BuilderInterfaces.innerName`,
`@Builder.className` allows using the `*` character as a placeholder for the name of the built class,
so you could define the meta-annotation like this:

```java
import org.jilt.Builder;

@Builder(className = "*JiltBuilder")
public @interface MyBuilder {
}
```

**Note**: since Jilt is implemented as a Java annotation processor,
that means it shares the limitations common to all annotation processors.
The restriction that is most relevant to meta-annotation support is that only files from a single source set
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/jilt/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,13 @@
* Allows declaring what should be the name of the generated Builder class.
* Must be a valid Java (top-level) class name.
* <p>
* This is an optional attribute - the default is <code>&lt;TargetClass&gt;Builder</code>
* (so, for example, if we're building an instance of a <code>Person</code> class,
* the default name will be <code>PersonBuilder</code>).
* When used for creating a meta-annotation by placing {@link Builder} on a different annotation,
* you can use the {@code *} character as a placeholder for the name of the target class.
* <p>
* This is an optional attribute - the default is {@code <TargetClassName>Builder},
* same as {@code *Builder}
* (so, for example, if we're building an instance of a {@code Person} class,
* the default name will be {@code PersonBuilder}).
*/
String className() default "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ protected final String builderClassStringName() {
String annotationBuilderClassName = builderAnnotation.className();
return annotationBuilderClassName.isEmpty()
? this.targetClassSimpleName() + "Builder"
: annotationBuilderClassName;
// we need to replace any '*' in className with the target class's name
: annotationBuilderClassName.replaceAll("\\*", this.targetClassSimpleName().toString());
}

protected final Filer filer() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/jilt/test/MetaAnnotationsTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.jilt.test;

import org.jilt.test.data.meta.MetaConstructorValue;
import org.jilt.test.data.meta.MetaConstructorValueBuilder;
import org.jilt.test.data.meta.MetaConstructorValueBuilders.Meta;
import org.jilt.test.data.meta.MetaConstructorValueJiltBuilder;
import org.jilt.test.data.meta.MetaConstructorValueJiltBuilders.Meta;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class MetaAnnotationsTest {
@Test
public void test_meta_builder_on_constructor() {
Meta lastValue = MetaConstructorValueBuilder.builder()
Meta lastValue = MetaConstructorValueJiltBuilder.builder()
.withAttr2("attr2_value")
.withAttr4(4)
.withAttr3(true);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jilt/test/data/meta/MetaBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.jilt.BuilderStyle;

@BuilderInterfaces(lastInnerName = "Meta")
@Builder(setterPrefix = "with", factoryMethod = "builder", style = BuilderStyle.STAGED)
@Builder(setterPrefix = "with", factoryMethod = "builder", style = BuilderStyle.STAGED, className = "*JiltBuilder")
public @interface MetaBuilder {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

@Builder(
style = BuilderStyle.TYPE_SAFE_UNGROUPED_OPTIONALS,
className = "TypeSafeValueCreator",
className = "*Creator",
packageName = "org.jilt.test.data.typesafe_ungrouped.custom",
setterPrefix = "with",
factoryMethod = "creator",
buildMethod = "create"
)
@BuilderInterfaces(
outerName = "TypeSafeValueCreators",
packageName = "org.jilt.test.data.typesafe_ungrouped.custom.customer",
innerNames = "Step_*"
)
Expand Down
Loading