/
StubGenerator.java
454 lines (389 loc) · 13.8 KB
/
StubGenerator.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
package org.checkerframework.framework.stub;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Options;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.javacutil.ElementUtils;
import org.checkerframework.javacutil.SystemUtil;
import org.checkerframework.javacutil.TypesUtils;
import org.plumelib.util.CollectionsPlume;
import org.plumelib.util.StringsPlume;
/**
* Generates a stub file from a single class or an entire package.
*
* <p>A stub file can be used to add annotations to methods of classes, that are only available in
* binary or the source of which cannot be edited.
*
* @checker_framework.manual #stub Using stub classes
*/
public class StubGenerator {
/** The indentation for the class. */
private static final String INDENTION = " ";
/** The output stream. */
private final PrintStream out;
/** the current indentation for the line being processed. */
private String currentIndention = "";
/** the package of the class being processed. */
private String currentPackage = null;
/** Constructs a {@code StubGenerator} that outputs to {@code System.out}. */
public StubGenerator() {
this(System.out);
}
/**
* Constructs a {@code StubGenerator} that outputs to the provided output stream.
*
* @param out the output stream
*/
public StubGenerator(PrintStream out) {
this.out = out;
}
/**
* Constructs a {@code StubGenerator} that outputs to the provided output stream.
*
* @param out the output stream
*/
public StubGenerator(OutputStream out) {
this.out = new PrintStream(out);
}
/** Generate the stub file for all the classes within the provided package. */
public void stubFromField(Element elt) {
if (!(elt.getKind() == ElementKind.FIELD)) {
return;
}
String pkg = ElementUtils.getQualifiedName(ElementUtils.enclosingPackage(elt));
if (!"".equals(pkg)) {
currentPackage = pkg;
currentIndention = " ";
indent();
}
VariableElement field = (VariableElement) elt;
printFieldDecl(field);
}
/** Generate the stub file for all the classes within the provided package. */
public void stubFromPackage(PackageElement packageElement) {
currentPackage = packageElement.getQualifiedName().toString();
indent();
out.print("package ");
out.print(currentPackage);
out.println(";");
for (TypeElement element : ElementFilter.typesIn(packageElement.getEnclosedElements())) {
if (isPublicOrProtected(element)) {
out.println();
printClass(element);
}
}
}
/** Generate the stub file for all the classes within the provided package. */
public void stubFromMethod(Element elt) {
if (!(elt.getKind() == ElementKind.CONSTRUCTOR || elt.getKind() == ElementKind.METHOD)) {
return;
}
String newPackage = ElementUtils.getQualifiedName(ElementUtils.enclosingPackage(elt));
if (!newPackage.equals("")) {
currentPackage = newPackage;
currentIndention = " ";
indent();
}
ExecutableElement method = (ExecutableElement) elt;
printMethodDecl(method);
}
/** Generate the stub file for provided class. The generated file includes the package name. */
public void stubFromType(TypeElement typeElement) {
// only output stub for classes or interfaces. not enums
if (typeElement.getKind() != ElementKind.CLASS
&& typeElement.getKind() != ElementKind.INTERFACE) {
return;
}
String newPackageName =
ElementUtils.getQualifiedName(ElementUtils.enclosingPackage(typeElement));
boolean newPackage = !newPackageName.equals(currentPackage);
currentPackage = newPackageName;
if (newPackage) {
indent();
out.print("package ");
out.print(currentPackage);
out.println(";");
out.println();
}
String fullClassName = ElementUtils.getQualifiedClassName(typeElement).toString();
String className =
fullClassName.substring(
fullClassName.indexOf(currentPackage)
+ currentPackage.length()
// +1 because currentPackage doesn't include
// the . between the package name and the classname
+ 1);
int index = className.lastIndexOf('.');
if (index == -1) {
printClass(typeElement);
} else {
String outer = className.substring(0, index);
printClass(typeElement, outer.replace('.', '$'));
}
}
/** helper method that outputs the index for the provided class. */
private void printClass(TypeElement typeElement) {
printClass(typeElement, null);
}
/**
* Helper method that prints the stub file for the provided class.
*
* @param typeElement the class to output
* @param outerClass the outer class of the class, or null if {@code typeElement} is a top-level
* class
*/
private void printClass(TypeElement typeElement, @Nullable String outerClass) {
indent();
List<? extends AnnotationMirror> teannos = typeElement.getAnnotationMirrors();
if (teannos != null && !teannos.isEmpty()) {
for (AnnotationMirror am : teannos) {
out.println(am);
}
}
if (typeElement.getKind() == ElementKind.INTERFACE) {
out.print("interface");
} else if (typeElement.getKind() == ElementKind.CLASS) {
out.print("class");
} else {
return;
}
out.print(' ');
if (outerClass != null) {
out.print(outerClass + "$");
}
out.print(typeElement.getSimpleName());
// Type parameters
if (!typeElement.getTypeParameters().isEmpty()) {
out.print('<');
out.print(formatList(typeElement.getTypeParameters()));
out.print('>');
}
// Extends
if (typeElement.getSuperclass().getKind() != TypeKind.NONE
&& !TypesUtils.isObject(typeElement.getSuperclass())) {
out.print(" extends ");
out.print(formatType(typeElement.getSuperclass()));
}
// implements
if (!typeElement.getInterfaces().isEmpty()) {
final boolean isInterface = typeElement.getKind() == ElementKind.INTERFACE;
out.print(isInterface ? " extends " : " implements ");
List<String> ls =
CollectionsPlume.mapList(StubGenerator::formatType, typeElement.getInterfaces());
out.print(formatList(ls));
}
out.println(" {");
String tempIndention = currentIndention;
currentIndention = currentIndention + INDENTION;
// Inner classes, which the stub generator prints later.
List<TypeElement> innerClass = new ArrayList<>();
// side-effects innerClass
printTypeMembers(typeElement.getEnclosedElements(), innerClass);
currentIndention = tempIndention;
indent();
out.println("}");
for (TypeElement element : innerClass) {
printClass(element, typeElement.getSimpleName().toString());
}
}
/**
* Helper method that outputs the public or protected inner members of a class.
*
* @param members list of the class members
*/
private void printTypeMembers(List<? extends Element> members, List<TypeElement> innerClass) {
for (Element element : members) {
if (isPublicOrProtected(element)) {
printMember(element, innerClass);
}
}
}
/** Helper method that outputs the declaration of the member. */
private void printMember(Element member, List<TypeElement> innerClass) {
if (member.getKind().isField()) {
printFieldDecl((VariableElement) member);
} else if (member instanceof ExecutableElement) {
printMethodDecl((ExecutableElement) member);
} else if (member instanceof TypeElement) {
innerClass.add((TypeElement) member);
}
}
/**
* Helper method that outputs the field declaration for the given field.
*
* <p>It indicates whether the field is {@code protected}.
*/
private void printFieldDecl(VariableElement field) {
if ("class".equals(field.getSimpleName().toString())) {
error("Cannot write class literals in stub files.");
return;
}
indent();
List<? extends AnnotationMirror> veannos = field.getAnnotationMirrors();
if (veannos != null && !veannos.isEmpty()) {
for (AnnotationMirror am : veannos) {
out.println(am);
}
}
// if protected, indicate that, but not public
if (field.getModifiers().contains(Modifier.PROTECTED)) {
out.print("protected ");
}
if (field.getModifiers().contains(Modifier.STATIC)) {
out.print("static ");
}
if (field.getModifiers().contains(Modifier.FINAL)) {
out.print("final ");
}
out.print(formatType(field.asType()));
out.print(" ");
out.print(field.getSimpleName());
out.println(';');
}
/**
* Helper method that outputs the method declaration for the given method.
*
* <p>IT indicates whether the field is {@code protected}.
*/
private void printMethodDecl(ExecutableElement method) {
indent();
List<? extends AnnotationMirror> eeannos = method.getAnnotationMirrors();
if (eeannos != null && !eeannos.isEmpty()) {
for (AnnotationMirror am : eeannos) {
out.println(am);
}
}
// if protected, indicate that, but not public
if (method.getModifiers().contains(Modifier.PROTECTED)) {
out.print("protected ");
}
if (method.getModifiers().contains(Modifier.STATIC)) {
out.print("static ");
}
// print Generic arguments
if (!method.getTypeParameters().isEmpty()) {
out.print('<');
out.print(formatList(method.getTypeParameters()));
out.print("> ");
}
// not return type for constructors
if (method.getKind() != ElementKind.CONSTRUCTOR) {
out.print(formatType(method.getReturnType()));
out.print(" ");
out.print(method.getSimpleName());
} else {
out.print(method.getEnclosingElement().getSimpleName());
}
out.print('(');
boolean isFirst = true;
for (VariableElement param : method.getParameters()) {
if (!isFirst) {
out.print(", ");
}
out.print(formatType(param.asType()));
out.print(' ');
out.print(param.getSimpleName());
isFirst = false;
}
out.print(')');
if (!method.getThrownTypes().isEmpty()) {
out.print(" throws ");
List<String> ltt =
CollectionsPlume.mapList(StubGenerator::formatType, method.getThrownTypes());
out.print(formatList(ltt));
}
out.println(';');
}
/** Indent the current line. */
private void indent() {
out.print(currentIndention);
}
/**
* Return a string representation of the list in the form of {@code item1, item2, item3, ...},
* without surrounding square brackets as the default representation has.
*
* @param lst a list to format
* @return a string representation of the list, without surrounding square brackets
*/
private String formatList(List<?> lst) {
return StringsPlume.join(", ", lst);
}
/** Returns true if the element is public or protected element. */
private boolean isPublicOrProtected(Element element) {
return element.getModifiers().contains(Modifier.PUBLIC)
|| element.getModifiers().contains(Modifier.PROTECTED);
}
/**
* Returns the simple name of the type.
*
* @param typeRep a type
* @return the simple name of the type
*/
private static String formatType(TypeMirror typeRep) {
StringTokenizer tokenizer = new StringTokenizer(typeRep.toString(), "()<>[], ", true);
StringBuilder sb = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (token.length() == 1 || token.lastIndexOf('.') == -1) {
sb.append(token);
} else {
int index = token.lastIndexOf('.');
sb.append(token.substring(index + 1));
}
}
return sb.toString();
}
/**
* The main entry point to StubGenerator.
*
* @param args command-line arguments
*/
@SuppressWarnings("signature") // User-supplied arguments to main
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage:");
System.out.println(" java StubGenerator [class or package name]");
return;
}
Context context = new Context();
Options options = Options.instance(context);
if (SystemUtil.getJreVersion() == 8) {
options.put(Option.SOURCE, "8");
options.put(Option.TARGET, "8");
}
JavaCompiler javac = JavaCompiler.instance(context);
javac.initModules(com.sun.tools.javac.util.List.nil());
javac.enterDone();
ProcessingEnvironment env = JavacProcessingEnvironment.instance(context);
StubGenerator generator = new StubGenerator();
if (env.getElementUtils().getPackageElement(args[0]) != null) {
generator.stubFromPackage(env.getElementUtils().getPackageElement(args[0]));
} else if (env.getElementUtils().getTypeElement(args[0]) != null) {
generator.stubFromType(env.getElementUtils().getTypeElement(args[0]));
} else {
error("Couldn't find a package or a class named " + args[0]);
}
}
private static void error(String string) {
System.err.println("StubGenerator: " + string);
}
}