/
ReflectExtensions.java
310 lines (292 loc) · 10.4 KB
/
ReflectExtensions.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
305
306
307
308
309
310
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.maven.docs.testing;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import com.google.common.collect.Iterables;
import org.eclipse.jdt.core.Flags;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.Functions;
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures;
import org.eclipse.xtext.xbase.lib.Pure;
import io.sarl.lang.annotation.DefaultValue;
import io.sarl.lang.annotation.SarlSourceCode;
import io.sarl.lang.annotation.SyntheticMember;
import io.sarl.lang.util.Utils;
/** Functions that are based on Java reflection, for building the documentation.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.7
*/
public final class ReflectExtensions {
private static Function<Method, String> defaultNameFormatter;
private ReflectExtensions() {
//
}
/** Change the default name formatter.
*
* @param formatter the default name formatter.
*/
public static void setDefaultNameFormatter(Function<Method, String> formatter) {
defaultNameFormatter = formatter;
}
/** Replies the default name formatter.
*
* @return the default name formatter.
*/
public static Function<Method, String> getDefaultNameFormatter() {
return defaultNameFormatter;
}
private static boolean isDeprecated(Method method) {
return Flags.isDeprecated(method.getModifiers()) || method.getAnnotation(Deprecated.class) != null;
}
/** Extract the public methods from the given types.
*
* @param type the first type to parse.
* @param otherTypes the other types to parse.
* @return the code.
*/
@Pure
@Inline(value = "getPublicMethodsWithFormat($1, null, $2)", imported = ReflectExtensions.class)
public static String getPublicMethods(Class<?> type, Class<?>... otherTypes) {
return getPublicMethodsWithFormat(type, null);
}
/** Extract the public methods from the given types.
*
* @param type the first type to parse.
* @param nameFormatter the formatter for the function's names. If {@code null}, no formatting is applied.
* @param otherTypes the other types to parse.
* @return the code.
*/
@Pure
public static String getPublicMethodsWithFormat(Class<?> type, Function<Method, String> nameFormatter, Class<?>... otherTypes) {
final StringBuilder it = new StringBuilder();
appendPublicMethods(it, false, nameFormatter, IterableExtensions.flatten(
Arrays.asList(
Collections.singletonList(type),
Arrays.asList(otherTypes))));
return it.toString();
}
/** Extract the public methods from the given types.
*
* @param it the output.
* @param indent indicates if the code should be indented.
* @param types the types to parse.
*/
@Inline(value = "appendPublicMethods($1, $2, null, $4.asList($3))", imported = Arrays.class)
public static void appendPublicMethods(StringBuilder it, boolean indent, Class<?>... types) {
appendPublicMethods(it, indent, null, Arrays.asList(types));
}
/** Extract the public methods from the given types.
*
* @param it the output.
* @param indent indicates if the code should be indented.
* @param nameFormatter the formatter for the function's names. If {@code null}, no formatting is applied.
* @param types the types to parse.
*/
public static void appendPublicMethods(StringBuilder it, boolean indent, Function<Method, String> nameFormatter,
Iterable<? extends Class<?>> types) {
final List<String> lines = new LinkedList<>();
for (final Class<?> type : types) {
for (final Method method : type.getDeclaredMethods()) {
if (Flags.isPublic(method.getModifiers()) && !Utils.isHiddenMember(method.getName())
&& !isDeprecated(method) && !method.isSynthetic()
&& method.getAnnotation(SyntheticMember.class) == null) {
final StringBuilder line = new StringBuilder();
if (indent) {
line.append("\t"); //$NON-NLS-1$
}
Function<Method, String> nformatter = nameFormatter;
if (nformatter == null) {
nformatter = getDefaultNameFormatter();
}
final String formattedName;
if (nformatter != null) {
formattedName = nformatter.apply(method);
} else {
formattedName = method.getName();
}
line.append("def ").append(formattedName); //$NON-NLS-1$
if (method.getParameterCount() > 0) {
line.append("("); //$NON-NLS-1$
boolean first = true;
int i = 1;
for (final Parameter param : method.getParameters()) {
if (first) {
first = false;
} else {
line.append(", "); //$NON-NLS-1$
}
//it.append(param.getName());
//it.append(" : "); //$NON-NLS-1$
toType(line, param.getParameterizedType(), method.isVarArgs() && i == method.getParameterCount());
final String defaultValue = extractDefaultValueString(param);
if (!Strings.isEmpty(defaultValue)) {
line.append(" = "); //$NON-NLS-1$
line.append(defaultValue);
}
}
line.append(")"); //$NON-NLS-1$
++i;
}
if (method.getGenericReturnType() != null && !Objects.equals(method.getGenericReturnType(), Void.class)
&& !Objects.equals(method.getGenericReturnType(), void.class)) {
line.append(" : "); //$NON-NLS-1$
toType(line, method.getGenericReturnType(), false);
}
line.append("\n"); //$NON-NLS-1$
lines.add(line.toString());
}
}
}
lines.sort(null);
for (final String line : lines) {
it.append(line);
}
}
private static String extractDefaultValueString(Parameter parameter) {
final DefaultValue defaultValueAnnotation = parameter.getAnnotation(DefaultValue.class);
if (defaultValueAnnotation == null) {
return null;
}
final String fieldId = defaultValueAnnotation.value();
if (!Strings.isEmpty(fieldId)) {
final Class<?> container = parameter.getDeclaringExecutable().getDeclaringClass();
if (container != null) {
final int index = fieldId.indexOf('#');
Class<?> target;
final String fieldName;
if (index > 0) {
try {
final Class<?> type = Class.forName(fieldId.substring(0, index), true, container.getClassLoader());
target = type;
} catch (Throwable exception) {
target = container;
}
fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId.substring(index + 1));
} else {
target = container;
fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId);
}
final Field field = Iterables.find(Arrays.asList(target.getDeclaredFields()),
(it) -> Strings.equal(it.getName(), fieldName),
null);
if (field != null) {
final SarlSourceCode sourceCodeAnnotation = parameter.getAnnotation(SarlSourceCode.class);
if (sourceCodeAnnotation != null) {
final String value = sourceCodeAnnotation.value();
if (!Strings.isEmpty(fieldId)) {
return value;
}
}
}
}
}
return null;
}
/** Extract the type name.
*
* @param it the output.
* @param otype the type to parse.
* @param isVarArg indicates if the type is used within a variadic parameter.
*/
@SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"})
public static void toType(StringBuilder it, Type otype, boolean isVarArg) {
final Type type;
if (otype instanceof Class<?>) {
type = isVarArg ? ((Class<?>) otype).getComponentType() : otype;
} else {
type = otype;
}
if (type instanceof Class<?>) {
it.append(((Class<?>) type).getSimpleName());
} else if (type instanceof ParameterizedType) {
final ParameterizedType paramType = (ParameterizedType) type;
final Type ownerType = paramType.getOwnerType();
final boolean isForFunction = ownerType != null && Functions.class.getName().equals(ownerType.getTypeName());
final boolean isForProcedure = ownerType != null && Procedures.class.getName().equals(ownerType.getTypeName());
if (!isForFunction && !isForProcedure) {
it.append(((Class<?>) paramType.getRawType()).getSimpleName());
if (paramType.getActualTypeArguments().length > 0) {
it.append("<"); //$NON-NLS-1$
boolean first = true;
for (final Type subtype : paramType.getActualTypeArguments()) {
if (first) {
first = false;
} else {
it.append(", "); //$NON-NLS-1$
}
final StringBuilder it2 = new StringBuilder();
toType(it2, subtype, false);
it.append(it2);
}
it.append(">"); //$NON-NLS-1$
}
} else {
int nb = paramType.getActualTypeArguments().length;
if (isForFunction) {
--nb;
}
it.append("("); //$NON-NLS-1$
for (int i = 0; i < nb; ++i) {
final Type subtype = paramType.getActualTypeArguments()[i];
if (i > 0) {
it.append(", "); //$NON-NLS-1$
}
toType(it, subtype, false);
}
it.append(") => "); //$NON-NLS-1$
if (isForFunction) {
toType(it, paramType.getActualTypeArguments()[nb], false);
} else {
it.append("void"); //$NON-NLS-1$
}
}
} else if (type instanceof WildcardType) {
final Type[] types = ((WildcardType) type).getUpperBounds();
toType(it, types[0], false);
} else if (type instanceof GenericArrayType) {
toType(it, ((GenericArrayType) type).getGenericComponentType(), false);
it.append("[]"); //$NON-NLS-1$
} else {
it.append(Object.class.getSimpleName());
}
if (isVarArg) {
it.append("*"); //$NON-NLS-1$
}
}
}