langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
changeset 43375 0945845b891f
parent 26266 2d24bda701dc
child 43376 f165c71d9e03
equal deleted inserted replaced
43374:d312a15c5fcf 43375:0945845b891f
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    28 import javax.annotation.processing.*;
    28 import javax.annotation.processing.*;
    29 import javax.lang.model.*;
    29 import javax.lang.model.*;
    30 import javax.lang.model.element.*;
    30 import javax.lang.model.element.*;
    31 import static javax.lang.model.element.ElementKind.*;
    31 import static javax.lang.model.element.ElementKind.*;
    32 import static javax.lang.model.element.NestingKind.*;
    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.*;
    33 import javax.lang.model.type.*;
    35 import javax.lang.model.type.*;
    34 import javax.lang.model.util.*;
    36 import javax.lang.model.util.*;
    35 
    37 
    36 import java.io.PrintWriter;
    38 import java.io.PrintWriter;
    37 import java.io.Writer;
    39 import java.io.Writer;
    38 import java.util.*;
    40 import java.util.*;
       
    41 import java.util.stream.Collectors;
    39 
    42 
    40 import com.sun.tools.javac.util.DefinedBy;
    43 import com.sun.tools.javac.util.DefinedBy;
    41 import com.sun.tools.javac.util.DefinedBy.Api;
    44 import com.sun.tools.javac.util.DefinedBy.Api;
    42 import com.sun.tools.javac.util.StringUtils;
    45 import com.sun.tools.javac.util.StringUtils;
    43 
    46 
   296             else
   299             else
   297                 writer.println("// Unnamed package");
   300                 writer.println("// Unnamed package");
   298             return this;
   301             return this;
   299         }
   302         }
   300 
   303 
       
   304         @Override @DefinedBy(Api.LANGUAGE_MODEL)
       
   305         public PrintingElementVisitor visitModule(ModuleElement e, Boolean p) {
       
   306             defaultAction(e, false);
       
   307 
       
   308             if (!e.isUnnamed()) {
       
   309                 // TODO: openness of the module not currently exposed
       
   310                 // by the language model API, but should be printed
       
   311                 // here once available.
       
   312                 writer.println("module " + e.getQualifiedName() + " {");
       
   313                 indentation++;
       
   314                 for (ModuleElement.Directive directive : e.getDirectives()) {
       
   315                     printDirective(directive);
       
   316                 }
       
   317                 indentation--;
       
   318                 writer.println("}");
       
   319             } else
       
   320                 writer.println("// Unnamed module"); // Should we do more here?
       
   321             return this;
       
   322         }
       
   323 
       
   324         private void printDirective(ModuleElement.Directive directive) {
       
   325             indent();
       
   326             switch (directive.getKind()) {
       
   327             case EXPORTS: // "exports package-name [to module-name-list]"
       
   328                 {
       
   329                     ExportsDirective exportsDirective = (ExportsDirective) directive;
       
   330                     writer.print("exports ");
       
   331                     writer.print(exportsDirective.getPackage().getQualifiedName());
       
   332                     printModuleList(exportsDirective.getTargetModules());
       
   333                 }
       
   334                 break;
       
   335 
       
   336             case OPENS: // opens package-name [to module-name-list]
       
   337                 {
       
   338                     OpensDirective opensDirective = (OpensDirective) directive;
       
   339                     writer.print("opens ");
       
   340                     writer.print(opensDirective.getPackage().getQualifiedName());
       
   341                     printModuleList(opensDirective.getTargetModules());
       
   342                 }
       
   343                 break;
       
   344 
       
   345             case PROVIDES: // provides service-name with implementation-name
       
   346                 {
       
   347                     ProvidesDirective providesDirective = (ProvidesDirective) directive;
       
   348                     writer.print("provides ");
       
   349                     writer.print(providesDirective.getService().getQualifiedName());
       
   350                     writer.print(" with ");
       
   351                     printNameableList(providesDirective.getImplementations());
       
   352                 }
       
   353                 break;
       
   354 
       
   355             case REQUIRES: // requires (static|transitive)* module-name
       
   356                 {
       
   357                     RequiresDirective requiresDirective = (RequiresDirective) directive;
       
   358                     writer.print("requires ");
       
   359                     if (requiresDirective.isStatic())
       
   360                         writer.print("static ");
       
   361                     if (requiresDirective.isTransitive())
       
   362                         writer.print("transitive ");
       
   363                     writer.print(requiresDirective.getDependency().getQualifiedName());
       
   364                 }
       
   365                 break;
       
   366 
       
   367             case USES: // uses service-name
       
   368                 {
       
   369                     UsesDirective usesDirective = (UsesDirective) directive;
       
   370                     writer.print("uses ");
       
   371                     writer.print(usesDirective.getService().getQualifiedName());
       
   372                 }
       
   373                 break;
       
   374 
       
   375             default:
       
   376                 throw new UnsupportedOperationException("unknown directive " + directive);
       
   377             }
       
   378             writer.println(";");
       
   379         }
       
   380 
       
   381         private void printModuleList(List<? extends ModuleElement> modules) {
       
   382             if (modules != null) {
       
   383                 writer.print(" to ");
       
   384                 printNameableList(modules);
       
   385             }
       
   386         }
       
   387 
       
   388         private void printNameableList(List<? extends QualifiedNameable> nameables) {
       
   389                 writer.print(nameables.stream().
       
   390                              map(QualifiedNameable::getQualifiedName).
       
   391                              collect(Collectors.joining(", ")));
       
   392         }
       
   393 
   301         public void flush() {
   394         public void flush() {
   302             writer.flush();
   395             writer.flush();
   303         }
   396         }
   304 
   397 
   305         private void printDocComment(Element e) {
   398         private void printDocComment(Element e) {