Skip to content

Commit d216a81

Browse files
committed
move generics impl types
1 parent b493b52 commit d216a81

7 files changed

Lines changed: 144 additions & 135 deletions

File tree

src/main/java/com/jsoniter/Codegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static Type chooseImpl(Type type) {
145145
} else if (clazz == Set.class) {
146146
clazz = implClazz == null ? HashSet.class : implClazz;
147147
}
148-
return new ParameterizedTypeImpl(new Type[]{compType}, null, clazz);
148+
return GenericsHelper.createParameterizedType(new Type[]{compType}, null, clazz);
149149
}
150150
if (Map.class.isAssignableFrom(clazz)) {
151151
Type keyType = String.class;
@@ -167,13 +167,13 @@ private static Type chooseImpl(Type type) {
167167
keyType = String.class;
168168
}
169169
MapKeyDecoders.register(keyType);
170-
return new ParameterizedTypeImpl(new Type[]{keyType, valueType}, null, clazz);
170+
return GenericsHelper.createParameterizedType(new Type[]{keyType, valueType}, null, clazz);
171171
}
172172
if (implClazz != null) {
173173
if (typeArgs.length == 0) {
174174
return implClazz;
175175
} else {
176-
return new ParameterizedTypeImpl(typeArgs, null, implClazz);
176+
return GenericsHelper.createParameterizedType(typeArgs, null, implClazz);
177177
}
178178
}
179179
return type;

src/main/java/com/jsoniter/CodegenImplObjectStrict.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private static void appendSetExtraToKeyValueTypeWrappers(StringBuilder lines, Cl
155155

156156
private static void appendSetExtraProperteis(StringBuilder lines, ClassDescriptor desc) {
157157
Binding onExtraProperties = desc.onExtraProperties;
158-
if (ParameterizedTypeImpl.isSameClass(onExtraProperties.valueType, Map.class)) {
158+
if (GenericsHelper.isSameClass(onExtraProperties.valueType, Map.class)) {
159159
if (onExtraProperties.field != null) {
160160
append(lines, String.format("obj.%s = extra;", onExtraProperties.field.getName()));
161161
} else {

src/main/java/com/jsoniter/annotation/JsoniterConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private void updateBindingWithJsonProperty(Binding binding, JsonProperty jsonPro
285285
}
286286
}
287287
if (jsonProperty.implementation() != Object.class) {
288-
binding.valueType = ParameterizedTypeImpl.useImpl(binding.valueType, jsonProperty.implementation());
288+
binding.valueType = GenericsHelper.useImpl(binding.valueType, jsonProperty.implementation());
289289
binding.valueTypeLiteral = TypeLiteral.create(binding.valueType);
290290
}
291291
}

src/main/java/com/jsoniter/spi/Binding.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ private static Type substituteTypeVariables(Map<String, Type> lookup, Type type)
5858
for (int i = 0; i < args.length; i++) {
5959
args[i] = substituteTypeVariables(lookup, args[i]);
6060
}
61-
return new ParameterizedTypeImpl(args, pType.getOwnerType(), pType.getRawType());
61+
return GenericsHelper.createParameterizedType(args, pType.getOwnerType(), pType.getRawType());
6262
}
6363
if (type instanceof GenericArrayType) {
6464
GenericArrayType gaType = (GenericArrayType) type;
65-
return new GenericArrayTypeImpl(substituteTypeVariables(lookup, gaType.getGenericComponentType()));
65+
Type componentType = substituteTypeVariables(lookup, gaType.getGenericComponentType());
66+
return GenericsHelper.createGenericArrayType(componentType);
6667
}
6768
return type;
6869
}

src/main/java/com/jsoniter/spi/GenericArrayTypeImpl.java

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.jsoniter.spi;
2+
3+
import java.lang.reflect.GenericArrayType;
4+
import java.lang.reflect.ParameterizedType;
5+
import java.lang.reflect.Type;
6+
import java.util.Arrays;
7+
8+
public class GenericsHelper {
9+
10+
public static GenericArrayType createGenericArrayType(Type componentType) {
11+
return new GenericArrayTypeImpl(componentType);
12+
}
13+
14+
public static ParameterizedType createParameterizedType(Type[] actualTypeArguments, Type ownerType, Type rawType) {
15+
return new ParameterizedTypeImpl(actualTypeArguments, ownerType, rawType);
16+
}
17+
18+
public static boolean isSameClass(Type type, Class clazz) {
19+
if (type == clazz) {
20+
return true;
21+
}
22+
if (type instanceof ParameterizedType) {
23+
ParameterizedType pType = (ParameterizedType) type;
24+
return pType.getRawType() == clazz;
25+
}
26+
return false;
27+
}
28+
29+
public static Type useImpl(Type type, Class clazz) {
30+
if (type instanceof Class) {
31+
return clazz;
32+
}
33+
if (type instanceof ParameterizedType) {
34+
ParameterizedType pType = (ParameterizedType) type;
35+
return createParameterizedType(pType.getActualTypeArguments(), pType.getOwnerType(), clazz);
36+
}
37+
throw new JsonException("can not change impl for: " + type);
38+
}
39+
40+
private static class GenericArrayTypeImpl implements GenericArrayType {
41+
42+
private final Type componentType;
43+
44+
GenericArrayTypeImpl(Type componentType) {
45+
this.componentType = componentType;
46+
}
47+
48+
@Override
49+
public Type getGenericComponentType() {
50+
return componentType;
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (o == null || getClass() != o.getClass()) return false;
57+
58+
GenericArrayTypeImpl that = (GenericArrayTypeImpl) o;
59+
60+
return componentType != null ? componentType.equals(that.componentType) : that.componentType == null;
61+
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return componentType != null ? componentType.hashCode() : 0;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "GenericArrayTypeImpl{" +
72+
"componentType=" + componentType +
73+
'}';
74+
}
75+
}
76+
77+
private static class ParameterizedTypeImpl implements ParameterizedType {
78+
private final Type[] actualTypeArguments;
79+
private final Type ownerType;
80+
private final Type rawType;
81+
82+
public ParameterizedTypeImpl(Type[] actualTypeArguments, Type ownerType, Type rawType){
83+
this.actualTypeArguments = actualTypeArguments;
84+
this.ownerType = ownerType;
85+
this.rawType = rawType;
86+
}
87+
88+
public Type[] getActualTypeArguments() {
89+
return actualTypeArguments;
90+
}
91+
92+
public Type getOwnerType() {
93+
return ownerType;
94+
}
95+
96+
public Type getRawType() {
97+
return rawType;
98+
}
99+
100+
@Override
101+
public boolean equals(Object o) {
102+
if (this == o) return true;
103+
if (o == null || getClass() != o.getClass()) return false;
104+
105+
ParameterizedTypeImpl that = (ParameterizedTypeImpl) o;
106+
107+
// Probably incorrect - comparing Object[] arrays with Arrays.equals
108+
if (!Arrays.equals(actualTypeArguments, that.actualTypeArguments)) return false;
109+
if (ownerType != null ? !ownerType.equals(that.ownerType) : that.ownerType != null) return false;
110+
return rawType != null ? rawType.equals(that.rawType) : that.rawType == null;
111+
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
int result = Arrays.hashCode(actualTypeArguments);
117+
result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0);
118+
result = 31 * result + (rawType != null ? rawType.hashCode() : 0);
119+
return result;
120+
}
121+
122+
@Override
123+
public String toString() {
124+
String rawTypeName = rawType.toString();
125+
if (rawType instanceof Class) {
126+
Class clazz = (Class) rawType;
127+
rawTypeName = clazz.getName();
128+
}
129+
return "ParameterizedTypeImpl{" +
130+
"actualTypeArguments=" + Arrays.toString(actualTypeArguments) +
131+
", ownerType=" + ownerType +
132+
", rawType=" + rawTypeName +
133+
'}';
134+
}
135+
}
136+
}

src/main/java/com/jsoniter/spi/ParameterizedTypeImpl.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)