-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
RtHelper.java
195 lines (182 loc) · 5.74 KB
/
RtHelper.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
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.support.util;
import org.jspecify.annotations.Nullable;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtTypeReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* This class is a helper for runtime reflection.
*/
public abstract class RtHelper {
private RtHelper() {
}
/**
* Gets all the runtime fields for a given class (including the
* superclasses and superinterfaces).
*/
public static Field[] getAllFields(Class<?> c) {
List<Field> fields = new ArrayList<>();
addAllFields(c, fields);
Field[] result = new Field[fields.size()];
return fields.toArray(result);
}
private static void addAllFields(Class<?> c, List<Field> fields) {
if (c != null && c != Object.class) {
Collections.addAll(fields, c.getDeclaredFields());
addAllFields(c.getSuperclass(), fields);
for (Class<?> iface : c.getInterfaces()) {
addAllFields(iface, fields);
}
}
}
/**
* Gets all the field references for a given class (including the
* superclasses').
*/
public static Collection<CtFieldReference<?>> getAllFields(Class<?> c, Factory factory) {
Collection<CtFieldReference<?>> l = new ArrayList<>();
for (Field f : getAllFields(c)) {
l.add(factory.Field().createReference(f));
}
return l;
}
/**
* Gets all the runtime methods for a given class or interface (including
* the superclasses' or interfaces').
*/
public static Method[] getAllMethods(Class<?> c) {
List<Method> methods = new ArrayList<>();
if (c.isInterface()) {
getAllIMethods(c, methods);
} else {
while (c != null && c != Object.class) {
Collections.addAll(methods, c.getDeclaredMethods());
c = c.getSuperclass();
}
}
Method[] result = new Method[methods.size()];
return methods.toArray(result);
}
private static void getAllIMethods(Class<?> c, List<Method> methods) {
Collections.addAll(methods, c.getDeclaredMethods());
for (Class<?> i : c.getInterfaces()) {
getAllIMethods(i, methods);
}
}
/**
* Actually invokes from a compile-time invocation (by using runtime
* reflection).
*/
@SuppressWarnings("unchecked")
public static <T> T invoke(CtInvocation<T> i)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Object target = i.getTarget() == null ? null : ((CtLiteral<?>) i.getTarget()).getValue();
List<Object> args = new ArrayList<>();
for (CtExpression<?> e : i.getArguments()) {
args.add(((CtLiteral<?>) e).getValue());
}
Class<?> c = i.getExecutable().getDeclaringType().getActualClass();
ArrayList<Class<?>> argTypes = new ArrayList<>();
for (CtTypeReference<?> type : i.getExecutable().getActualTypeArguments()) {
argTypes.add(type.getActualClass());
}
return (T) c.getMethod(i.getExecutable().getSimpleName(), argTypes.toArray(new Class[0]))
.invoke(target, args.toArray());
}
/**
* Return the set of modifiers defined by the modifiers integer
* (java.lang.reflect).
*/
public static Set<ModifierKind> getModifiers(int mod) {
Set<ModifierKind> set = new HashSet<>();
if (Modifier.isAbstract(mod)) {
set.add(ModifierKind.ABSTRACT);
}
if (Modifier.isFinal(mod)) {
set.add(ModifierKind.FINAL);
}
if (Modifier.isNative(mod)) {
set.add(ModifierKind.NATIVE);
}
if (Modifier.isPrivate(mod)) {
set.add(ModifierKind.PRIVATE);
}
if (Modifier.isProtected(mod)) {
set.add(ModifierKind.PROTECTED);
}
if (Modifier.isPublic(mod)) {
set.add(ModifierKind.PUBLIC);
}
if (Modifier.isStatic(mod)) {
set.add(ModifierKind.STATIC);
}
if (Modifier.isStrict(mod)) {
set.add(ModifierKind.STRICTFP);
}
if (Modifier.isSynchronized(mod)) {
set.add(ModifierKind.SYNCHRONIZED);
}
if (Modifier.isTransient(mod)) {
set.add(ModifierKind.TRANSIENT);
}
if (Modifier.isVolatile(mod)) {
set.add(ModifierKind.VOLATILE);
}
return set;
}
/**
* return all executables of this class
*/
public static Collection<CtExecutableReference<?>> getAllExecutables(Class<?> clazz, Factory factory) {
Collection<CtExecutableReference<?>> l = new ArrayList<>();
for (Method m : clazz.getDeclaredMethods()) {
l.add(factory.Method().createReference(m));
}
for (Constructor<?> c : clazz.getDeclaredConstructors()) {
l.add(factory.Constructor().createReference(c));
}
return l;
}
/**
* Looks for first public method of clazz (or any super class or super interface),
* whose name is equal to methodName and number of parameters is numParams
* @param clazz
* @param methodName
* @param numParams
* @return the found method or null
*/
public static @Nullable Method getMethod(Class<?> clazz, String methodName, int numParams) {
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (!method.isSynthetic() && method.getName().equals(methodName)) {
Class<?>[] params = method.getParameterTypes();
if (params.length == numParams) {
return method;
}
}
}
return null;
}
}