-
Notifications
You must be signed in to change notification settings - Fork 82
/
StubCreator.java
506 lines (472 loc) · 17.3 KB
/
StubCreator.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*******************************************************************************
* Copyright (c) 2006, 2017 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.binary;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.internal.corext.refactoring.Checks;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.ui.util.Progress;
public class StubCreator {
/** The internal string buffer */
protected StringBuffer fBuffer;
/** Should stubs for private member be generated as well? */
protected final boolean fStubInvisible;
public StubCreator(final boolean stubInvisible) {
fStubInvisible= stubInvisible;
}
protected void appendEnumConstants(final IType type) throws JavaModelException {
final IField[] fields= type.getFields();
final List<IField> list= new ArrayList<>(fields.length);
for (IField field : fields) {
if (Flags.isEnum(field.getFlags()))
list.add(field);
}
for (int index= 0; index < list.size(); index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
fBuffer.append(list.get(index).getElementName());
}
fBuffer.append(";"); //$NON-NLS-1$
}
protected void appendExpression(final String signature) {
appendExpression(signature, false);
}
protected void appendExpression(String signature, boolean castNull) {
switch (signature.charAt(0)) {
case Signature.C_BOOLEAN:
fBuffer.append("false"); //$NON-NLS-1$
break;
case Signature.C_BYTE:
case Signature.C_CHAR:
case Signature.C_DOUBLE:
case Signature.C_FLOAT:
case Signature.C_INT:
case Signature.C_LONG:
case Signature.C_SHORT:
fBuffer.append("0"); //$NON-NLS-1$
break;
default:
if (castNull) {
fBuffer.append("("); //$NON-NLS-1$
fBuffer.append(Signature.toString(signature));
fBuffer.append(")"); //$NON-NLS-1$
}
fBuffer.append("null"); //$NON-NLS-1$
break;
}
}
protected void appendFieldDeclaration(final IField field) throws JavaModelException {
appendFlags(field);
fBuffer.append(" "); //$NON-NLS-1$
final String signature= field.getTypeSignature();
fBuffer.append(Signature.toString(signature));
fBuffer.append(" "); //$NON-NLS-1$
fBuffer.append(field.getElementName());
if (Flags.isFinal(field.getFlags())) {
fBuffer.append("="); //$NON-NLS-1$
appendExpression(signature);
}
fBuffer.append(";"); //$NON-NLS-1$
}
protected void appendFlags(final IMember member) throws JavaModelException {
if (member instanceof IAnnotatable) {
IAnnotation[] annotations= ((IAnnotatable) member).getAnnotations();
Set<IAnnotation> annotationSet= new HashSet<>();
for (IAnnotation annotation : annotations) {
if (!annotationSet.contains(annotation)) {
appendAnnotation(annotation);
annotationSet.add(annotation);
}
}
annotationSet.clear();
annotationSet= null;
}
int flags= member.getFlags();
final int kind= member.getElementType();
if (kind == IJavaElement.TYPE) {
flags&= ~Flags.AccSuper;
final IType type= (IType) member;
if (!type.isMember())
flags&= ~Flags.AccPrivate;
if (Flags.isEnum(flags))
flags&= ~Flags.AccAbstract;
}
if (Flags.isEnum(flags))
flags&= ~Flags.AccFinal;
if (kind == IJavaElement.METHOD) {
flags&= ~Flags.AccVarargs;
flags&= ~Flags.AccBridge;
}
if (flags != 0)
fBuffer.append(Flags.toString(flags));
}
private void appendAnnotation(IAnnotation annotation) throws JavaModelException {
String name= annotation.getElementName();
if (!fStubInvisible && name.startsWith("sun.") || name.startsWith("jdk.internal.")) //$NON-NLS-1$ //$NON-NLS-2$
return; // skip Sun-internal annotations
fBuffer.append('@');
fBuffer.append(name.replace('$', '.'));
fBuffer.append('(');
IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs();
for (IMemberValuePair pair : memberValuePairs) {
fBuffer.append(pair.getMemberName());
fBuffer.append('=');
appendAnnotationValue(pair.getValue(), pair.getValueKind());
fBuffer.append(',');
}
if (memberValuePairs.length > 0)
fBuffer.deleteCharAt(fBuffer.length() - 1);
fBuffer.append(')').append('\n');
}
private void appendAnnotationValue(Object value, int valueKind) throws JavaModelException {
if (value instanceof Object[]) {
Object[] objects= (Object[]) value;
fBuffer.append('{');
for (Object object : objects) {
appendAnnotationValue(object, valueKind);
fBuffer.append(',');
}
if (objects.length > 0)
fBuffer.deleteCharAt(fBuffer.length() - 1);
fBuffer.append('}');
} else {
switch (valueKind) {
case IMemberValuePair.K_ANNOTATION:
appendAnnotation((IAnnotation) value);
break;
case IMemberValuePair.K_STRING:
fBuffer.append('"').append(value).append('"');
break;
default:
fBuffer.append(value);
break;
}
}
}
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
for (IJavaElement childJavaElement : type.getChildren()) {
final IMember child= (IMember) childJavaElement;
final int flags= child.getFlags();
final boolean isPrivate= Flags.isPrivate(flags);
final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
if (child instanceof IType) {
if (stub || "java.lang.invoke.MethodHandle".equals(type.getFullyQualifiedName()) //$NON-NLS-1$
|| "java.util.concurrent.ConcurrentHashMap$CollectionView".equals(((IType) child).getFullyQualifiedName())) //$NON-NLS-1$
appendTypeDeclaration((IType) child, Progress.subMonitor(monitor, 1));
} else if (child instanceof IField) {
if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
appendFieldDeclaration((IField) child);
} else if (child instanceof IMethod) {
final IMethod method= (IMethod) child;
final String name= method.getElementName();
if (method.getDeclaringType().isEnum()) {
final int count= method.getNumberOfParameters();
if (count == 0 && "values".equals(name)) //$NON-NLS-1$
continue;
if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
continue;
if (method.isConstructor())
continue;
}
boolean skip= !stub || "<clinit>".equals(name); //$NON-NLS-1$
if (method.isConstructor())
skip= false;
skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
if (!skip)
appendMethodDeclaration(method);
}
fBuffer.append("\n"); //$NON-NLS-1$
}
} finally {
monitor.done();
}
}
protected void appendMethodBody(final IMethod method) throws JavaModelException {
if (method.isConstructor()) {
final IType declaringType= method.getDeclaringType();
String superSignature= declaringType.getSuperclassTypeSignature();
if (superSignature != null) {
superSignature= Signature.getTypeErasure(superSignature);
final IType superclass= declaringType.getJavaProject().findType(Signature.getSignatureQualifier(superSignature), Signature.getSignatureSimpleName(superSignature));
if (superclass != null) {
final IMethod[] superMethods= superclass.getMethods();
// collect super constructors by parameter count
Map<Integer, List<IMethod>> superConstructorsByParamCount= new TreeMap<>();
boolean multi= false;
IMethod superConstructor= null;
for (IMethod superMethod : superMethods) {
if (superMethod.isConstructor()
&& !Flags.isPrivate(superMethod.getFlags())
&& (!Flags.isPackageDefault(superMethod.getFlags()) || declaringType.getPackageFragment().equals(superclass.getPackageFragment()))
) {
int paramCount= superMethod.getNumberOfParameters();
if (paramCount == 0) {
superConstructor= superMethod;
break;
}
List<IMethod> constructors= superConstructorsByParamCount.get(paramCount);
if (constructors == null) {
constructors= new ArrayList<>();
superConstructorsByParamCount.put(paramCount, constructors);
}
constructors.add(superMethod);
}
}
if (superConstructor == null && superConstructorsByParamCount.size() > 0) {
// look for constructors without exceptions and without parameters
done: for (List<IMethod> constructors : superConstructorsByParamCount.values()) {
for (IMethod constructor : constructors) {
if (constructor.getExceptionTypes().length == 0) {
superConstructor= constructor;
multi= constructors.size() != 1;
if (multi)
break;
else
break done;
}
if (superConstructor == null) {
superConstructor= constructor;
multi= constructors.size() != 1;
}
}
}
if (superConstructor == null) {
// give up, get first
superConstructor= superConstructorsByParamCount.values().iterator().next().get(0);
multi= true;
}
}
if (superConstructor != null) {
final String[] superParameters= superConstructor.getParameterTypes();
final int paramLength= superParameters.length;
fBuffer.append("super("); //$NON-NLS-1$
if (paramLength != 0) {
for (int index= 0; index < paramLength; index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
appendExpression(superParameters[index], multi);
}
}
fBuffer.append(");"); //$NON-NLS-1$
}
}
}
} else {
String returnType= method.getReturnType();
if (!Signature.SIG_VOID.equals(returnType)) {
fBuffer.append("return "); //$NON-NLS-1$
appendExpression(returnType);
fBuffer.append(";"); //$NON-NLS-1$
}
}
}
protected void appendMethodDeclaration(final IMethod method) throws JavaModelException {
appendFlags(method);
fBuffer.append(" "); //$NON-NLS-1$
final ITypeParameter[] parameters= method.getTypeParameters();
if (parameters.length > 0) {
appendTypeParameters(parameters);
fBuffer.append(" "); //$NON-NLS-1$
}
final String returnType= method.getReturnType();
if (!method.isConstructor()) {
fBuffer.append(Signature.toString(returnType));
fBuffer.append(" "); //$NON-NLS-1$
}
fBuffer.append(method.getElementName());
fBuffer.append("("); //$NON-NLS-1$
final String[] parameterTypes= method.getParameterTypes();
final int flags= method.getFlags();
final boolean varargs= Flags.isVarargs(flags);
final int parameterLength= parameterTypes.length;
for (int index= 0; index < parameterLength; index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
fBuffer.append(Signature.toString(parameterTypes[index]));
if (varargs && index == parameterLength - 1) {
final int length= fBuffer.length();
if (length >= 2 && fBuffer.indexOf("[]", length - 2) >= 0) //$NON-NLS-1$
fBuffer.setLength(length - 2);
fBuffer.append("..."); //$NON-NLS-1$
}
fBuffer.append(" "); //$NON-NLS-1$
appendMethodParameterName(method, index);
}
fBuffer.append(")"); //$NON-NLS-1$
final String[] exceptionTypes= method.getExceptionTypes();
final int exceptionLength= exceptionTypes.length;
if (exceptionLength > 0)
fBuffer.append(" throws "); //$NON-NLS-1$
for (int index= 0; index < exceptionLength; index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
fBuffer.append(Signature.toString(exceptionTypes[index]));
}
if (Flags.isAnnnotationDefault(flags) ) {
fBuffer.append(" default "); //$NON-NLS-1$
IMemberValuePair pair= method.getDefaultValue();
appendAnnotationValue(pair.getValue(), pair.getValueKind());
}
if (Flags.isAbstract(flags) || Flags.isNative(flags))
fBuffer.append(";"); //$NON-NLS-1$
else {
fBuffer.append("{\n"); //$NON-NLS-1$
appendMethodBody(method);
fBuffer.append("}"); //$NON-NLS-1$
}
}
/**
* Appends a parameter name
*
* @param method the method
* @param index the index of the parameter
*/
protected void appendMethodParameterName(IMethod method, int index) {
fBuffer.append("a"); //$NON-NLS-1$
fBuffer.append(index);
}
protected void appendSuperInterfaceTypes(final IType type) throws JavaModelException {
final String[] signatures= type.getSuperInterfaceTypeSignatures();
if (signatures.length > 0) {
if (type.isInterface())
fBuffer.append(" extends "); //$NON-NLS-1$
else
fBuffer.append(" implements "); //$NON-NLS-1$
}
for (int index= 0; index < signatures.length; index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
fBuffer.append(Signature.toString(signatures[index]));
}
}
protected void appendTopLevelType(final IType type, IProgressMonitor subProgressMonitor) throws JavaModelException {
String packageName= type.getPackageFragment().getElementName();
if (packageName.length() > 0) {
fBuffer.append("package "); //$NON-NLS-1$
fBuffer.append(packageName);
fBuffer.append(";\n"); //$NON-NLS-1$
}
appendTypeDeclaration(type, subProgressMonitor);
}
protected void appendTypeDeclaration(final IType type, final IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
if (type.isAnnotation()) {
appendFlags(type);
fBuffer.append(" @interface "); //$NON-NLS-1$
fBuffer.append(type.getElementName());
fBuffer.append("{\n"); //$NON-NLS-1$
appendMembers(type, Progress.subMonitor(monitor, 1));
fBuffer.append("}"); //$NON-NLS-1$
} else if (type.isInterface()) {
appendFlags(type);
fBuffer.append(" interface "); //$NON-NLS-1$
fBuffer.append(type.getElementName());
appendTypeParameters(type.getTypeParameters());
appendSuperInterfaceTypes(type);
fBuffer.append("{\n"); //$NON-NLS-1$
appendMembers(type, Progress.subMonitor(monitor, 1));
fBuffer.append("}"); //$NON-NLS-1$
} else if (type.isClass()) {
appendFlags(type);
fBuffer.append(" class "); //$NON-NLS-1$
fBuffer.append(type.getElementName());
appendTypeParameters(type.getTypeParameters());
final String signature= type.getSuperclassTypeSignature();
if (signature != null) {
fBuffer.append(" extends "); //$NON-NLS-1$
fBuffer.append(Signature.toString(signature));
}
appendSuperInterfaceTypes(type);
fBuffer.append("{\n"); //$NON-NLS-1$
appendMembers(type, Progress.subMonitor(monitor, 1));
fBuffer.append("}"); //$NON-NLS-1$
} else if (type.isEnum()) {
appendFlags(type);
fBuffer.append(" enum "); //$NON-NLS-1$
fBuffer.append(type.getElementName());
appendSuperInterfaceTypes(type);
fBuffer.append("{\n"); //$NON-NLS-1$
appendEnumConstants(type);
appendMembers(type, Progress.subMonitor(monitor, 1));
fBuffer.append("}"); //$NON-NLS-1$
}
} finally {
monitor.done();
}
}
protected void appendTypeParameters(final ITypeParameter[] parameters) throws JavaModelException {
final int length= parameters.length;
if (length > 0)
fBuffer.append("<"); //$NON-NLS-1$
for (int index= 0; index < length; index++) {
if (index > 0)
fBuffer.append(","); //$NON-NLS-1$
final ITypeParameter parameter= parameters[index];
fBuffer.append(parameter.getElementName());
final String[] bounds= parameter.getBounds();
final int size= bounds.length;
if (size > 0)
fBuffer.append(" extends "); //$NON-NLS-1$
for (int offset= 0; offset < size; offset++) {
if (offset > 0)
fBuffer.append(" & "); //$NON-NLS-1$
fBuffer.append(bounds[offset]);
}
}
if (length > 0)
fBuffer.append(">"); //$NON-NLS-1$
}
/**
* Creates and returns a stub for the given top-level type.
*
* @param topLevelType the top-level type
* @param monitor the progress monitor, can be <code>null</code>
* @return the source stub
* @throws JavaModelException if this element does not exist or if an exception occurs while
* accessing its corresponding resource
*/
public String createStub(IType topLevelType, IProgressMonitor monitor) throws JavaModelException {
Assert.isTrue(Checks.isTopLevel(topLevelType));
if (monitor == null)
monitor= new NullProgressMonitor();
fBuffer= new StringBuffer(2046);
appendTopLevelType(topLevelType, monitor);
String result= fBuffer.toString();
fBuffer= null;
return result;
}
}