forked from EB-wilson/JavaDynamilizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionType.java
More file actions
185 lines (146 loc) · 4.94 KB
/
Copy pathFunctionType.java
File metadata and controls
185 lines (146 loc) · 4.94 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
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
package dynamilize;
import dynamilize.classmaker.ClassInfo;
import java.lang.invoke.MethodType;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class FunctionType{
public static int MAX_RECYCLE = 2048;
private static final Class<?>[] EMPTY = new Class[0];
private static final Stack<FunctionType> RECYCLE_POOL = new Stack<>();
private Class<?>[] paramType;
private int hash;
private FunctionType(Class<?>... paramType){
this.paramType = paramType;
hash = Arrays.hashCode(paramType);
}
//此类存在频繁的调用,数据量小,使用流处理数据会产生不必要的性能花销,使用for遍历取代流处理
public static Class<?>[] wrapper(Class<?>... clazz){
for(int i = 0; i < clazz.length; i++){
clazz[i] = wrapper(clazz[i]);
}
return clazz;
}
public static Class<?>[] unwrapped(Class<?>... clazz){
for(int i = 0; i < clazz.length; i++){
clazz[i] = unwrapped(clazz[i]);
}
return clazz;
}
public static Class<?> wrapper(Class<?> clazz){
if(clazz == int.class) return Integer.class;
if(clazz == float.class) return Float.class;
if(clazz == long.class) return Long.class;
if(clazz == double.class) return Double.class;
if(clazz == byte.class) return Byte.class;
if(clazz == short.class) return Short.class;
if(clazz == boolean.class) return Boolean.class;
if(clazz == char.class) return Character.class;
return clazz;
}
public static Class<?> unwrapped(Class<?> clazz){
if(clazz == Integer.class) return int.class;
if(clazz == Float.class) return float.class;
if(clazz == Long.class) return long.class;
if(clazz == Double.class) return double.class;
if(clazz == Byte.class) return byte.class;
if(clazz == Short.class) return short.class;
if(clazz == Boolean.class) return boolean.class;
if(clazz == Character.class) return char.class;
return clazz;
}
public static FunctionType inst(List<Class<?>> paramType){
return inst(paramType.toArray(new Class[0]));
}
public static synchronized FunctionType inst(Class<?>... paramType){
if(RECYCLE_POOL.isEmpty()) return new FunctionType(paramType);
FunctionType res = RECYCLE_POOL.pop();
res.paramType = paramType;
res.hash = Arrays.hashCode(paramType);
return res;
}
public static FunctionType inst(Object... param){
return inst(unwrapped(toTypes(param)));
}
public static Class<?>[] toTypes(Object... objects){
Class<?>[] types = new Class[objects.length];
for(int i = 0; i < types.length; i++){
types[i] = objects[i] == null? void.class: objects[i].getClass();
}
return types;
}
public static Class<?>[] toTypes(List<?> objects){
Class<?>[] types = new Class[objects.size()];
for(int i = 0; i < types.length; i++){
types[i] = objects.get(i) == null? void.class: objects.get(i).getClass();
}
return types;
}
public static FunctionType inst(FunctionType type){
return inst(type.paramType);
}
public static FunctionType from(MethodType type){
return inst(type.parameterArray());
}
public static FunctionType from(Executable method){
return inst(method.getParameterTypes());
}
public static FunctionType generic(int argCount){
Class<?>[] argTypes = new Class[argCount];
Arrays.fill(argTypes, void.class);
return inst(argTypes);
}
public static String signature(Method method){
return method.getName() + FunctionType.from(method);
}
public static String signature(String name, Class<?>... argTypes){
return name + FunctionType.inst(argTypes);
}
public static String signature(String name, FunctionType type){
return name + type;
}
public static int typeNameHash(Class<?>[] types){
return Arrays.hashCode(Arrays.stream(types).map(Class::getName).toArray());
}
public boolean match(Object... args){
return match(unwrapped(toTypes(args)));
}
public boolean match(Class<?>... argsType){
if(argsType.length != paramType.length) return false;
for(int i = 0; i < paramType.length; i++){
if(argsType[i] == void.class) continue;
if(!paramType[i].isAssignableFrom(argsType[i])) return false;
}
return true;
}
public Class<?>[] getTypes(){
return paramType;
}
public void recycle(){
if(RECYCLE_POOL.size() >= MAX_RECYCLE) return;
paramType = EMPTY;
hash = -1;
RECYCLE_POOL.push(this);
}
@Override
public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof FunctionType that)) return false;
return paramType.length == that.paramType.length && hashCode() == o.hashCode();
}
@Override
public int hashCode(){
return hash;
}
@Override
public String toString(){
StringBuilder b = new StringBuilder("(");
for(Class<?> clazz: paramType){
b.append(ClassInfo.asType(clazz).realName());
}
b.append(")");
return b.toString();
}
}