Skip to content

Commit f0e6294

Browse files
authored
Add support for handling multiple AnnotateWith (#697)
1 parent 9a6e5be commit f0e6294

File tree

9 files changed

+188
-41
lines changed

9 files changed

+188
-41
lines changed

doma-processor/src/main/java/org/seasar/doma/internal/apt/annot/Annotations.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
44

55
import java.util.ArrayList;
6+
import java.util.List;
67
import java.util.Map;
78
import java.util.function.BiFunction;
89
import java.util.function.Function;
10+
import java.util.stream.Collectors;
911
import javax.lang.model.element.AnnotationMirror;
1012
import javax.lang.model.element.AnnotationValue;
1113
import javax.lang.model.element.Element;
@@ -57,33 +59,42 @@ public AllArgsConstructorAnnot newAllArgsConstructorAnnot(TypeElement typeElemen
5759
typeElement, ctx.getOptions().getLombokAllArgsConstructor(), AllArgsConstructorAnnot::new);
5860
}
5961

60-
public AnnotateWithAnnot newAnnotateWithAnnot(TypeElement typeElement) {
62+
public List<AnnotateWithAnnot> newAnnotateWithAnnots(TypeElement typeElement) {
6163
assertNotNull(typeElement);
62-
AnnotationMirror annotateWith =
63-
ctx.getMoreElements().getAnnotationMirror(typeElement, AnnotateWith.class);
64-
if (annotateWith == null) {
65-
for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) {
66-
TypeElement ownerElement =
67-
ctx.getMoreElements().toTypeElement(annotationMirror.getAnnotationType().asElement());
68-
if (ownerElement == null) {
69-
continue;
70-
}
71-
annotateWith = ctx.getMoreElements().getAnnotationMirror(ownerElement, AnnotateWith.class);
72-
if (annotateWith != null) {
73-
break;
74-
}
75-
}
76-
if (annotateWith == null) {
77-
return null;
64+
List<AnnotationMirror> annotateWiths = new ArrayList<>();
65+
{
66+
AnnotationMirror annotateWith =
67+
ctx.getMoreElements().getAnnotationMirror(typeElement, AnnotateWith.class);
68+
if (annotateWith != null) {
69+
annotateWiths.add(annotateWith);
7870
}
7971
}
80-
Map<String, AnnotationValue> values = ctx.getMoreElements().getValuesWithDefaults(annotateWith);
81-
AnnotationValue annotations = values.get(AnnotateWithAnnot.ANNOTATIONS);
82-
ArrayList<AnnotationAnnot> annotationsValue = new ArrayList<>();
83-
for (AnnotationMirror annotationMirror : AnnotationValueUtil.toAnnotationList(annotations)) {
84-
annotationsValue.add(newAnnotationAnnot(annotationMirror));
72+
for (AnnotationMirror annotationMirror : typeElement.getAnnotationMirrors()) {
73+
TypeElement ownerElement =
74+
ctx.getMoreElements().toTypeElement(annotationMirror.getAnnotationType().asElement());
75+
if (ownerElement == null) {
76+
continue;
77+
}
78+
AnnotationMirror annotateWith =
79+
ctx.getMoreElements().getAnnotationMirror(ownerElement, AnnotateWith.class);
80+
if (annotateWith != null) {
81+
annotateWiths.add(annotateWith);
82+
}
8583
}
86-
return new AnnotateWithAnnot(annotateWith, annotations, annotationsValue);
84+
return annotateWiths.stream()
85+
.map(
86+
annotateWith -> {
87+
Map<String, AnnotationValue> values =
88+
ctx.getMoreElements().getValuesWithDefaults(annotateWith);
89+
AnnotationValue annotations = values.get(AnnotateWithAnnot.ANNOTATIONS);
90+
ArrayList<AnnotationAnnot> annotationsValue = new ArrayList<>();
91+
for (AnnotationMirror annotationMirror :
92+
AnnotationValueUtil.toAnnotationList(annotations)) {
93+
annotationsValue.add(newAnnotationAnnot(annotationMirror));
94+
}
95+
return new AnnotateWithAnnot(annotateWith, annotations, annotationsValue);
96+
})
97+
.collect(Collectors.toList());
8798
}
8899

89100
private AnnotationAnnot newAnnotationAnnot(AnnotationMirror annotationMirror) {

doma-processor/src/main/java/org/seasar/doma/internal/apt/generator/DaoImplGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private void printConstructors() {
133133
if (daoMeta.hasUserDefinedConfig()) {
134134
Code configCode = createConfigCode();
135135
printNoArgConstructor(configCode);
136-
if (daoMeta.getAnnotateWithAnnot() == null) {
136+
if (daoMeta.getAnnotateWithAnnots().isEmpty()) {
137137
boolean required = areJdbcConstructorsRequired();
138138
if (required) {
139139
printConnectionArgConstructor(configCode);

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/dao/DaoMeta.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class DaoMeta implements TypeElementMeta {
2121

2222
private final DaoAnnot daoAnnot;
2323

24-
private AnnotateWithAnnot annotateWithAnnot;
24+
private List<AnnotateWithAnnot> annotateWithAnnots = Collections.emptyList();
2525

2626
private TypeMirror type;
2727

@@ -90,23 +90,22 @@ public AccessLevel getAccessLevel() {
9090
return daoAnnot.getAccessLevelValue();
9191
}
9292

93-
public AnnotateWithAnnot getAnnotateWithAnnot() {
94-
return annotateWithAnnot;
93+
public List<AnnotateWithAnnot> getAnnotateWithAnnots() {
94+
return annotateWithAnnots;
9595
}
9696

97-
public void setAnnotateWithAnnot(AnnotateWithAnnot annotateWithAnnot) {
98-
this.annotateWithAnnot = annotateWithAnnot;
97+
public void setAnnotateWithAnnots(List<AnnotateWithAnnot> annotateWithAnnots) {
98+
this.annotateWithAnnots = annotateWithAnnots;
9999
}
100100

101101
public List<AnnotationAnnot> getAnnotationMirrors(AnnotationTarget target) {
102102
assertNotNull(target);
103-
if (annotateWithAnnot == null || annotateWithAnnot.getAnnotationsValue() == null) {
104-
return Collections.emptyList();
105-
}
106103
List<AnnotationAnnot> results = new ArrayList<>();
107-
for (AnnotationAnnot annotationAnnot : annotateWithAnnot.getAnnotationsValue()) {
108-
if (target.name().contentEquals(annotationAnnot.getTargetValue().getSimpleName())) {
109-
results.add(annotationAnnot);
104+
for (AnnotateWithAnnot annotateWithAnnot : annotateWithAnnots) {
105+
for (AnnotationAnnot annotationAnnot : annotateWithAnnot.getAnnotationsValue()) {
106+
if (target.name().contentEquals(annotationAnnot.getTargetValue().getSimpleName())) {
107+
results.add(annotationAnnot);
108+
}
110109
}
111110
}
112111
return results;

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/dao/DaoMetaFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,9 @@ private void validateInterface(TypeElement interfaceElement, DaoMeta daoMeta) {
219219
}
220220

221221
private void doAnnotateWith(DaoMeta daoMeta) {
222-
AnnotateWithAnnot annotateWithAnnot =
223-
ctx.getAnnotations().newAnnotateWithAnnot(daoMeta.getTypeElement());
224-
if (annotateWithAnnot != null) {
225-
daoMeta.setAnnotateWithAnnot(annotateWithAnnot);
226-
}
222+
List<AnnotateWithAnnot> annotateWithAnnots =
223+
ctx.getAnnotations().newAnnotateWithAnnots(daoMeta.getTypeElement());
224+
daoMeta.setAnnotateWithAnnots(annotateWithAnnots);
227225
}
228226

229227
private void doParentDao(DaoMeta daoMeta) {

doma-processor/src/test/java/org/seasar/doma/internal/apt/processor/dao/DaoProcessorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
129129
invocationContext(OnlyDefaultMethodsExtendsDao.class),
130130
invocationContext(
131131
ApplicationScopedDao.class,
132-
"-Adoma.cdi.ApplicationScoped=" + ApplicationScoped.class.getCanonicalName()));
132+
"-Adoma.cdi.ApplicationScoped=" + ApplicationScoped.class.getCanonicalName()),
133+
invocationContext(MultipleAnnotateWithDao.class));
133134
}
134135

135136
private TestTemplateInvocationContext invocationContext(Class<?> clazz, String... options) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.seasar.doma.internal.apt.processor.dao;
2+
3+
import org.seasar.doma.AnnotateWith;
4+
import org.seasar.doma.Annotation;
5+
import org.seasar.doma.AnnotationTarget;
6+
import org.seasar.doma.Dao;
7+
import org.seasar.doma.Insert;
8+
import org.seasar.doma.internal.apt.processor.entity.Emp;
9+
10+
@Dao
11+
@MultipleAnnotationConfig1
12+
@MultipleAnnotationConfig2
13+
@AnnotateWith(
14+
annotations = {
15+
@Annotation(
16+
target = AnnotationTarget.CONSTRUCTOR_PARAMETER,
17+
type = ConstructorParameterAnnotation.class,
18+
elements = "aaa = 1, bbb = true"),
19+
@Annotation(
20+
target = AnnotationTarget.CONSTRUCTOR_PARAMETER,
21+
type = ConstructorParameterAnnotation2.class,
22+
elements = "aaa = 1, bbb = true")
23+
})
24+
public interface MultipleAnnotateWithDao {
25+
26+
@Insert
27+
int insert(Emp emp);
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.seasar.doma.internal.apt.processor.dao;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
import org.seasar.doma.AnnotateWith;
8+
import org.seasar.doma.Annotation;
9+
import org.seasar.doma.AnnotationTarget;
10+
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@AnnotateWith(
14+
annotations = {
15+
@Annotation(
16+
target = AnnotationTarget.CLASS,
17+
type = ClassAnnotation.class,
18+
elements = "aaa = 1, bbb = true"),
19+
@Annotation(
20+
target = AnnotationTarget.CLASS,
21+
type = ClassAnnotation2.class,
22+
elements = "aaa = 1, bbb = true"),
23+
})
24+
public @interface MultipleAnnotationConfig1 {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.seasar.doma.internal.apt.processor.dao;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
import org.seasar.doma.AnnotateWith;
8+
import org.seasar.doma.Annotation;
9+
import org.seasar.doma.AnnotationTarget;
10+
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@AnnotateWith(
14+
annotations = {
15+
@Annotation(
16+
target = AnnotationTarget.CONSTRUCTOR,
17+
type = ConstructorAnnotation.class,
18+
elements = "aaa = 1, bbb = true"),
19+
@Annotation(
20+
target = AnnotationTarget.CONSTRUCTOR,
21+
type = ConstructorAnnotation2.class,
22+
elements = "aaa = 1, bbb = true"),
23+
})
24+
public @interface MultipleAnnotationConfig2 {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.seasar.doma.internal.apt.processor.dao;
2+
3+
/** */
4+
@org.seasar.doma.internal.apt.processor.dao.ClassAnnotation(aaa = 1, bbb = true)
5+
@org.seasar.doma.internal.apt.processor.dao.ClassAnnotation2(aaa = 1, bbb = true)
6+
@javax.annotation.Generated(value = { "Doma", "@VERSION@" }, date = "1970-01-01T09:00:00.000+0900")
7+
@org.seasar.doma.DaoImplementation
8+
public class MultipleAnnotateWithDaoImpl implements org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDao, org.seasar.doma.jdbc.ConfigProvider {
9+
10+
static {
11+
org.seasar.doma.internal.Artifact.validateVersion("@VERSION@");
12+
}
13+
14+
private static final java.lang.reflect.Method __method0 = org.seasar.doma.internal.jdbc.dao.DaoImplSupport.getDeclaredMethod(org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDao.class, "insert", org.seasar.doma.internal.apt.processor.entity.Emp.class);
15+
16+
private final org.seasar.doma.internal.jdbc.dao.DaoImplSupport __support;
17+
18+
/**
19+
* @param config the config
20+
*/
21+
@org.seasar.doma.internal.apt.processor.dao.ConstructorAnnotation(aaa = 1, bbb = true)
22+
@org.seasar.doma.internal.apt.processor.dao.ConstructorAnnotation2(aaa = 1, bbb = true)
23+
public MultipleAnnotateWithDaoImpl(@org.seasar.doma.internal.apt.processor.dao.ConstructorParameterAnnotation(aaa = 1, bbb = true) @org.seasar.doma.internal.apt.processor.dao.ConstructorParameterAnnotation2(aaa = 1, bbb = true) org.seasar.doma.jdbc.Config config) {
24+
__support = new org.seasar.doma.internal.jdbc.dao.DaoImplSupport(config);
25+
}
26+
27+
@Override
28+
public org.seasar.doma.jdbc.Config getConfig() {
29+
return __support.getConfig();
30+
}
31+
32+
@Override
33+
public int insert(org.seasar.doma.internal.apt.processor.entity.Emp emp) {
34+
__support.entering("org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDaoImpl", "insert", emp);
35+
try {
36+
if (emp == null) {
37+
throw new org.seasar.doma.DomaNullPointerException("emp");
38+
}
39+
org.seasar.doma.jdbc.query.AutoInsertQuery<org.seasar.doma.internal.apt.processor.entity.Emp> __query = __support.getQueryImplementors().createAutoInsertQuery(__method0, org.seasar.doma.internal.apt.processor.entity._Emp.getSingletonInternal());
40+
__query.setMethod(__method0);
41+
__query.setConfig(__support.getConfig());
42+
__query.setEntity(emp);
43+
__query.setCallerClassName("org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDaoImpl");
44+
__query.setCallerMethodName("insert");
45+
__query.setQueryTimeout(-1);
46+
__query.setSqlLogType(org.seasar.doma.jdbc.SqlLogType.FORMATTED);
47+
__query.setNullExcluded(false);
48+
__query.setIncludedPropertyNames();
49+
__query.setExcludedPropertyNames();
50+
__query.prepare();
51+
org.seasar.doma.jdbc.command.InsertCommand __command = __support.getCommandImplementors().createInsertCommand(__method0, __query);
52+
int __result = __command.execute();
53+
__query.complete();
54+
__support.exiting("org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDaoImpl", "insert", __result);
55+
return __result;
56+
} catch (java.lang.RuntimeException __e) {
57+
__support.throwing("org.seasar.doma.internal.apt.processor.dao.MultipleAnnotateWithDaoImpl", "insert", __e);
58+
throw __e;
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)