forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericsHelper.java
More file actions
136 lines (109 loc) · 4.42 KB
/
GenericsHelper.java
File metadata and controls
136 lines (109 loc) · 4.42 KB
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
package com.jsoniter.spi;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
public class GenericsHelper {
public static GenericArrayType createGenericArrayType(Type componentType) {
return new GenericArrayTypeImpl(componentType);
}
public static ParameterizedType createParameterizedType(Type[] actualTypeArguments, Type ownerType, Type rawType) {
return new ParameterizedTypeImpl(actualTypeArguments, ownerType, rawType);
}
public static boolean isSameClass(Type type, Class clazz) {
if (type == clazz) {
return true;
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
return pType.getRawType() == clazz;
}
return false;
}
public static Type useImpl(Type type, Class clazz) {
if (type instanceof Class) {
return clazz;
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
return createParameterizedType(pType.getActualTypeArguments(), pType.getOwnerType(), clazz);
}
throw new JsonException("can not change impl for: " + type);
}
private static class GenericArrayTypeImpl implements GenericArrayType {
private final Type componentType;
GenericArrayTypeImpl(Type componentType) {
this.componentType = componentType;
}
@Override
public Type getGenericComponentType() {
return componentType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GenericArrayTypeImpl that = (GenericArrayTypeImpl) o;
return componentType != null ? componentType.equals(that.componentType) : that.componentType == null;
}
@Override
public int hashCode() {
return componentType != null ? componentType.hashCode() : 0;
}
@Override
public String toString() {
return "GenericArrayTypeImpl{" +
"componentType=" + componentType +
'}';
}
}
private static class ParameterizedTypeImpl implements ParameterizedType {
private final Type[] actualTypeArguments;
private final Type ownerType;
private final Type rawType;
public ParameterizedTypeImpl(Type[] actualTypeArguments, Type ownerType, Type rawType){
this.actualTypeArguments = actualTypeArguments;
this.ownerType = ownerType;
this.rawType = rawType;
}
public Type[] getActualTypeArguments() {
return actualTypeArguments;
}
public Type getOwnerType() {
return ownerType;
}
public Type getRawType() {
return rawType;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParameterizedTypeImpl that = (ParameterizedTypeImpl) o;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if (!Arrays.equals(actualTypeArguments, that.actualTypeArguments)) return false;
if (ownerType != null ? !ownerType.equals(that.ownerType) : that.ownerType != null) return false;
return rawType != null ? rawType.equals(that.rawType) : that.rawType == null;
}
@Override
public int hashCode() {
int result = Arrays.hashCode(actualTypeArguments);
result = 31 * result + (ownerType != null ? ownerType.hashCode() : 0);
result = 31 * result + (rawType != null ? rawType.hashCode() : 0);
return result;
}
@Override
public String toString() {
String rawTypeName = rawType.toString();
if (rawType instanceof Class) {
Class clazz = (Class) rawType;
rawTypeName = clazz.getName();
}
return "ParameterizedTypeImpl{" +
"actualTypeArguments=" + Arrays.toString(actualTypeArguments) +
", ownerType=" + ownerType +
", rawType=" + rawTypeName +
'}';
}
}
}