langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
changeset 26266 2d24bda701dc
parent 25874 83c19f00452c
child 43646 017aba6e9260
child 43375 0945845b891f
equal deleted inserted replaced
26265:46aacfffd3b5 26266:2d24bda701dc
    34 import javax.lang.model.util.*;
    34 import javax.lang.model.util.*;
    35 
    35 
    36 import java.io.PrintWriter;
    36 import java.io.PrintWriter;
    37 import java.io.Writer;
    37 import java.io.Writer;
    38 import java.util.*;
    38 import java.util.*;
       
    39 
       
    40 import com.sun.tools.javac.util.DefinedBy;
       
    41 import com.sun.tools.javac.util.DefinedBy.Api;
    39 import com.sun.tools.javac.util.StringUtils;
    42 import com.sun.tools.javac.util.StringUtils;
    40 
    43 
    41 /**
    44 /**
    42  * A processor which prints out elements.  Used to implement the
    45  * A processor which prints out elements.  Used to implement the
    43  * -Xprint option; the included visitor class is used to implement
    46  * -Xprint option; the included visitor class is used to implement
    60 
    63 
    61     public void setWriter(Writer w) {
    64     public void setWriter(Writer w) {
    62         writer = new PrintWriter(w);
    65         writer = new PrintWriter(w);
    63     }
    66     }
    64 
    67 
    65     @Override
    68     @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
    66     public boolean process(Set<? extends TypeElement> tes,
    69     public boolean process(Set<? extends TypeElement> tes,
    67                            RoundEnvironment renv) {
    70                            RoundEnvironment renv) {
    68 
    71 
    69         for(Element element : renv.getRootElements()) {
    72         for(Element element : renv.getRootElements()) {
    70             print(element);
    73             print(element);
    93             this.writer = new PrintWriter(w);
    96             this.writer = new PrintWriter(w);
    94             this.elementUtils = elementUtils;
    97             this.elementUtils = elementUtils;
    95             indentation = 0;
    98             indentation = 0;
    96         }
    99         }
    97 
   100 
    98         @Override
   101         @Override @DefinedBy(Api.LANGUAGE_MODEL)
    99         protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) {
   102         protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) {
   100             if (newLine != null && newLine)
   103             if (newLine != null && newLine)
   101                 writer.println();
   104                 writer.println();
   102             printDocComment(e);
   105             printDocComment(e);
   103             printModifiers(e);
   106             printModifiers(e);
   104             return this;
   107             return this;
   105         }
   108         }
   106 
   109 
   107         @Override
   110         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   108         public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) {
   111         public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) {
   109             ElementKind kind = e.getKind();
   112             ElementKind kind = e.getKind();
   110 
   113 
   111             if (kind != STATIC_INIT &&
   114             if (kind != STATIC_INIT &&
   112                 kind != INSTANCE_INIT) {
   115                 kind != INSTANCE_INIT) {
   116                 if (kind == CONSTRUCTOR &&
   119                 if (kind == CONSTRUCTOR &&
   117                     enclosing != null &&
   120                     enclosing != null &&
   118                     NestingKind.ANONYMOUS ==
   121                     NestingKind.ANONYMOUS ==
   119                     // Use an anonymous class to determine anonymity!
   122                     // Use an anonymous class to determine anonymity!
   120                     (new SimpleElementVisitor7<NestingKind, Void>() {
   123                     (new SimpleElementVisitor7<NestingKind, Void>() {
   121                         @Override
   124                         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   122                         public NestingKind visitType(TypeElement e, Void p) {
   125                         public NestingKind visitType(TypeElement e, Void p) {
   123                             return e.getNestingKind();
   126                             return e.getNestingKind();
   124                         }
   127                         }
   125                     }).visit(enclosing))
   128                     }).visit(enclosing))
   126                     return this;
   129                     return this;
   153             }
   156             }
   154             return this;
   157             return this;
   155         }
   158         }
   156 
   159 
   157 
   160 
   158         @Override
   161         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   159         public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
   162         public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
   160             ElementKind kind = e.getKind();
   163             ElementKind kind = e.getKind();
   161             NestingKind nestingKind = e.getNestingKind();
   164             NestingKind nestingKind = e.getNestingKind();
   162 
   165 
   163             if (NestingKind.ANONYMOUS == nestingKind) {
   166             if (NestingKind.ANONYMOUS == nestingKind) {
   257             indent();
   260             indent();
   258             writer.println("}");
   261             writer.println("}");
   259             return this;
   262             return this;
   260         }
   263         }
   261 
   264 
   262         @Override
   265         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   263         public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
   266         public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) {
   264             ElementKind kind = e.getKind();
   267             ElementKind kind = e.getKind();
   265             defaultAction(e, newLine);
   268             defaultAction(e, newLine);
   266 
   269 
   267             if (kind == ENUM_CONSTANT)
   270             if (kind == ENUM_CONSTANT)
   276                 writer.println(";");
   279                 writer.println(";");
   277             }
   280             }
   278             return this;
   281             return this;
   279         }
   282         }
   280 
   283 
   281         @Override
   284         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   282         public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
   285         public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
   283             writer.print(e.getSimpleName());
   286             writer.print(e.getSimpleName());
   284             return this;
   287             return this;
   285         }
   288         }
   286 
   289 
   287         // Should we do more here?
   290         // Should we do more here?
   288         @Override
   291         @Override @DefinedBy(Api.LANGUAGE_MODEL)
   289         public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
   292         public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
   290             defaultAction(e, false);
   293             defaultAction(e, false);
   291             if (!e.isUnnamed())
   294             if (!e.isUnnamed())
   292                 writer.println("package " + e.getQualifiedName() + ";");
   295                 writer.println("package " + e.getQualifiedName() + ";");
   293             else
   296             else