/
AttributeInfo.java
222 lines (209 loc) · 7.9 KB
/
AttributeInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.dslplatform.json.processor;
import com.dslplatform.json.CompiledJson;
import com.dslplatform.json.JsonAttribute;
import com.dslplatform.json.Nullable;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import java.util.*;
public class AttributeInfo {
public final String id;
public final String name;
@Nullable public final ExecutableElement readMethod;
@Nullable public final ExecutableElement writeMethod;
@Nullable public final VariableElement field;
@Nullable public final VariableElement argument;
public final TypeMirror type;
@Nullable public final AnnotationMirror annotation;
public final Element element;
public final boolean notNull;
public final boolean mandatory;
public final int index;
public final String alias;
public final boolean fullMatch;
public final CompiledJson.TypeSignature typeSignature;
public final JsonAttribute.IncludePolicy includeToMinimal;
public final ConverterInfo converter;
public final boolean isJsonObject;
public final List<String> alternativeNames = new ArrayList<String>();
public final String readProperty;
public final String typeName;
public final boolean isArray;
public final boolean isList;
public final boolean isSet;
public final boolean isMap;
public final boolean isGeneric;
public final LinkedHashSet<TypeMirror> usedTypes;
public final Map<String, Integer> typeVariablesIndex;
public final boolean containsStructOwnerType;
public AttributeInfo(
String name,
@Nullable ExecutableElement readMethod,
@Nullable ExecutableElement writeMethod,
@Nullable VariableElement field,
@Nullable VariableElement argument,
TypeMirror type,
boolean isList,
boolean isSet,
boolean isMap,
@Nullable AnnotationMirror annotation,
boolean notNull,
boolean mandatory,
final int index,
@Nullable String alias,
boolean fullMatch,
@Nullable CompiledJson.TypeSignature typeSignature,
JsonAttribute.IncludePolicy includeToMinimal,
@Nullable ConverterInfo converter,
boolean isJsonObject,
LinkedHashSet<TypeMirror> usedTypes,
Map<String, Integer> typeVariablesIndex,
Map<String, TypeMirror> genericSignatures,
boolean containsStructOwnerType) {
this.id = alias != null ? alias : name;
this.name = name;
this.readMethod = readMethod;
this.writeMethod = writeMethod;
this.field = field;
this.argument = argument;
this.element = field != null ? field : readMethod;
this.type = type;
this.annotation = annotation;
this.notNull = notNull;
this.mandatory = mandatory;
this.index = index;
this.alias = alias;
this.fullMatch = fullMatch;
this.typeSignature = typeSignature;
this.includeToMinimal = includeToMinimal;
this.converter = converter;
this.isJsonObject = isJsonObject;
this.typeName = createTypeSignature(type, usedTypes, genericSignatures);
this.readProperty = field != null ? field.getSimpleName().toString() : readMethod.getSimpleName() + "()";
this.isArray = type.getKind() == TypeKind.ARRAY;
this.isList = isList;
this.isSet = isSet;
this.isMap = isMap;
this.usedTypes = usedTypes;
this.typeVariablesIndex = typeVariablesIndex;
this.isGeneric = !typeVariablesIndex.isEmpty();
this.containsStructOwnerType = containsStructOwnerType;
}
public AttributeInfo asConcreteType(LinkedHashMap<String, TypeMirror> genericSignatures) {
TypeMirror concreteType = genericSignatures.get(this.type.toString());
if (concreteType == null) return null;
return new AttributeInfo(
this.name,
this.readMethod,
this.writeMethod,
this.field,
this.argument,
concreteType,
this.isList,
this.isSet,
this.isMap,
this.annotation,
this.notNull,
this.mandatory,
this.index,
this.alias,
this.fullMatch,
this.typeSignature,
this.includeToMinimal,
this.converter,
this.isJsonObject,
this.usedTypes,
this.typeVariablesIndex,
genericSignatures,
this.containsStructOwnerType
);
}
public boolean isEnum(Map<String, StructInfo> structs) {
StructInfo struct = typeName == null ? null : structs.get(typeName);
return struct != null && struct.type == ObjectType.ENUM;
}
private boolean canResolveCollection(String content, TypeSupport typeSupport, Map<String, StructInfo> structs) {
if (typeSupport.isSupported(content)) return true;
StructInfo target = structs.get(content);
return target != null && (target.hasKnownConversion() || !target.isParameterized && target.unknowns.isEmpty());
}
public boolean canReadInput() {
if (converter != null || isJsonObject) return true;
if (field != null || writeMethod != null || argument != null) return true;
if (readMethod != null && notNull) return isList || isSet;
return false;
}
public boolean canWriteOutput() {
if (converter != null || isJsonObject) return true;
return field != null || readMethod != null;
}
@Nullable
public List<String> collectionContent(TypeSupport typeSupport, Map<String, StructInfo> structs) {
if (isArray) {
String content = typeName.substring(0, typeName.length() - 2);
return canResolveCollection(content, typeSupport, structs) ? Collections.singletonList(content) : null;
} else if (isList || isSet) {
int ind = typeName.indexOf('<');
if (ind == -1) return null;
String content = typeName.substring(ind + 1, typeName.length() - 1);
return canResolveCollection(content, typeSupport, structs) ? Collections.singletonList(content) : null;
} else if (isMap) {
int indGen = typeName.indexOf('<');
if (indGen == -1) return null;
int indComma = typeName.indexOf(',', indGen + 1);
String content1 = typeName.substring(indGen + 1, indComma);
String content2 = typeName.substring(indComma + 1, typeName.length() - 1);
return canResolveCollection(content1, typeSupport, structs) && canResolveCollection(content2, typeSupport, structs)
? Arrays.asList(content1, content2)
: null;
}
return null;
}
static String createTypeSignature(
TypeMirror type,
LinkedHashSet<TypeMirror> usedTypes,
Map<String, TypeMirror> genericSignatures) {
if (usedTypes.isEmpty()) return Analysis.typeWithoutAnnotations(type.toString());
StringBuilder builder = new StringBuilder();
createTypeSignature(type, genericSignatures, builder);
return builder.toString();
}
String buildTypeName(TypeMirror type, Map<String, TypeMirror> genericSignatures) {
return createTypeSignature(type, usedTypes, genericSignatures);
}
private static void createTypeSignature(
TypeMirror type,
Map<String, TypeMirror> genericSignatures,
StringBuilder builder) {
String typeName = Analysis.typeWithoutAnnotations(type.toString());
if (type.getKind() == TypeKind.DECLARED) {
DeclaredType declaredType = (DeclaredType) type;
if (declaredType.getTypeArguments().isEmpty()) {
builder.append(typeName);
} else {
TypeElement typeElement = (TypeElement) declaredType.asElement();
builder.append(typeElement.getQualifiedName()).append("<");
for (TypeMirror typeArgument : declaredType.getTypeArguments()) {
createTypeSignature(typeArgument, genericSignatures, builder);
builder.append(",");
}
builder.setCharAt(builder.length() - 1, '>');
}
} else if (type.getKind() == TypeKind.ARRAY) {
ArrayType arrayType = (ArrayType) type;
createTypeSignature(arrayType.getComponentType(), genericSignatures, builder);
builder.append("[]");
} else if (type instanceof WildcardType) {
WildcardType wt = (WildcardType)type;
createTypeSignature(wt.getExtendsBound(), genericSignatures, builder);
} else if (type instanceof TypeVariable) {
TypeMirror mirror = genericSignatures.get(typeName);
if (mirror != null && mirror != type) {
createTypeSignature(mirror, genericSignatures, builder);
} else {
builder.append(typeName);
}
} else {
builder.append(typeName);
}
}
}