Skip to content

Commit c64c031

Browse files
committed
Simplify Annot classes
1 parent ea86c2e commit c64c031

File tree

62 files changed

+852
-1293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+852
-1293
lines changed

src/main/java/org/seasar/doma/internal/apt/Context.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.seasar.doma.internal.apt;
22

33
import javax.annotation.processing.ProcessingEnvironment;
4+
import org.seasar.doma.internal.apt.annot.Annotations;
45

56
public class Context {
67

@@ -33,4 +34,8 @@ public Notifier getNotifier() {
3334
public Resources getResources() {
3435
return new Resources(this);
3536
}
37+
38+
public Annotations getAnnotations() {
39+
return new Annotations(this);
40+
}
3641
}

src/main/java/org/seasar/doma/internal/apt/Elements.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
import java.io.Writer;
66
import java.lang.annotation.Annotation;
7-
import java.util.Arrays;
8-
import java.util.List;
9-
import java.util.Map;
7+
import java.util.*;
108
import java.util.function.Predicate;
119
import javax.annotation.processing.ProcessingEnvironment;
1210
import javax.lang.model.element.*;
@@ -250,4 +248,15 @@ public ExecutableElement getNoArgConstructor(TypeElement typeElement) {
250248
}
251249
return null;
252250
}
251+
252+
public Map<String, AnnotationValue> getValuesWithDefaults(AnnotationMirror annotationMirror) {
253+
Map<String, AnnotationValue> map = new HashMap<>();
254+
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
255+
getElementValuesWithDefaults(annotationMirror).entrySet()) {
256+
String key = entry.getKey().getSimpleName().toString();
257+
AnnotationValue value = entry.getValue();
258+
map.put(key, value);
259+
}
260+
return Collections.unmodifiableMap(map);
261+
}
253262
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.seasar.doma.internal.apt.annot;
2+
3+
import javax.lang.model.element.AnnotationMirror;
4+
5+
class AbstractAnnot {
6+
7+
private final AnnotationMirror annotationMirror;
8+
9+
AbstractAnnot(AnnotationMirror annotationMirror) {
10+
this.annotationMirror = annotationMirror;
11+
}
12+
13+
public AnnotationMirror getAnnotationMirror() {
14+
return annotationMirror;
15+
}
16+
}

src/main/java/org/seasar/doma/internal/apt/annot/AllArgsConstructorAnnot.java

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,28 @@
11
package org.seasar.doma.internal.apt.annot;
22

3-
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
3+
import static org.seasar.doma.internal.util.AssertionUtil.assertNonNullValue;
44

55
import java.util.Map;
66
import javax.lang.model.element.AnnotationMirror;
77
import javax.lang.model.element.AnnotationValue;
8-
import javax.lang.model.element.ExecutableElement;
9-
import javax.lang.model.element.TypeElement;
108
import javax.lang.model.element.VariableElement;
119
import org.seasar.doma.internal.apt.AptIllegalStateException;
12-
import org.seasar.doma.internal.apt.Context;
1310
import org.seasar.doma.internal.apt.util.AnnotationValueUtil;
1411

15-
public class AllArgsConstructorAnnot {
12+
public class AllArgsConstructorAnnot extends AbstractAnnot {
1613

17-
protected final AnnotationMirror annotationMirror;
14+
private static final String STATIC_NAME = "staticName";
1815

19-
protected AnnotationValue staticName;
16+
private static final String ACCESS = "access";
2017

21-
protected AnnotationValue access;
18+
private final AnnotationValue staticName;
2219

23-
protected AllArgsConstructorAnnot(AnnotationMirror annotationMirror) {
24-
assertNotNull(annotationMirror);
25-
this.annotationMirror = annotationMirror;
26-
}
27-
28-
public static AllArgsConstructorAnnot newInstance(TypeElement typeElement, Context ctx) {
29-
assertNotNull(ctx);
30-
AnnotationMirror annotationMirror =
31-
ctx.getElements()
32-
.getAnnotationMirror(typeElement, ctx.getOptions().getLombokAllArgsConstructor());
33-
if (annotationMirror == null) {
34-
return null;
35-
}
36-
AllArgsConstructorAnnot result = new AllArgsConstructorAnnot(annotationMirror);
37-
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
38-
ctx.getElements().getElementValuesWithDefaults(annotationMirror).entrySet()) {
39-
String name = entry.getKey().getSimpleName().toString();
40-
AnnotationValue value = entry.getValue();
41-
if ("staticName".equals(name)) {
42-
result.staticName = value;
43-
} else if ("access".equals(name)) {
44-
result.access = value;
45-
}
46-
}
47-
return result;
48-
}
20+
private final AnnotationValue access;
4921

50-
public AnnotationMirror getAnnotationMirror() {
51-
return annotationMirror;
22+
AllArgsConstructorAnnot(AnnotationMirror annotationMirror, Map<String, AnnotationValue> values) {
23+
super(annotationMirror);
24+
this.staticName = assertNonNullValue(values, STATIC_NAME);
25+
this.access = assertNonNullValue(values, ACCESS);
5226
}
5327

5428
public AnnotationValue getStaticName() {
@@ -62,23 +36,23 @@ public AnnotationValue getAccess() {
6236
public String getStaticNameValue() {
6337
String value = AnnotationValueUtil.toString(staticName);
6438
if (value == null) {
65-
throw new AptIllegalStateException("staticConstructor");
39+
throw new AptIllegalStateException(STATIC_NAME);
6640
}
6741
return value;
6842
}
6943

7044
public boolean isAccessPrivate() {
7145
VariableElement enumConstant = AnnotationValueUtil.toEnumConstant(access);
7246
if (enumConstant == null) {
73-
throw new AptIllegalStateException("access");
47+
throw new AptIllegalStateException(ACCESS);
7448
}
7549
return "PRIVATE".equals(enumConstant.getSimpleName().toString());
7650
}
7751

7852
public boolean isAccessNone() {
7953
VariableElement enumConstant = AnnotationValueUtil.toEnumConstant(access);
8054
if (enumConstant == null) {
81-
throw new AptIllegalStateException("access");
55+
throw new AptIllegalStateException(ACCESS);
8256
}
8357
return "NONE".equals(enumConstant.getSimpleName().toString());
8458
}

src/main/java/org/seasar/doma/internal/apt/annot/AnnotateWithAnnot.java

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,26 @@
33
import static org.seasar.doma.internal.util.AssertionUtil.*;
44

55
import java.util.ArrayList;
6+
import java.util.Collections;
67
import java.util.List;
7-
import java.util.Map;
8+
import javax.lang.model.element.AnnotationMirror;
89
import javax.lang.model.element.AnnotationValue;
9-
import javax.lang.model.element.ExecutableElement;
10-
import javax.lang.model.element.TypeElement;
11-
import org.seasar.doma.AnnotateWith;
12-
import org.seasar.doma.internal.apt.Context;
13-
import org.seasar.doma.internal.apt.util.AnnotationValueUtil;
1410

15-
public class AnnotateWithAnnot {
11+
public class AnnotateWithAnnot extends AbstractAnnot {
1612

17-
protected final javax.lang.model.element.AnnotationMirror annotationMirror;
13+
static final String ANNOTATIONS = "annotations";
1814

19-
protected TypeElement ownerElement;
15+
private final AnnotationValue annotations;
2016

21-
protected AnnotationValue annotations;
17+
private final List<AnnotationAnnot> annotationsValue;
2218

23-
protected List<AnnotationAnnot> annotationsValue;
24-
25-
protected AnnotateWithAnnot(
26-
javax.lang.model.element.AnnotationMirror annotationMirror, TypeElement ownerElement) {
27-
assertNotNull(annotationMirror, ownerElement);
28-
this.annotationMirror = annotationMirror;
29-
this.ownerElement = ownerElement;
30-
}
31-
32-
public static AnnotateWithAnnot newInstance(TypeElement clazz, Context ctx) {
33-
assertNotNull(ctx);
34-
javax.lang.model.element.AnnotationMirror annotateWith =
35-
ctx.getElements().getAnnotationMirror(clazz, AnnotateWith.class);
36-
TypeElement ownerElement = null;
37-
if (annotateWith == null) {
38-
for (javax.lang.model.element.AnnotationMirror annotationMirror :
39-
clazz.getAnnotationMirrors()) {
40-
ownerElement =
41-
ctx.getElements().toTypeElement(annotationMirror.getAnnotationType().asElement());
42-
if (ownerElement == null) {
43-
continue;
44-
}
45-
annotateWith = ctx.getElements().getAnnotationMirror(ownerElement, AnnotateWith.class);
46-
if (annotateWith != null) {
47-
break;
48-
}
49-
}
50-
if (annotateWith == null) {
51-
return null;
52-
}
53-
} else {
54-
ownerElement = clazz;
55-
}
56-
AnnotateWithAnnot result = new AnnotateWithAnnot(annotateWith, ownerElement);
57-
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
58-
ctx.getElements().getElementValuesWithDefaults(annotateWith).entrySet()) {
59-
String name = entry.getKey().getSimpleName().toString();
60-
AnnotationValue value = entry.getValue();
61-
if ("annotations".equals(name)) {
62-
result.annotations = value;
63-
result.annotationsValue = new ArrayList<AnnotationAnnot>();
64-
for (javax.lang.model.element.AnnotationMirror a :
65-
AnnotationValueUtil.toAnnotationList(value)) {
66-
result.annotationsValue.add(AnnotationAnnot.newInstance(a, ctx));
67-
}
68-
}
69-
}
70-
return result;
71-
}
72-
73-
public TypeElement getOwnerElement() {
74-
return ownerElement;
75-
}
76-
77-
public javax.lang.model.element.AnnotationMirror getAnnotationMirror() {
78-
return annotationMirror;
19+
AnnotateWithAnnot(
20+
AnnotationMirror annotationMirror,
21+
AnnotationValue annotations,
22+
ArrayList<AnnotationAnnot> annotationsValues) {
23+
super(annotationMirror);
24+
this.annotations = annotations;
25+
this.annotationsValue = Collections.unmodifiableList(annotationsValues);
7926
}
8027

8128
public AnnotationValue getAnnotations() {

src/main/java/org/seasar/doma/internal/apt/annot/AnnotationAnnot.java

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,32 @@
33
import static org.seasar.doma.internal.util.AssertionUtil.*;
44

55
import java.util.Map;
6+
import javax.lang.model.element.AnnotationMirror;
67
import javax.lang.model.element.AnnotationValue;
7-
import javax.lang.model.element.ExecutableElement;
88
import javax.lang.model.element.VariableElement;
99
import javax.lang.model.type.TypeMirror;
1010
import org.seasar.doma.internal.apt.AptIllegalStateException;
11-
import org.seasar.doma.internal.apt.Context;
1211
import org.seasar.doma.internal.apt.util.AnnotationValueUtil;
1312

14-
public class AnnotationAnnot {
15-
protected final javax.lang.model.element.AnnotationMirror annotationMirror;
13+
public class AnnotationAnnot extends AbstractAnnot {
1614

17-
protected AnnotationValue target;
15+
private static final String TARGET = "target";
1816

19-
protected AnnotationValue type;
17+
private static final String TYPE = "type";
2018

21-
protected AnnotationValue elements;
19+
private static final String ELEMENTS = "elements";
2220

23-
protected AnnotationAnnot(javax.lang.model.element.AnnotationMirror annotationMirror) {
24-
assertNotNull(annotationMirror);
25-
this.annotationMirror = annotationMirror;
26-
}
21+
private final AnnotationValue target;
2722

28-
public static AnnotationAnnot newInstance(
29-
javax.lang.model.element.AnnotationMirror annotationMirror, Context ctx) {
30-
assertNotNull(annotationMirror);
31-
AnnotationAnnot result = new AnnotationAnnot(annotationMirror);
32-
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
33-
ctx.getElements().getElementValuesWithDefaults(annotationMirror).entrySet()) {
34-
String name = entry.getKey().getSimpleName().toString();
35-
AnnotationValue value = entry.getValue();
36-
if ("target".equals(name)) {
37-
result.target = value;
38-
} else if ("type".equals(name)) {
39-
result.type = value;
40-
} else if ("elements".equals(name)) {
41-
result.elements = value;
42-
}
43-
}
44-
return result;
45-
}
23+
private final AnnotationValue type;
24+
25+
private final AnnotationValue elements;
4626

47-
public javax.lang.model.element.AnnotationMirror getAnnotationMirror() {
48-
return annotationMirror;
27+
AnnotationAnnot(AnnotationMirror annotationMirror, Map<String, AnnotationValue> values) {
28+
super(annotationMirror);
29+
this.target = assertNonNullValue(values, TARGET);
30+
this.type = assertNonNullValue(values, TYPE);
31+
this.elements = assertNonNullValue(values, ELEMENTS);
4932
}
5033

5134
public AnnotationValue getTarget() {
@@ -63,23 +46,23 @@ public AnnotationValue getElements() {
6346
public VariableElement getTargetValue() {
6447
VariableElement value = AnnotationValueUtil.toEnumConstant(target);
6548
if (value == null) {
66-
throw new AptIllegalStateException("target");
49+
throw new AptIllegalStateException(TARGET);
6750
}
6851
return value;
6952
}
7053

7154
public TypeMirror getTypeValue() {
7255
TypeMirror value = AnnotationValueUtil.toType(type);
7356
if (value == null) {
74-
throw new AptIllegalStateException("type");
57+
throw new AptIllegalStateException(TYPE);
7558
}
7659
return value;
7760
}
7861

7962
public String getElementsValue() {
8063
String value = AnnotationValueUtil.toString(elements);
8164
if (value == null) {
82-
throw new AptIllegalStateException("elements");
65+
throw new AptIllegalStateException(ELEMENTS);
8366
}
8467
return value;
8568
}

0 commit comments

Comments
 (0)