Skip to content

Commit 4f785c5

Browse files
committed
Simplify CtType classes
1 parent 1b87233 commit 4f785c5

Some content is hidden

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

44 files changed

+1238
-1379
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.cttype.CtTypes;
56
import org.seasar.doma.internal.apt.decl.Declarations;
67

78
public class Context {
@@ -43,4 +44,8 @@ public Annotations getAnnotations() {
4344
public Declarations getDeclarations() {
4445
return new Declarations(this);
4546
}
47+
48+
public CtTypes getCtTypes() {
49+
return new CtTypes(this);
50+
}
4651
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public Void visitNoTypeAsVoid(NoType t, StringBuilder p) {
238238
return null;
239239
}
240240

241+
@Override
242+
public Void visitNoTypeAsNone(NoType t, StringBuilder stringBuilder) {
243+
return null;
244+
}
245+
241246
public Void visitPrimitive(PrimitiveType t, StringBuilder p) {
242247
p.append(t.getKind().name().toLowerCase());
243248
return null;
@@ -331,6 +336,11 @@ public Void visitNoTypeAsVoid(NoType t, StringBuilder p) {
331336
return null;
332337
}
333338

339+
@Override
340+
public Void visitNoTypeAsNone(NoType t, StringBuilder stringBuilder) {
341+
return null;
342+
}
343+
334344
public Void visitPrimitive(PrimitiveType t, StringBuilder p) {
335345
if (p.length() == 0) {
336346
p.append(t);
@@ -517,6 +527,11 @@ public Void visitNoTypeAsVoid(NoType t, StringBuilder p) {
517527
return null;
518528
}
519529

530+
@Override
531+
public Void visitNoTypeAsNone(NoType t, StringBuilder stringBuilder) {
532+
return null;
533+
}
534+
520535
public Void visitPrimitive(PrimitiveType t, StringBuilder p) {
521536
p.append(t.getKind().name().toLowerCase());
522537
return null;

src/main/java/org/seasar/doma/internal/apt/cttype/AbstractCtType.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,43 @@
44

55
import javax.lang.model.element.ElementKind;
66
import javax.lang.model.element.TypeElement;
7+
import javax.lang.model.type.TypeKind;
78
import javax.lang.model.type.TypeMirror;
89
import org.seasar.doma.internal.apt.Context;
910
import org.seasar.doma.internal.apt.util.MetaUtil;
1011

1112
public abstract class AbstractCtType implements CtType {
1213

13-
protected final TypeMirror typeMirror;
14-
1514
protected final Context ctx;
1615

17-
protected final String typeName;
16+
protected final TypeMirror type;
17+
18+
private final String typeName;
1819

19-
protected final String boxedTypeName;
20+
private final String boxedTypeName;
2021

2122
protected final String metaTypeName;
2223

2324
protected final TypeElement typeElement;
2425

25-
protected final String packageName;
26+
private final String packageName;
2627

27-
protected final String packageExcludedBinaryName;
28+
private final String qualifiedName;
2829

29-
protected final String qualifiedName;
30-
31-
protected AbstractCtType(TypeMirror typeMirror, Context ctx) {
32-
assertNotNull(typeMirror, ctx);
33-
this.typeMirror = typeMirror;
30+
protected AbstractCtType(Context ctx, TypeMirror type) {
31+
assertNotNull(ctx, type);
3432
this.ctx = ctx;
35-
this.typeName = ctx.getTypes().getTypeName(typeMirror);
36-
this.boxedTypeName = ctx.getTypes().getBoxedTypeName(typeMirror);
37-
this.metaTypeName = getMetaTypeName(typeMirror, ctx);
38-
this.typeElement = ctx.getTypes().toTypeElement(typeMirror);
33+
this.type = type;
34+
this.typeName = ctx.getTypes().getTypeName(type);
35+
this.boxedTypeName = ctx.getTypes().getBoxedTypeName(type);
36+
this.metaTypeName = getMetaTypeName(type, ctx);
37+
this.typeElement = ctx.getTypes().toTypeElement(type);
3938
if (typeElement != null) {
4039
qualifiedName = typeElement.getQualifiedName().toString();
4140
packageName = ctx.getElements().getPackageName(typeElement);
42-
packageExcludedBinaryName = ctx.getElements().getPackageExcludedBinaryName(typeElement);
4341
} else {
4442
qualifiedName = typeName;
4543
packageName = "";
46-
packageExcludedBinaryName = typeName;
4744
}
4845
}
4946

@@ -66,8 +63,8 @@ private static String makeTypeParamDecl(String typeName) {
6663
}
6764

6865
@Override
69-
public TypeMirror getTypeMirror() {
70-
return typeMirror;
66+
public TypeMirror getType() {
67+
return type;
7168
}
7269

7370
@Override
@@ -100,18 +97,28 @@ public String getPackageName() {
10097
return packageName;
10198
}
10299

103-
@Override
104-
public String getPackageExcludedBinaryName() {
105-
return packageExcludedBinaryName;
106-
}
107-
108100
@Override
109101
public boolean isEnum() {
110102
return typeElement != null && typeElement.getKind() == ElementKind.ENUM;
111103
}
112104

113105
@Override
114106
public boolean isPrimitive() {
115-
return typeMirror.getKind().isPrimitive();
107+
return type.getKind().isPrimitive();
108+
}
109+
110+
@Override
111+
public boolean isNone() {
112+
return type.getKind() == TypeKind.NONE;
113+
}
114+
115+
@Override
116+
public boolean isWildcard() {
117+
return type.getKind() == TypeKind.WILDCARD;
118+
}
119+
120+
@Override
121+
public boolean isTypevar() {
122+
return type.getKind() == TypeKind.TYPEVAR;
116123
}
117124
}

src/main/java/org/seasar/doma/internal/apt/cttype/AnyCtType.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
public class AnyCtType extends AbstractCtType {
99

10-
public AnyCtType(TypeMirror type, Context ctx) {
11-
super(type, ctx);
12-
}
13-
14-
public static AnyCtType newInstance(TypeMirror type, Context ctx) {
15-
assertNotNull(type, ctx);
16-
return new AnyCtType(type, ctx);
10+
AnyCtType(Context ctx, TypeMirror type) {
11+
super(ctx, type);
1712
}
1813

1914
@Override

src/main/java/org/seasar/doma/internal/apt/cttype/BasicCtType.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package org.seasar.doma.internal.apt.cttype;
22

3-
import static org.seasar.doma.internal.util.AssertionUtil.*;
4-
53
import javax.lang.model.type.TypeMirror;
64
import org.seasar.doma.internal.apt.Context;
75

86
public class BasicCtType extends AbstractCtType {
97

10-
protected WrapperCtType wrapperCtType;
8+
private final WrapperCtType wrapperCtType;
119

12-
public BasicCtType(TypeMirror type, Context ctx) {
13-
super(type, ctx);
10+
BasicCtType(Context ctx, TypeMirror type, WrapperCtType wrapperCtType) {
11+
super(ctx, type);
12+
this.wrapperCtType = wrapperCtType;
1413
}
1514

1615
public WrapperCtType getWrapperCtType() {
1716
return wrapperCtType;
1817
}
1918

2019
public String getDefaultValue() {
21-
switch (typeMirror.getKind()) {
20+
switch (type.getKind()) {
2221
case BOOLEAN:
2322
return String.valueOf(false);
2423
case BYTE:
@@ -34,17 +33,6 @@ public String getDefaultValue() {
3433
}
3534
}
3635

37-
public static BasicCtType newInstance(TypeMirror type, Context ctx) {
38-
assertNotNull(type, ctx);
39-
BasicCtType basicCtType = new BasicCtType(type, ctx);
40-
WrapperCtType wrapperCtType = WrapperCtType.newInstance(basicCtType, ctx);
41-
if (wrapperCtType == null) {
42-
return null;
43-
}
44-
basicCtType.wrapperCtType = wrapperCtType;
45-
return basicCtType;
46-
}
47-
4836
@Override
4937
public <R, P, TH extends Throwable> R accept(CtTypeVisitor<R, P, TH> visitor, P p) throws TH {
5038
return visitor.visitBasicCtType(this, p);

src/main/java/org/seasar/doma/internal/apt/cttype/BiFunctionCtType.java

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22

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

5-
import java.util.List;
6-
import java.util.function.BiFunction;
7-
import javax.lang.model.type.DeclaredType;
8-
import javax.lang.model.type.TypeKind;
95
import javax.lang.model.type.TypeMirror;
106
import org.seasar.doma.internal.apt.Context;
117

128
public class BiFunctionCtType extends AbstractCtType {
139

14-
protected final boolean isRawType;
10+
private final CtType firstArgCtType;
1511

16-
protected CtType firstArgCtType;
12+
private final CtType secondArgCtType;
1713

18-
protected CtType secondArgCtType;
14+
private final CtType resultCtType;
1915

20-
protected AnyCtType resultCtType;
21-
22-
public BiFunctionCtType(TypeMirror type, Context ctx, boolean isRawType) {
23-
super(type, ctx);
24-
this.isRawType = isRawType;
16+
BiFunctionCtType(
17+
Context ctx,
18+
TypeMirror type,
19+
CtType firstArgCtType,
20+
CtType secondArgCtType,
21+
CtType resultCtType) {
22+
super(ctx, type);
23+
assertNotNull(firstArgCtType, secondArgCtType, resultCtType);
24+
this.firstArgCtType = firstArgCtType;
25+
this.secondArgCtType = secondArgCtType;
26+
this.resultCtType = resultCtType;
2527
}
2628

2729
public CtType getFirstArgCtType() {
@@ -32,68 +34,16 @@ public CtType getSecondArgCtType() {
3234
return secondArgCtType;
3335
}
3436

35-
public AnyCtType getResultCtType() {
37+
public CtType getResultCtType() {
3638
return resultCtType;
3739
}
3840

39-
public boolean isRawType() {
40-
return isRawType;
41-
}
42-
43-
public boolean isWildcardType() {
44-
return resultCtType.getTypeMirror() != null
45-
&& resultCtType.getTypeMirror().getKind() == TypeKind.WILDCARD
46-
|| firstArgCtType.getTypeMirror() != null
47-
&& firstArgCtType.getTypeMirror().getKind() == TypeKind.WILDCARD
48-
|| secondArgCtType.getTypeMirror() != null
49-
&& secondArgCtType.getTypeMirror().getKind() == TypeKind.WILDCARD;
50-
}
51-
52-
public static BiFunctionCtType newInstance(TypeMirror type, Context ctx) {
53-
assertNotNull(type, ctx);
54-
DeclaredType biFunctionDeclaredType = getBiFunctionDeclaredType(type, ctx);
55-
if (biFunctionDeclaredType == null) {
56-
return null;
57-
}
58-
59-
List<? extends TypeMirror> typeArguments = biFunctionDeclaredType.getTypeArguments();
60-
boolean isRawType = typeArguments.size() != 3;
61-
BiFunctionCtType biFunctionCtType = new BiFunctionCtType(type, ctx, isRawType);
62-
if (!isRawType) {
63-
TypeMirror firstArgTypeMirror = typeArguments.get(0);
64-
TypeMirror secondArgTypeMirror = typeArguments.get(1);
65-
TypeMirror resultTypeMirror = typeArguments.get(2);
66-
67-
biFunctionCtType.firstArgCtType = ConfigCtType.newInstance(firstArgTypeMirror, ctx);
68-
if (biFunctionCtType.firstArgCtType == null) {
69-
biFunctionCtType.firstArgCtType = AnyCtType.newInstance(firstArgTypeMirror, ctx);
70-
}
71-
72-
biFunctionCtType.secondArgCtType = PreparedSqlCtType.newInstance(secondArgTypeMirror, ctx);
73-
if (biFunctionCtType.secondArgCtType == null) {
74-
biFunctionCtType.secondArgCtType = AnyCtType.newInstance(secondArgTypeMirror, ctx);
75-
}
76-
77-
biFunctionCtType.resultCtType = AnyCtType.newInstance(resultTypeMirror, ctx);
78-
}
79-
80-
return biFunctionCtType;
41+
public boolean isRaw() {
42+
return firstArgCtType.isNone() || secondArgCtType.isNone() || resultCtType.isNone();
8143
}
8244

83-
protected static DeclaredType getBiFunctionDeclaredType(TypeMirror type, Context ctx) {
84-
if (ctx.getTypes().isSameType(type, BiFunction.class)) {
85-
return ctx.getTypes().toDeclaredType(type);
86-
}
87-
for (TypeMirror supertype : ctx.getTypes().directSupertypes(type)) {
88-
if (ctx.getTypes().isSameType(supertype, BiFunction.class)) {
89-
return ctx.getTypes().toDeclaredType(supertype);
90-
}
91-
DeclaredType result = getBiFunctionDeclaredType(supertype, ctx);
92-
if (result != null) {
93-
return result;
94-
}
95-
}
96-
return null;
45+
public boolean hasWildcard() {
46+
return firstArgCtType.isWildcard() || secondArgCtType.isWildcard() || resultCtType.isWildcard();
9747
}
9848

9949
@Override

0 commit comments

Comments
 (0)