forked from Chocohead/Fabric-ASM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumAdder.java
304 lines (269 loc) · 10.9 KB
/
EnumAdder.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2019 Chocohead
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.chocohead.mm.api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.objectweb.asm.Type;
/**
* Simple builder to add additional Enum entries repetitively using the given constructor
*
* @author Chocohead
*/
public final class EnumAdder {
/**
* Glorified Pair/Tuple implementation specifically for {@link EnumAdder}
*
* @author Chocohead
*/
public final class EnumAddition {
/** The name of the new entry */
public final String name;
/** The class name of the inner class provider for the new entry
* @since 1.9
*/
public final String structClass;
/** The factory which produces the parameters to construct the new entry with */
private final Supplier<Object[]> parameterFactory;
/**
* @param name The name of the new entry
* @param parameterFactory A factory which produces the parameters to construct the new entry with
*
* @since 2.0
*/
EnumAddition(String name, Supplier<Object[]> parameterFactory) {
this(name, null, parameterFactory);
}
/**
* @param name The name of the new entry
* @param structClass The class name of the inner class provider for the new entry, maybe be {@code null}
* @param parameterFactory A factory which produces the parameters to construct the new entry with
*
* @since 1.9
*/
EnumAddition(String name, String structClass, Supplier<Object[]> parameterFactory) {
this.name = name;
this.structClass = structClass;
this.parameterFactory = parameterFactory;
}
/**
* Uses {@link #parameterFactory} to produce the parameters to make the new entry
*
* @return The parameters as produced by the parameter factory
*
* @throws IllegalArgumentException If the factory produces an invalid parameter array
*/
public Object[] getParameters() {
return checkParameters(parameterFactory.get());
}
/**
* Whether the new entry will be created as a subclass of the enum target type
*
* @return Whether the new entry has an associated inner class provider
*
* @since 1.9
*/
public boolean isEnumSubclass() {
return structClass != null;
}
}
/** The name of the enum being added to */
public final String type;
/** The parameter types of the constructor being used */
public final Type[] parameterTypes;
/** The collection of entries to be added, in order of being declared */
private final List<EnumAddition> additions = new ArrayList<>();
/** Whether {@link #build()} has been called */
private boolean finished = false;
/**
* New {@link EnumAdder}s can be made via {@link ClassTinkerers#enumBuilder(String, Class...)}
*
* @param type The name of the enum to be extended
* @param parameterTypes The type of parameters the constructor to be used takes
*/
EnumAdder(String type, Class<?>... parameterTypes) {
this(type, Arrays.stream(parameterTypes).map(Type::getType).toArray(Type[]::new));
}
/**
* New {@link EnumAdder}s can be made via {@link ClassTinkerers#enumBuilder(String, String...)}
*
* @param type The name of the enum to be extended
* @param parameterTypes The names of the taken parameter types of the constructor being used
*
* @throws IllegalArgumentException If any of the given parameter type names are invalid
*/
EnumAdder(String type, String... parameterTypes) {
this(type, Arrays.stream(parameterTypes).map(Type::getType).toArray(Type[]::new));
}
/**
* New {@link EnumAdder}s can be made via {@link ClassTinkerers#enumBuilder(String, Object...)}
*
* @param enumType The name of the enum to be extended
* @param parameterTypes The type or name of parameters the constructor to be used takes
*
* @throws IllegalArgumentException If any of the given parameter types are invalid
*/
EnumAdder(String enumType, Object... parameterTypes) {
this(enumType, Arrays.stream(parameterTypes).map(type -> {
if (type.getClass() == Type.class) {
return (Type) type;
} else if (type.getClass() == String.class) {
return Type.getType(((String) type).replace('.', '/'));
} else if (type.getClass() == Class.class) {
if (((Class<?>) type).getName().startsWith("net.minecraft.")) {
throw new IllegalArgumentException("Early loaded " + ((Class<?>) type).getName());
}
return Type.getType((Class<?>) type);
} else {
throw new IllegalArgumentException("Unsure how to map parameter type " + type.getClass() + " (from " + type + ')');
}
}).toArray(Type[]::new));
}
private EnumAdder(String type, Type[] parameterTypes) {
this.type = type;
this.parameterTypes = parameterTypes;
}
/**
* Add an entry to the Enum with the given name, constructed using the given parameters
*
* @param name The name of the new entry, undetermined result if the name already exists
* @param parameters The parameters to construct the new entry with
* @return This object to chain with
*
* @throws NullPointerException If name or parameters are {@code null}
* @throws IllegalArgumentException If the length of parameters differs to that of {@link #parameterTypes}
* @throws IllegalStateException If {@link #build()} has already been called on this object
*/
public EnumAdder addEnum(String name, Object... parameters) {
if (finished) throw new IllegalStateException("Attempted to add onto a built EnumAdder");
if (name == null) throw new NullPointerException("Null name attempted to be added to " + type);
checkParameters(parameters);
additions.add(new EnumAddition(name, () -> parameters));
return this;
}
/**
* Add an entry to the Enum with the given name, constructed using the parameters supplied using the given factory
*
* @param name The name of the new entry, undetermined result if the name already exists
* @param parameterFactory The factory to produce the parameters to construct the new entry with
* @return This object to chain with
*
* @throws NullPointerException If name or parameterFactory are {@code null}
* @throws IllegalStateException If {@link #build()} has already been called on this object
*/
public EnumAdder addEnum(String name, Supplier<Object[]> parameterFactory) {
if (finished) throw new IllegalStateException("Attempted to add onto a built EnumAdder");
if (name == null) throw new NullPointerException("Null name attempted to be added to " + type);
if (parameterFactory == null)
throw new NullPointerException("Null parameter factory provided to make " + name + " of " + type);
additions.add(new EnumAddition(name, parameterFactory));
return this;
}
/**
* Add an entry to the Enum with the given name, constructed using the given parameters, as an enum subclass
*
* @param name The name of the new entry, undetermined result if the name already exists
* @param structClass The class which the subclass will be built from
* @param parameters The parameters to construct the new entry with
* @return This object to chain with
*
* @throws NullPointerException If name or parameters are {@code null}
* @throws IllegalArgumentException If the length of parameters differs to that of {@link #parameterTypes} or structClass is {@code null}
* @throws IllegalStateException If {@link #build()} has already been called on this object
*
* @since 1.9
*/
public EnumAdder addEnumSubclass(String name, String structClass, Object... parameters) {
if (finished) throw new IllegalStateException("Attempted to add onto a built EnumAdder");
if (name == null) throw new NullPointerException("Null name attempted to be added to " + type);
if (structClass == null) throw new IllegalArgumentException("Null structClass provided to make " + name + " of " + type);
checkParameters(parameters);
additions.add(new EnumAddition(name, structClass.replace('.', '/'), () -> parameters));
return this;
}
/**
* Add an entry to the Enum with the given name, constructed using the parameters supplied using the
* given factory, as an enum subclass
*
* @param name The name of the new entry, undetermined result if the name already exists
* @param structClass The class which the subclass will be built from
* @param parameterFactory The factory to produce the parameters to construct the new entry with
* @return This object to chain with
*
* @throws NullPointerException If name or parameterFactory are {@code null}
* @throws IllegalArgumentException If structClass is {@code null}
* @throws IllegalStateException If {@link #build()} has already been called on this object
*
* @since 1.9
*/
public EnumAdder addEnumSubclass(String name, String structClass, Supplier<Object[]> parameterFactory) {
if (finished) throw new IllegalStateException("Attempted to add onto a built EnumAdder");
if (name == null) throw new NullPointerException("Null name attempted to be added to " + type);
if (structClass == null) throw new IllegalArgumentException("Null structClass provided to make " + name + " of " + type);
if (parameterFactory == null)
throw new NullPointerException("Null parameter factory provided to make " + name + " of " + type);
additions.add(new EnumAddition(name, structClass.replace('.', '/'), parameterFactory));
return this;
}
/**
* Checks that the provided parameters array has the same length as {@link #parameterTypes}
*
* @param parameters The parameters used to construct the Enum entry
*
* @throws NullPointerException If parameters is {@code null}
* @throws IllegalArgumentException If the length of parameters is different to parameterTypes
*/
Object[] checkParameters(Object[] parameters) {
if (parameters.length != parameterTypes.length)
throw new IllegalArgumentException("Differing number of parameters provided for types expected");
return parameters;
}
/**
* Gets if there are any additional constructor parameters beyond the minimum of a name
*
* @return Whether {@link #parameterTypes} is non-empty
*
* @since 1.8
*/
public boolean hasParameters() {
return parameterTypes.length > 0;
}
/**
* Mark this as complete, registers the changes to actually be applied during class load
*/
public void build() {
ClassTinkerers.addEnum(this);
finished = true;
}
/**
* Get a read only view of the additions made via {@link #addEnum(String, Object...)}
*
* @return A read only view of {@link #additions}
*/
public Collection<EnumAddition> getAdditions() {
return Collections.unmodifiableCollection(additions);
}
/**
* Get whether any of the additions made will subclass the Enum
*
* @return If any of the additions return {@code true} for {@link EnumAddition#isEnumSubclass()}
*
* @since 1.9
*/
public boolean willSubclass() {
for (EnumAddition addition : additions) {
if (addition.isEnumSubclass()) {
return true;
}
}
return false;
}
}