< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java

Print this page




  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_14)
  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) {


 431                 indent();
 432             }
 433 
 434             if (kind == ENUM_CONSTANT)
 435                 return;
 436 
 437             Set<Modifier> modifiers = new LinkedHashSet<>();
 438             modifiers.addAll(e.getModifiers());
 439 
 440             switch (kind) {
 441             case ANNOTATION_TYPE:
 442             case INTERFACE:
 443                 modifiers.remove(Modifier.ABSTRACT);
 444                 break;
 445 
 446             case ENUM:
 447                 modifiers.remove(Modifier.FINAL);
 448                 modifiers.remove(Modifier.ABSTRACT);
 449                 break;
 450 




 451             case METHOD:
 452             case FIELD:
 453                 Element enclosingElement = e.getEnclosingElement();
 454                 if (enclosingElement != null &&
 455                     enclosingElement.getKind().isInterface()) {
 456                     modifiers.remove(Modifier.PUBLIC);
 457                     modifiers.remove(Modifier.ABSTRACT); // only for methods
 458                     modifiers.remove(Modifier.STATIC);   // only for fields
 459                     modifiers.remove(Modifier.FINAL);    // only for fields
 460                 }
 461                 break;
 462 
 463             }
 464             if (!modifiers.isEmpty()) {
 465                 writer.print(modifiers.stream()
 466                              .map(Modifier::toString)
 467                              .collect(Collectors.joining(" ", "", " ")));
 468             }
 469         }
 470 


 552                     }
 553 
 554                     if (parameters.size() >= 2)
 555                         indentation--;
 556                 }
 557                 break;
 558             }
 559         }
 560 
 561         private void printInterfaces(TypeElement e) {
 562             ElementKind kind = e.getKind();
 563 
 564             if(kind != ANNOTATION_TYPE) {
 565                 List<? extends TypeMirror> interfaces = e.getInterfaces();
 566                 if (!interfaces.isEmpty()) {
 567                     writer.print((kind.isClass() ? " implements " : " extends "));
 568                     writer.print(interfaces.stream()
 569                                  .map(TypeMirror::toString)
 570                                  .collect(Collectors.joining(", ")));
 571                 }











 572             }
 573         }
 574 
 575         private void printThrows(ExecutableElement e) {
 576             List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
 577             final int size = thrownTypes.size();
 578             if (size != 0) {
 579                 writer.print(" throws");
 580 
 581                 int i = 1;
 582                 for(TypeMirror thrownType: thrownTypes) {
 583                     if (i == 1)
 584                         writer.print(" ");
 585 
 586                     if (i == 2)
 587                         indentation++;
 588 
 589                     if (i >= 2)
 590                         indent();
 591 




  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_14)
  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 components
 223                     writer.print("(");
 224                     writer.print(e.getStateComponents()
 225                                  .stream()
 226                                  .map(stateDes -> stateDes.asType().toString() + " " + 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) {


 450                 indent();
 451             }
 452 
 453             if (kind == ENUM_CONSTANT)
 454                 return;
 455 
 456             Set<Modifier> modifiers = new LinkedHashSet<>();
 457             modifiers.addAll(e.getModifiers());
 458 
 459             switch (kind) {
 460             case ANNOTATION_TYPE:
 461             case INTERFACE:
 462                 modifiers.remove(Modifier.ABSTRACT);
 463                 break;
 464 
 465             case ENUM:
 466                 modifiers.remove(Modifier.FINAL);
 467                 modifiers.remove(Modifier.ABSTRACT);
 468                 break;
 469 
 470             case RECORD:
 471                 modifiers.remove(Modifier.FINAL);
 472                 break;
 473 
 474             case METHOD:
 475             case FIELD:
 476                 Element enclosingElement = e.getEnclosingElement();
 477                 if (enclosingElement != null &&
 478                     enclosingElement.getKind().isInterface()) {
 479                     modifiers.remove(Modifier.PUBLIC);
 480                     modifiers.remove(Modifier.ABSTRACT); // only for methods
 481                     modifiers.remove(Modifier.STATIC);   // only for fields
 482                     modifiers.remove(Modifier.FINAL);    // only for fields
 483                 }
 484                 break;
 485 
 486             }
 487             if (!modifiers.isEmpty()) {
 488                 writer.print(modifiers.stream()
 489                              .map(Modifier::toString)
 490                              .collect(Collectors.joining(" ", "", " ")));
 491             }
 492         }
 493 


 575                     }
 576 
 577                     if (parameters.size() >= 2)
 578                         indentation--;
 579                 }
 580                 break;
 581             }
 582         }
 583 
 584         private void printInterfaces(TypeElement e) {
 585             ElementKind kind = e.getKind();
 586 
 587             if(kind != ANNOTATION_TYPE) {
 588                 List<? extends TypeMirror> interfaces = e.getInterfaces();
 589                 if (!interfaces.isEmpty()) {
 590                     writer.print((kind.isClass() ? " implements " : " extends "));
 591                     writer.print(interfaces.stream()
 592                                  .map(TypeMirror::toString)
 593                                  .collect(Collectors.joining(", ")));
 594                 }
 595             }
 596         }
 597 
 598         private void printPermittedSubtypes(TypeElement e) {
 599             List<? extends TypeMirror> subtypes = e.getPermittedSubtypes();
 600             if (!subtypes.isEmpty()) { // could remove this check with more complicated joining call
 601                 writer.print(" permits ");
 602                 writer.print(subtypes
 603                              .stream()
 604                              .map(subtype -> subtype.toString())
 605                              .collect(Collectors.joining(", ")));
 606             }
 607         }
 608 
 609         private void printThrows(ExecutableElement e) {
 610             List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
 611             final int size = thrownTypes.size();
 612             if (size != 0) {
 613                 writer.print(" throws");
 614 
 615                 int i = 1;
 616                 for(TypeMirror thrownType: thrownTypes) {
 617                     if (i == 1)
 618                         writer.print(" ");
 619 
 620                     if (i == 2)
 621                         indentation++;
 622 
 623                     if (i >= 2)
 624                         indent();
 625 


< prev index next >