22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import javax.annotation.processing.*;
29 import javax.lang.model.*;
30 import javax.lang.model.element.*;
31 import static javax.lang.model.element.ElementKind.*;
32 import static javax.lang.model.element.NestingKind.*;
33 import static javax.lang.model.element.ModuleElement.DirectiveKind.*;
34 import static javax.lang.model.element.ModuleElement.*;
35 import javax.lang.model.type.*;
36 import javax.lang.model.util.*;
37
38 import java.io.PrintWriter;
39 import java.io.Writer;
40 import java.util.*;
41 import java.util.stream.Collectors;
42
43 import com.sun.tools.javac.util.DefinedBy;
44 import com.sun.tools.javac.util.DefinedBy.Api;
45 import com.sun.tools.javac.util.StringUtils;
46
47 /**
48 * A processor which prints out elements. Used to implement the
49 * -Xprint option; the included visitor class is used to implement
50 * Elements.printElements.
51 *
52 * <p><b>This is NOT part of any supported API.
53 * If you write code that depends on this, you do so at your own risk.
54 * This code and its internal interfaces are subject to change or
55 * deletion without notice.</b>
56 */
57 @SupportedAnnotationTypes("*")
58 @SupportedSourceVersion(SourceVersion.RELEASE_13)
59 public class PrintingProcessor extends AbstractProcessor {
60 PrintWriter writer;
61
199 if (nestingKind == TOP_LEVEL) {
200 PackageElement pkg = elementUtils.getPackageOf(e);
201 if (!pkg.isUnnamed())
202 writer.print("package " + pkg.getQualifiedName() + ";\n");
203 }
204
205 defaultAction(e, true);
206
207 switch(kind) {
208 case ANNOTATION_TYPE:
209 writer.print("@interface");
210 break;
211 default:
212 writer.print(StringUtils.toLowerCase(kind.toString()));
213 }
214 writer.print(" ");
215 writer.print(e.getSimpleName());
216
217 printFormalTypeParameters(e, false);
218
219 // Print superclass information if informative
220 if (kind == CLASS) {
221 TypeMirror supertype = e.getSuperclass();
222 if (supertype.getKind() != TypeKind.NONE) {
223 TypeElement e2 = (TypeElement)
224 ((DeclaredType) supertype).asElement();
225 if (e2.getSuperclass().getKind() != TypeKind.NONE)
226 writer.print(" extends " + supertype);
227 }
228 }
229
230 printInterfaces(e);
231 }
232 writer.println(" {");
233 indentation++;
234
235 if (kind == ENUM) {
236 List<Element> enclosedElements = new ArrayList<>(e.getEnclosedElements());
237 // Handle any enum constants specially before other entities.
238 List<Element> enumConstants = new ArrayList<>();
239 for(Element element : enclosedElements) {
240 if (element.getKind() == ENUM_CONSTANT)
241 enumConstants.add(element);
242 }
243 if (!enumConstants.isEmpty()) {
244 int i;
245 for(i = 0; i < enumConstants.size()-1; i++) {
246 this.visit(enumConstants.get(i), true);
247 writer.print(",");
248 }
249 this.visit(enumConstants.get(i), true);
250 writer.println(";\n");
251
252 enclosedElements.removeAll(enumConstants);
253 }
254
255 for(Element element : enclosedElements)
256 this.visit(element);
257 } else {
258 for(Element element : e.getEnclosedElements())
259 this.visit(element);
260 }
261
262 indentation--;
263 indent();
264 writer.println("}");
265 return this;
266 }
267
268 @Override @DefinedBy(Api.LANGUAGE_MODEL)
269 public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
270 ElementKind kind = e.getKind();
271 defaultAction(e, newLine);
272
273 if (kind == ENUM_CONSTANT)
274 writer.print(e.getSimpleName());
275 else {
276 writer.print(e.asType().toString() + " " + e.getSimpleName() );
277 Object constantValue = e.getConstantValue();
278 if (constantValue != null) {
430 indent();
431 }
432
433 if (kind == ENUM_CONSTANT)
434 return;
435
436 Set<Modifier> modifiers = new LinkedHashSet<>();
437 modifiers.addAll(e.getModifiers());
438
439 switch (kind) {
440 case ANNOTATION_TYPE:
441 case INTERFACE:
442 modifiers.remove(Modifier.ABSTRACT);
443 break;
444
445 case ENUM:
446 modifiers.remove(Modifier.FINAL);
447 modifiers.remove(Modifier.ABSTRACT);
448 break;
449
450 case METHOD:
451 case FIELD:
452 Element enclosingElement = e.getEnclosingElement();
453 if (enclosingElement != null &&
454 enclosingElement.getKind().isInterface()) {
455 modifiers.remove(Modifier.PUBLIC);
456 modifiers.remove(Modifier.ABSTRACT); // only for methods
457 modifiers.remove(Modifier.STATIC); // only for fields
458 modifiers.remove(Modifier.FINAL); // only for fields
459 }
460 break;
461
462 }
463
464 for(Modifier m: modifiers) {
465 writer.print(m.toString() + " ");
466 }
467 }
468
469 private void printFormalTypeParameters(Parameterizable e,
564 }
565 }
566
567 private void printInterfaces(TypeElement e) {
568 ElementKind kind = e.getKind();
569
570 if(kind != ANNOTATION_TYPE) {
571 List<? extends TypeMirror> interfaces = e.getInterfaces();
572 if (interfaces.size() > 0) {
573 writer.print((kind.isClass() ? " implements" : " extends"));
574
575 boolean first = true;
576 for(TypeMirror interf: interfaces) {
577 if (!first)
578 writer.print(",");
579 writer.print(" ");
580 writer.print(interf.toString());
581 first = false;
582 }
583 }
584 }
585 }
586
587 private void printThrows(ExecutableElement e) {
588 List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
589 final int size = thrownTypes.size();
590 if (size != 0) {
591 writer.print(" throws");
592
593 int i = 1;
594 for(TypeMirror thrownType: thrownTypes) {
595 if (i == 1)
596 writer.print(" ");
597
598 if (i == 2)
599 indentation++;
600
601 if (i >= 2)
602 indent();
603
|
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import javax.annotation.processing.*;
29 import javax.lang.model.*;
30 import javax.lang.model.element.*;
31 import static javax.lang.model.element.ElementKind.*;
32 import static javax.lang.model.element.NestingKind.*;
33 import static javax.lang.model.element.ModuleElement.DirectiveKind.*;
34 import static javax.lang.model.element.ModuleElement.*;
35 import javax.lang.model.type.*;
36 import javax.lang.model.util.*;
37
38 import java.io.PrintWriter;
39 import java.io.Writer;
40 import java.util.*;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43
44
45 import com.sun.tools.javac.util.DefinedBy;
46 import com.sun.tools.javac.util.DefinedBy.Api;
47 import com.sun.tools.javac.util.StringUtils;
48
49 /**
50 * A processor which prints out elements. Used to implement the
51 * -Xprint option; the included visitor class is used to implement
52 * Elements.printElements.
53 *
54 * <p><b>This is NOT part of any supported API.
55 * If you write code that depends on this, you do so at your own risk.
56 * This code and its internal interfaces are subject to change or
57 * deletion without notice.</b>
58 */
59 @SupportedAnnotationTypes("*")
60 @SupportedSourceVersion(SourceVersion.RELEASE_13)
61 public class PrintingProcessor extends AbstractProcessor {
62 PrintWriter writer;
63
201 if (nestingKind == TOP_LEVEL) {
202 PackageElement pkg = elementUtils.getPackageOf(e);
203 if (!pkg.isUnnamed())
204 writer.print("package " + pkg.getQualifiedName() + ";\n");
205 }
206
207 defaultAction(e, true);
208
209 switch(kind) {
210 case ANNOTATION_TYPE:
211 writer.print("@interface");
212 break;
213 default:
214 writer.print(StringUtils.toLowerCase(kind.toString()));
215 }
216 writer.print(" ");
217 writer.print(e.getSimpleName());
218
219 printFormalTypeParameters(e, false);
220
221 if (kind == RECORD) {
222 // Print out state description...
223 writer.print("(");
224 writer.print(e.getStateDescription()
225 .stream()
226 .map(stateDes -> stateDes.getSimpleName())
227 .collect(Collectors.joining(", ")));
228 writer.print(")");
229 }
230
231 // Print superclass information if informative
232 if (kind == CLASS) {
233 TypeMirror supertype = e.getSuperclass();
234 if (supertype.getKind() != TypeKind.NONE) {
235 TypeElement e2 = (TypeElement)
236 ((DeclaredType) supertype).asElement();
237 if (e2.getSuperclass().getKind() != TypeKind.NONE)
238 writer.print(" extends " + supertype);
239 }
240 }
241
242 printInterfaces(e);
243 printPermittedSubtypes(e);
244 }
245 writer.println(" {");
246 indentation++;
247
248 if (kind == ENUM) {
249 List<Element> enclosedElements = new ArrayList<>(e.getEnclosedElements());
250 // Handle any enum constants specially before other entities.
251 List<Element> enumConstants = new ArrayList<>();
252 for(Element element : enclosedElements) {
253 if (element.getKind() == ENUM_CONSTANT)
254 enumConstants.add(element);
255 }
256 if (!enumConstants.isEmpty()) {
257 int i;
258 for(i = 0; i < enumConstants.size()-1; i++) {
259 this.visit(enumConstants.get(i), true);
260 writer.print(",");
261 }
262 this.visit(enumConstants.get(i), true);
263 writer.println(";\n");
264
265 enclosedElements.removeAll(enumConstants);
266 }
267
268 for(Element element : enclosedElements)
269 this.visit(element);
270 } else {
271 for(Element element :
272 (kind != RECORD ?
273 e.getEnclosedElements() :
274 e.getEnclosedElements()
275 .stream()
276 .filter(elt -> elementUtils.getOrigin(elt) != Elements.Origin.EXPLICIT )
277 .collect(Collectors.toList()) ))
278 this.visit(element);
279 }
280
281 indentation--;
282 indent();
283 writer.println("}");
284 return this;
285 }
286
287 @Override @DefinedBy(Api.LANGUAGE_MODEL)
288 public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
289 ElementKind kind = e.getKind();
290 defaultAction(e, newLine);
291
292 if (kind == ENUM_CONSTANT)
293 writer.print(e.getSimpleName());
294 else {
295 writer.print(e.asType().toString() + " " + e.getSimpleName() );
296 Object constantValue = e.getConstantValue();
297 if (constantValue != null) {
449 indent();
450 }
451
452 if (kind == ENUM_CONSTANT)
453 return;
454
455 Set<Modifier> modifiers = new LinkedHashSet<>();
456 modifiers.addAll(e.getModifiers());
457
458 switch (kind) {
459 case ANNOTATION_TYPE:
460 case INTERFACE:
461 modifiers.remove(Modifier.ABSTRACT);
462 break;
463
464 case ENUM:
465 modifiers.remove(Modifier.FINAL);
466 modifiers.remove(Modifier.ABSTRACT);
467 break;
468
469 case RECORD:
470 modifiers.remove(Modifier.FINAL);
471 break;
472
473 case METHOD:
474 case FIELD:
475 Element enclosingElement = e.getEnclosingElement();
476 if (enclosingElement != null &&
477 enclosingElement.getKind().isInterface()) {
478 modifiers.remove(Modifier.PUBLIC);
479 modifiers.remove(Modifier.ABSTRACT); // only for methods
480 modifiers.remove(Modifier.STATIC); // only for fields
481 modifiers.remove(Modifier.FINAL); // only for fields
482 }
483 break;
484
485 }
486
487 for(Modifier m: modifiers) {
488 writer.print(m.toString() + " ");
489 }
490 }
491
492 private void printFormalTypeParameters(Parameterizable e,
587 }
588 }
589
590 private void printInterfaces(TypeElement e) {
591 ElementKind kind = e.getKind();
592
593 if(kind != ANNOTATION_TYPE) {
594 List<? extends TypeMirror> interfaces = e.getInterfaces();
595 if (interfaces.size() > 0) {
596 writer.print((kind.isClass() ? " implements" : " extends"));
597
598 boolean first = true;
599 for(TypeMirror interf: interfaces) {
600 if (!first)
601 writer.print(",");
602 writer.print(" ");
603 writer.print(interf.toString());
604 first = false;
605 }
606 }
607 }
608 }
609
610 private void printPermittedSubtypes(TypeElement e) {
611 List<? extends TypeMirror> subtypes = e.getPermittedSubtypes();
612 if (!subtypes.isEmpty()) { // could remove this check with more complicated joining call
613 writer.print("permits ");
614 writer.print(Stream.of(subtypes).
615 map(subtype -> subtype.toString()).
616 collect(Collectors.joining(", ")));
617 }
618 }
619
620 private void printThrows(ExecutableElement e) {
621 List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
622 final int size = thrownTypes.size();
623 if (size != 0) {
624 writer.print(" throws");
625
626 int i = 1;
627 for(TypeMirror thrownType: thrownTypes) {
628 if (i == 1)
629 writer.print(" ");
630
631 if (i == 2)
632 indentation++;
633
634 if (i >= 2)
635 indent();
636
|