-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
408 lines (369 loc) · 12 KB
/
Copy pathNode.java
File metadata and controls
408 lines (369 loc) · 12 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package jtree.nodes;
import static jtree.util.Utils.*;
import static lombok.AccessLevel.*;
import java.lang.reflect.*;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.text.StringEscapeUtils;
import jtree.util.Either;
import lombok.Getter;
import lombok.SneakyThrows;
public abstract class Node implements INode {
@Override
public abstract String toCode();
@Override
public abstract Node clone();
@SuppressWarnings("unchecked")
public static <T> Optional<T> clone(Optional<T> optional) {
return (Optional<T>)optional.map(Node::clone);
}
@SuppressWarnings("unchecked")
public static <T> List<T> clone(List<T> list) {
return (List<T>)list.stream().map(Node::clone).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
public static <F,S> Either<F,S> clone(Either<F,S> either) {
return (Either<F,S>)either.map(Node::clone, Node::clone);
}
private static <T> Object clone(T t) {
if(t instanceof Optional) {
return clone((Optional<?>)t);
} else if(t instanceof List) {
return clone((List<?>)t);
} else if(t instanceof INode) {
return ((INode)t).clone();
} else {
return t;
}
}
@SneakyThrows
private static Class<?> resolve(Type type) {
if(type instanceof Class) {
return (Class<?>)type;
} else if(type instanceof GenericArrayType) {
return Class.forName("[" + resolve(((GenericArrayType)type).getGenericComponentType()));
} else if(type instanceof ParameterizedType) {
return resolve(((ParameterizedType)type).getRawType());
} else if(type instanceof WildcardType) {
return resolve(((WildcardType)type).getUpperBounds()[0]);
} else if(type instanceof TypeVariable) {
return resolve(((TypeVariable<?>)type).getBounds()[0]);
} else {
throw new IllegalArgumentException("Don't know how to resolve " + type.getClass().getName() + " " + type.getTypeName());
}
}
private static Type resolveType(Type type) {
if(type instanceof TypeVariable) {
return resolveType(((TypeVariable<?>)type).getBounds()[0]);
} else if(type instanceof WildcardType) {
return resolveType(((WildcardType)type).getUpperBounds()[0]);
} else {
return type;
}
}
@SuppressWarnings("unchecked")
private static Function<?, String> selectToStringFuncFromType(Type type) {
if(type == null) {
return obj -> obj == null? "null" : ((Function<Object,String>)selectToStringFuncFromType(obj.getClass())).apply(obj);
}
type = resolveType(type);
Class<?> normalType = resolve(type);
if(List.class.isAssignableFrom(normalType)) {
Type componentType;
if(type instanceof ParameterizedType) {
componentType = ((ParameterizedType)type).getActualTypeArguments()[0];
} else {
componentType = null;
}
var componentToStringFunc = (Function<Object,String>)selectToStringFuncFromType(componentType);
return (List<?> list) -> {
var sb = new StringBuilder("[");
boolean first = true;
for(var elem : list) {
if(first) {
first = false;
} else {
sb.append(", ");
}
sb.append(componentToStringFunc.apply(elem));
}
sb.append(']');
return sb.toString();
};
} else if(Set.class.isAssignableFrom(normalType)) {
Type componentType;
if(type instanceof ParameterizedType) {
componentType = ((ParameterizedType)type).getActualTypeArguments()[0];
} else {
componentType = null;
}
var componentToStringFunc = (Function<Object,String>)selectToStringFuncFromType(componentType);
return (Set<?> list) -> {
var sb = new StringBuilder("{");
boolean first = true;
for(var elem : list) {
if(first) {
first = false;
} else {
sb.append(", ");
}
sb.append(componentToStringFunc.apply(elem));
}
sb.append('}');
return sb.toString();
};
} else if(normalType.isArray()) {
Type componentType;
if(type instanceof GenericArrayType) {
componentType = ((GenericArrayType)type).getGenericComponentType();
} else {
componentType = normalType.getComponentType();
}
var componentToStringFunc = (Function<Object,String>)selectToStringFuncFromType(componentType);
return (Object array) -> {
var sb = new StringBuilder("[");
int length = Array.getLength(array);
for(int i = 0; i < length; i++) {
if(i != 0) {
sb.append(", ");
}
sb.append(componentToStringFunc.apply(Array.get(array, i)));
}
sb.append(']');
return sb.toString();
};
} else if(Map.class.isAssignableFrom(normalType)) {
Type keyType, valueType;
if(type instanceof ParameterizedType) {
var typeArgs = ((ParameterizedType)type).getActualTypeArguments();
keyType = typeArgs[0];
valueType = typeArgs[1];
} else {
keyType = Object.class;
valueType = null;
}
Function<Object,String> keyToStringFunc, valueToStringFunc;
if(keyType == String.class) {
keyToStringFunc = obj -> {
String key = obj.toString();
if(key.matches("[\\w$]+")) {
return key;
} else {
return '"' + StringEscapeUtils.escapeJava(key) + '"';
}
};
} else {
keyToStringFunc = (Function<Object,String>)selectToStringFuncFromType(keyType);
}
valueToStringFunc = (Function<Object,String>)selectToStringFuncFromType(valueType);
return (Map<Object,Object> map) -> {
var sb = new StringBuilder("{");
boolean first = true;
for(var entry : map.entrySet()) {
if(first) {
first = false;
} else {
sb.append(", ");
}
sb.append(keyToStringFunc.apply(entry.getKey())).append("=").append(valueToStringFunc.apply(entry.getValue()));
}
sb.append("}");
return sb.toString();
};
} else if(normalType == Optional.class) {
Type componentType;
if(type instanceof ParameterizedType) {
componentType = ((ParameterizedType)type).getActualTypeArguments()[0];
} else {
componentType = null;
}
var valueToStringFunc = (Function<Object,String>)selectToStringFuncFromType(componentType);
return (Optional<?> opt) -> opt.map(value -> "Optional[" + valueToStringFunc.apply(value) + "]").orElse("Optional.empty()");
} else if(INode.class.isAssignableFrom(normalType)) {
return Object::toString;
} else if(CharSequence.class.isAssignableFrom(normalType)) {
return (CharSequence cseq) -> '"' + StringEscapeUtils.escapeJava(cseq.toString()) + '"';
} else if(normalType == char.class || normalType == Character.class) {
return (Character ch) -> "'" + (ch == '\''? "\\'" : StringEscapeUtils.escapeJava(ch.toString())) + "'";
} else if(normalType == byte.class || normalType == Byte.class) {
return (Byte b) -> "(byte)" + b;
} else if(normalType == short.class || normalType == Short.class) {
return (Short s) -> "(short)" + s;
} else if(normalType == long.class || normalType == Long.class) {
return (Long l) -> l + "L";
} else if(normalType == float.class || normalType == Float.class) {
return (Float f) -> f + "f";
} else {
return Object::toString;
}
}
private class ToStringFieldGetter<T> {
private final Method method;
@Getter
private final String name;
private final Function<? super T, String> toStringFunc;
@SuppressWarnings("unchecked")
protected ToStringFieldGetter(Method method) {
this.method = method;
String name = method.getName();
if(name.startsWith("get")) {
this.name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
} else {
this.name = name;
}
this.toStringFunc = (Function<? super T,String>)selectToStringFuncFromType(method.getReturnType());
}
@SuppressWarnings("unchecked")
@SneakyThrows
public T getValue() {
try {
return (T)method.invoke(Node.this);
} catch(InvocationTargetException e) {
throw e.getCause();
}
}
public String getValueString() {
return toStringFunc.apply(getValue());
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof ToStringFieldGetter && ((ToStringFieldGetter<?>)obj).name.equals(name);
}
@Override
public String toString() {
return getName() + "=" + getValueString();
}
}
@Getter(value = PRIVATE, lazy = true)
private final Set<ToStringFieldGetter<?>> fieldGetters = getFields();
private Set<ToStringFieldGetter<?>> getFields() {
var fields = new HashSet<ToStringFieldGetter<?>>();
findFields(fields, getClass());
return fields;
}
@SuppressWarnings({ "unchecked" })
private void findFields(HashSet<ToStringFieldGetter<?>> fields, Class<? extends Node> type) {
fields.addAll(Arrays.stream(type.getMethods())
.filter(method -> !java.lang.reflect.Modifier.isStatic(method.getModifiers())
&& method.getParameterCount() == 0
&& method.getReturnType() != void.class
&& !method.getName().equals("getClass")
&& method.getName().matches("(get|is|has)[A-Z]\\w*"))
.map(ToStringFieldGetter::new)
.collect(Collectors.toList()));
type = (Class<? extends Node>)type.getSuperclass();
if(type != Node.class) {
findFields(fields, type);
}
}
@Override
public String toString() {
Set<ToStringFieldGetter<?>> getters = getFieldGetters();
return getClass().getSimpleName() + "(" + join(", ", getters, ToStringFieldGetter::toString) + ")";
}
protected static Iterable<Integer> reverseRange(int end) {
return () -> new PrimitiveIterator.OfInt() {
int index = end-1;
@Override
public boolean hasNext() {
return index >= 0;
}
@Override
public int nextInt() {
return index--;
}
};
}
protected static Iterable<Integer> range(int end) {
return () -> new PrimitiveIterator.OfInt() {
int index = 0;
@Override
public boolean hasNext() {
return index < end;
}
@Override
public int nextInt() {
return index++;
}
};
}
@SuppressWarnings("unchecked")
protected static final <N extends INode, R extends INode> Consumer<R> cast(Consumer<N> consumer) {
return (Consumer<R>)consumer;
}
protected final <N extends INode> void visitList(TreeVisitor visitor, List<N> list) {
for(int i : reverseRange(list.size())) {
list.get(i).accept(visitor, this, (N node) -> {
if(node == null) {
list.remove(i);
} else {
list.set(i, node);
}
});
}
}
/*public Stream<Node> stream() {
var methods = Arrays.stream(getClass().getMethods());
return methods
.filter(method -> {
if(isStatic(method.getModifiers())) {
return false;
}
return true;
})
.map(method -> {
try {
return (Node)method.invoke(this);
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
}
@Override
public final Iterator<Node> iterator() {
return this.stream().iterator();
}
@SuppressWarnings({ "unchecked", "resource" })
protected static Stream<Node> stream(@NonNull Object... children) {
Stream<Node> stream = Stream.empty();
int start = 0;
for(int i = 0; i < children.length; i++) {
var child = children[i];
Objects.requireNonNull(child);
if(!(child instanceof Node)) {
if(child instanceof List) {
var list = (List<Node>)child;
for(Node child2 : list) {
Objects.requireNonNull(child2);
}
if(i != start) {
if(i == start+1) {
stream = Stream.concat(stream, Stream.of((Node)children[start]));
} else {
stream = Stream.concat(stream, (Stream<Node>)(Stream<?>)Arrays.stream(children, start, i));
}
}
stream = Stream.concat(stream, list.stream());
start = i+1;
} else {
throw new ClassCastException(child.getClass() + " cannot be cast to " + Node.class + " or " + List.class);
}
}
}
if(start != children.length) {
if(start+1 == children.length) {
stream = Stream.concat(stream, Stream.of((Node)children[start]));
} else {
stream = Stream.concat(stream, (Stream<Node>)(Stream<?>)Arrays.stream(children, start, children.length));
}
}
return stream;
}*/
}