Skip to content

Commit 1b87233

Browse files
committed
Simplify Declaration classes
1 parent c64c031 commit 1b87233

File tree

8 files changed

+228
-255
lines changed

8 files changed

+228
-255
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
@@ -2,6 +2,7 @@
22

33
import javax.annotation.processing.ProcessingEnvironment;
44
import org.seasar.doma.internal.apt.annot.Annotations;
5+
import org.seasar.doma.internal.apt.decl.Declarations;
56

67
public class Context {
78

@@ -38,4 +39,8 @@ public Resources getResources() {
3839
public Annotations getAnnotations() {
3940
return new Annotations(this);
4041
}
42+
43+
public Declarations getDeclarations() {
44+
return new Declarations(this);
45+
}
4146
}
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
11
package org.seasar.doma.internal.apt.decl;
22

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

5-
import java.util.List;
6-
import javax.lang.model.element.ElementKind;
75
import javax.lang.model.element.ExecutableElement;
8-
import javax.lang.model.type.TypeMirror;
9-
import org.seasar.doma.internal.apt.Context;
106

117
public class ConstructorDeclaration {
128

13-
protected ExecutableElement element;
9+
private final ExecutableElement element;
1410

15-
protected List<TypeParameterDeclaration> typeParameterDeclarations;
16-
17-
protected Context ctx;
18-
19-
protected ConstructorDeclaration() {}
11+
ConstructorDeclaration(ExecutableElement element) {
12+
assertNotNull(element);
13+
this.element = element;
14+
}
2015

2116
public ExecutableElement getElement() {
2217
return element;
2318
}
24-
25-
public TypeDeclaration getTypeDeclaration() {
26-
TypeMirror returnType = resolveTypeParameter(element.asType());
27-
return TypeDeclaration.newTypeDeclaration(returnType, ctx);
28-
}
29-
30-
protected TypeMirror resolveTypeParameter(TypeMirror formalType) {
31-
for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) {
32-
if (formalType.equals(typeParameterDecl.getFormalType())) {
33-
return typeParameterDecl.getActualType();
34-
}
35-
}
36-
return formalType;
37-
}
38-
39-
public static ConstructorDeclaration newInstance(
40-
ExecutableElement constructorElement,
41-
List<TypeParameterDeclaration> typeParameterDeclarations,
42-
Context ctx) {
43-
assertNotNull(constructorElement, typeParameterDeclarations, ctx);
44-
assertTrue(constructorElement.getKind() == ElementKind.CONSTRUCTOR);
45-
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration();
46-
constructorDeclaration.element = constructorElement;
47-
constructorDeclaration.typeParameterDeclarations = typeParameterDeclarations;
48-
constructorDeclaration.ctx = ctx;
49-
return constructorDeclaration;
50-
}
5119
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package org.seasar.doma.internal.apt.decl;
2+
3+
import static org.seasar.doma.internal.util.AssertionUtil.assertNotNull;
4+
import static org.seasar.doma.internal.util.AssertionUtil.assertTrue;
5+
6+
import java.util.*;
7+
import javax.lang.model.element.*;
8+
import javax.lang.model.type.DeclaredType;
9+
import javax.lang.model.type.TypeKind;
10+
import javax.lang.model.type.TypeMirror;
11+
import org.seasar.doma.internal.apt.Context;
12+
13+
public class Declarations {
14+
15+
private final Context ctx;
16+
17+
public Declarations(Context ctx) {
18+
assertNotNull(ctx);
19+
this.ctx = ctx;
20+
}
21+
22+
public TypeDeclaration newUnknownTypeDeclaration() {
23+
TypeMirror type = ctx.getTypes().getNoType(TypeKind.NONE);
24+
return newTypeDeclaration(type);
25+
}
26+
27+
public TypeDeclaration newBooleanTypeDeclaration() {
28+
TypeMirror type = ctx.getTypes().getTypeMirror(boolean.class);
29+
return newTypeDeclaration(type);
30+
}
31+
32+
public TypeDeclaration newTypeDeclaration(Class<?> clazz) {
33+
assertNotNull(clazz);
34+
TypeMirror type = ctx.getTypes().getTypeMirror(clazz);
35+
return newTypeDeclaration(type);
36+
}
37+
38+
public TypeDeclaration newTypeDeclaration(TypeMirror type) {
39+
assertNotNull(type);
40+
TypeElement typeElement = ctx.getTypes().toTypeElement(type);
41+
Map<String, List<TypeParameterDeclaration>> map = new LinkedHashMap<>();
42+
gatherTypeParameterDeclarations(type, map);
43+
return new TypeDeclaration(ctx, type, typeElement, map);
44+
}
45+
46+
private void gatherTypeParameterDeclarations(
47+
TypeMirror type, Map<String, List<TypeParameterDeclaration>> typeParameterDeclarationsMap) {
48+
TypeElement typeElement = ctx.getTypes().toTypeElement(type);
49+
if (typeElement == null) {
50+
return;
51+
}
52+
String binaryName = ctx.getElements().getBinaryNameAsString(typeElement);
53+
typeParameterDeclarationsMap.put(
54+
binaryName, createTypeParameterDeclarations(typeElement, type));
55+
for (TypeMirror superType : ctx.getTypes().directSupertypes(type)) {
56+
TypeElement superElement = ctx.getTypes().toTypeElement(superType);
57+
if (superElement == null) {
58+
continue;
59+
}
60+
String superBinaryName = ctx.getElements().getBinaryNameAsString(superElement);
61+
if (typeParameterDeclarationsMap.containsKey(superBinaryName)) {
62+
continue;
63+
}
64+
typeParameterDeclarationsMap.put(
65+
superBinaryName, createTypeParameterDeclarations(superElement, superType));
66+
gatherTypeParameterDeclarations(superType, typeParameterDeclarationsMap);
67+
}
68+
}
69+
70+
private List<TypeParameterDeclaration> createTypeParameterDeclarations(
71+
TypeElement typeElement, TypeMirror type) {
72+
assertNotNull(typeElement, type);
73+
List<TypeParameterDeclaration> list = new ArrayList<>();
74+
Iterator<? extends TypeParameterElement> formalParams =
75+
typeElement.getTypeParameters().iterator();
76+
DeclaredType declaredType = ctx.getTypes().toDeclaredType(type);
77+
Iterator<? extends TypeMirror> actualParams = declaredType.getTypeArguments().iterator();
78+
for (; formalParams.hasNext() && actualParams.hasNext(); ) {
79+
TypeMirror formalType = formalParams.next().asType();
80+
TypeMirror actualType = actualParams.next();
81+
TypeParameterDeclaration typeParameterDeclaration =
82+
ctx.getDeclarations().newTypeParameterDeclaration(formalType, actualType);
83+
list.add(typeParameterDeclaration);
84+
}
85+
return Collections.unmodifiableList(list);
86+
}
87+
88+
FieldDeclaration newFieldDeclaration(
89+
VariableElement fieldElement, List<TypeParameterDeclaration> typeParameterDeclarations) {
90+
assertNotNull(fieldElement, typeParameterDeclarations);
91+
assertTrue(
92+
fieldElement.getKind() == ElementKind.FIELD
93+
|| fieldElement.getKind() == ElementKind.ENUM_CONSTANT,
94+
fieldElement.getKind().toString());
95+
TypeMirror fieldType = resolveTypeParameter(fieldElement.asType(), typeParameterDeclarations);
96+
TypeDeclaration typeDeclaration = newTypeDeclaration(fieldType);
97+
return new FieldDeclaration(fieldElement, typeDeclaration);
98+
}
99+
100+
ConstructorDeclaration newConstructorDeclaration(
101+
ExecutableElement constructorElement,
102+
List<TypeParameterDeclaration> typeParameterDeclarations) {
103+
assertNotNull(constructorElement, typeParameterDeclarations);
104+
assertTrue(constructorElement.getKind() == ElementKind.CONSTRUCTOR);
105+
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(constructorElement);
106+
return constructorDeclaration;
107+
}
108+
109+
MethodDeclaration newMethodDeclaration(
110+
ExecutableElement methodElement, List<TypeParameterDeclaration> typeParameterDeclarations) {
111+
assertNotNull(methodElement, typeParameterDeclarations);
112+
assertTrue(methodElement.getKind() == ElementKind.METHOD);
113+
TypeMirror returnTypeMirror =
114+
resolveTypeParameter(methodElement.getReturnType(), typeParameterDeclarations);
115+
TypeDeclaration returnTypeDeclaration = newTypeDeclaration(returnTypeMirror);
116+
return new MethodDeclaration(methodElement, returnTypeDeclaration);
117+
}
118+
119+
private TypeParameterDeclaration newTypeParameterDeclaration(
120+
TypeMirror formalType, TypeMirror actualType) {
121+
assertNotNull(formalType, actualType);
122+
TypeParameterDeclaration typeParameterDeclaration =
123+
new TypeParameterDeclaration(formalType, actualType);
124+
return typeParameterDeclaration;
125+
}
126+
127+
private TypeMirror resolveTypeParameter(
128+
TypeMirror formalType, List<TypeParameterDeclaration> typeParameterDeclarations) {
129+
for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) {
130+
if (formalType.equals(typeParameterDecl.getFormalType())) {
131+
return typeParameterDecl.getActualType();
132+
}
133+
}
134+
return formalType;
135+
}
136+
}

src/main/java/org/seasar/doma/internal/apt/decl/FieldDeclaration.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,25 @@
22

33
import static org.seasar.doma.internal.util.AssertionUtil.*;
44

5-
import java.util.List;
6-
import javax.lang.model.element.ElementKind;
75
import javax.lang.model.element.VariableElement;
8-
import javax.lang.model.type.TypeMirror;
9-
import org.seasar.doma.internal.apt.Context;
106

117
public class FieldDeclaration {
128

13-
protected VariableElement element;
9+
private final VariableElement element;
1410

15-
protected List<TypeParameterDeclaration> typeParameterDeclarations;
11+
private final TypeDeclaration typeDeclaration;
1612

17-
protected Context ctx;
13+
FieldDeclaration(VariableElement element, TypeDeclaration typeDeclaration) {
14+
assertNotNull(element, typeDeclaration);
15+
this.element = element;
16+
this.typeDeclaration = typeDeclaration;
17+
}
1818

1919
public VariableElement getElement() {
2020
return element;
2121
}
2222

2323
public TypeDeclaration getTypeDeclaration() {
24-
TypeMirror fieldType = resolveTypeParameter(element.asType());
25-
return TypeDeclaration.newTypeDeclaration(fieldType, ctx);
26-
}
27-
28-
protected TypeMirror resolveTypeParameter(TypeMirror formalType) {
29-
for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) {
30-
if (formalType.equals(typeParameterDecl.getFormalType())) {
31-
return typeParameterDecl.getActualType();
32-
}
33-
}
34-
return formalType;
35-
}
36-
37-
public static FieldDeclaration newInstance(
38-
VariableElement fieldElement,
39-
List<TypeParameterDeclaration> typeParameterDeclarations,
40-
Context ctx) {
41-
assertNotNull(fieldElement, typeParameterDeclarations, ctx);
42-
assertTrue(
43-
fieldElement.getKind() == ElementKind.FIELD
44-
|| fieldElement.getKind() == ElementKind.ENUM_CONSTANT,
45-
fieldElement.getKind().toString());
46-
FieldDeclaration fieldDeclaration = new FieldDeclaration();
47-
fieldDeclaration.element = fieldElement;
48-
fieldDeclaration.typeParameterDeclarations = typeParameterDeclarations;
49-
fieldDeclaration.ctx = ctx;
50-
return fieldDeclaration;
24+
return typeDeclaration;
5125
}
5226
}

src/main/java/org/seasar/doma/internal/apt/decl/MethodDeclaration.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,30 @@
22

33
import static org.seasar.doma.internal.util.AssertionUtil.*;
44

5-
import java.util.List;
6-
import javax.lang.model.element.ElementKind;
75
import javax.lang.model.element.ExecutableElement;
86
import javax.lang.model.element.Modifier;
9-
import javax.lang.model.type.TypeMirror;
10-
import org.seasar.doma.internal.apt.Context;
117

128
public class MethodDeclaration {
139

14-
protected ExecutableElement element;
10+
private final ExecutableElement element;
1511

16-
protected List<TypeParameterDeclaration> typeParameterDeclarations;
12+
private final TypeDeclaration returnTypeDeclaration;
1713

18-
protected Context ctx;
19-
20-
protected MethodDeclaration() {}
14+
MethodDeclaration(ExecutableElement element, TypeDeclaration returnTypeDeclaration) {
15+
assertNotNull(element, returnTypeDeclaration);
16+
this.element = element;
17+
this.returnTypeDeclaration = returnTypeDeclaration;
18+
}
2119

2220
public ExecutableElement getElement() {
2321
return element;
2422
}
2523

2624
public TypeDeclaration getReturnTypeDeclaration() {
27-
TypeMirror returnType = resolveTypeParameter(element.getReturnType());
28-
return TypeDeclaration.newTypeDeclaration(returnType, ctx);
25+
return returnTypeDeclaration;
2926
}
3027

3128
public boolean isStatic() {
3229
return element.getModifiers().contains(Modifier.STATIC);
3330
}
34-
35-
protected TypeMirror resolveTypeParameter(TypeMirror formalType) {
36-
for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) {
37-
if (formalType.equals(typeParameterDecl.getFormalType())) {
38-
return typeParameterDecl.getActualType();
39-
}
40-
}
41-
return formalType;
42-
}
43-
44-
public static MethodDeclaration newInstance(
45-
ExecutableElement methodElement,
46-
List<TypeParameterDeclaration> typeParameterDeclarations,
47-
Context ctx) {
48-
assertNotNull(methodElement, typeParameterDeclarations, ctx);
49-
assertTrue(methodElement.getKind() == ElementKind.METHOD);
50-
MethodDeclaration methodDeclaration = new MethodDeclaration();
51-
methodDeclaration.element = methodElement;
52-
methodDeclaration.typeParameterDeclarations = typeParameterDeclarations;
53-
methodDeclaration.ctx = ctx;
54-
return methodDeclaration;
55-
}
5631
}

0 commit comments

Comments
 (0)