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 meta-annotation support #15

Merged
merged 5 commits into from Mar 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/jilt/Builder.java
Expand Up @@ -225,7 +225,7 @@
* @see #buildMethod
* @see BuilderInterfaces
*/
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface Builder {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jilt/BuilderInterfaces.java
Expand Up @@ -25,7 +25,7 @@
* @see Builder#style
* @see BuilderStyle
*/
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface BuilderInterfaces {
/**
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/org/jilt/JiltAnnotationProcessor.java
@@ -1,6 +1,7 @@
package org.jilt;

import org.jilt.internal.BuilderGeneratorFactory;
import org.jilt.utils.Annotations;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
Expand All @@ -9,10 +10,13 @@
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JiltAnnotationProcessor extends AbstractProcessor {
Expand All @@ -33,18 +37,39 @@ public synchronized void init(ProcessingEnvironment processingEnv) {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Builder.class)) {
for (Map.Entry<Element, Annotations> entry : getAnnotatedElements(roundEnv).entrySet()) {
try {
builderGeneratorFactory.forElement(annotatedElement).generateBuilderClass();
builderGeneratorFactory.forElement(entry.getKey(), entry.getValue()).generateBuilderClass();
} catch (Exception e) {
error(annotatedElement, e.getMessage());
return true;
error(entry.getKey(), e.getMessage());
}
}

return true;
}

private Map<Element, Annotations> getAnnotatedElements(RoundEnvironment roundEnv) {
Set<? extends Element> builderElements = roundEnv.getElementsAnnotatedWith(Builder.class);
Map<Element, Annotations> annotatedElements = initMap(builderElements, null, null);
for (Element builderElement : builderElements) {
if (builderElement.getKind() == ElementKind.ANNOTATION_TYPE) {
annotatedElements.remove(builderElement);
annotatedElements.putAll(initMap(roundEnv.getElementsAnnotatedWith((TypeElement) builderElement),
builderElement.getAnnotation(Builder.class), builderElement.getAnnotation(BuilderInterfaces.class)));
}
}

return annotatedElements;
}

private Map<Element, Annotations> initMap(Set<? extends Element> builderElements, Builder builderAnnotation, BuilderInterfaces builderInterfaces) {
Map<Element, Annotations> map = new HashMap<Element, Annotations>();
for (Element element : builderElements) {
map.put(element, new Annotations(builderAnnotation, builderInterfaces));
}
return map;
}

private void error(Element element, String msg, Object... args) {
messager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), element);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jilt/internal/BuilderGeneratorFactory.java
Expand Up @@ -2,6 +2,7 @@

import org.jilt.Builder;
import org.jilt.BuilderInterfaces;
import org.jilt.utils.Annotations;

import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -34,7 +35,7 @@ public BuilderGeneratorFactory(Filer filer, Elements elements) {
this.elements = elements;
}

public BuilderGenerator forElement(Element annotatedElement) throws Exception {
public BuilderGenerator forElement(Element annotatedElement, Annotations annotations) throws Exception {
TypeElement targetClass;
List<? extends VariableElement> attributes;
ExecutableElement targetFactoryMethod = null;
Expand Down Expand Up @@ -66,8 +67,8 @@ public BuilderGenerator forElement(Element annotatedElement) throws Exception {
"@Builder can only be placed on classes/records, constructors or static methods");
}

Builder builderAnnotation = annotatedElement.getAnnotation(Builder.class);
BuilderInterfaces builderInterfaces = annotatedElement.getAnnotation(BuilderInterfaces.class);
Builder builderAnnotation = annotations.getBuilder() == null ? annotatedElement.getAnnotation(Builder.class) : annotations.getBuilder();
BuilderInterfaces builderInterfaces = annotations.getBuilderInterface() == null ? annotatedElement.getAnnotation(BuilderInterfaces.class) : annotations.getBuilderInterface();
switch (builderAnnotation.style()) {
case STAGED:
case TYPE_SAFE:
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/jilt/utils/Annotations.java
@@ -0,0 +1,22 @@
package org.jilt.utils;

import org.jilt.Builder;
import org.jilt.BuilderInterfaces;

public final class Annotations {
private final Builder builder;
private final BuilderInterfaces builderInterfaces;

public Annotations(Builder builder, BuilderInterfaces builderInterfaces) {
this.builder = builder;
this.builderInterfaces = builderInterfaces;
}

public Builder getBuilder() {
return builder;
}

public BuilderInterfaces getBuilderInterface() {
return builderInterfaces;
}
}
23 changes: 23 additions & 0 deletions src/test/java/org/jilt/test/MetaAnnotationTest.java
@@ -0,0 +1,23 @@
package org.jilt.test;

import org.jilt.test.data.constructor.MetaConstructorValue;
import org.jilt.test.data.constructor.MetaConstructorValueBuilder;
import org.junit.Test;

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

public class MetaAnnotationTest {
@Test
public void test__meta_builder_on_constructor() {
MetaConstructorValue value = MetaConstructorValueBuilder.builder()
.withAttr2("attr2_value")
.withAttr4(4)
.withAttr3(true)
.build();

assertThat(value.attr1).isEqualTo(123);
assertThat(value.attr2).isEqualTo("attr2_value");
assertThat(value.attr3).isTrue();
assertThat(value.attr4).isEqualTo(4);
}
}
10 changes: 10 additions & 0 deletions src/test/java/org/jilt/test/data/annotations/MetaBuilder.java
@@ -0,0 +1,10 @@
package org.jilt.test.data.annotations;

import org.jilt.Builder;
import org.jilt.BuilderInterfaces;
import org.jilt.BuilderStyle;

@BuilderInterfaces(lastInnerName = "Meta")
@Builder(setterPrefix = "with", factoryMethod = "builder", style = BuilderStyle.STAGED)
public @interface MetaBuilder {
}
@@ -0,0 +1,18 @@
package org.jilt.test.data.constructor;

import org.jilt.test.data.annotations.MetaBuilder;

public class MetaConstructorValue {
public final int attr1;
public final String attr2;
public final boolean attr3;
public final int attr4;

@MetaBuilder
public MetaConstructorValue(String attr2, int attr4, boolean attr3) {
attr1 = 123;
this.attr2 = attr2;
this.attr3 = attr3;
this.attr4 = attr4;
}
}