changeset 4034:05e0692b98ab

8176477: Use DirectiveVisitor to print module information Reviewed-by: jjg
author darcy
date Thu, 09 Mar 2017 18:33:47 -0800
parents 2ec0091a0f73
children a76a2f66ff2a
files src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
diffstat 1 files changed, 63 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java	Thu Mar 09 23:15:55 2017 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java	Thu Mar 09 18:33:47 2017 -0800
@@ -323,72 +323,77 @@
 
         private void printDirective(ModuleElement.Directive directive) {
             indent();
-            switch (directive.getKind()) {
-            case EXPORTS: // "exports package-name [to module-name-list]"
-                {
-                    ExportsDirective exportsDirective = (ExportsDirective) directive;
-                    writer.print("exports ");
-                    writer.print(exportsDirective.getPackage().getQualifiedName());
-                    printModuleList(exportsDirective.getTargetModules());
-                }
-                break;
-
-            case OPENS: // opens package-name [to module-name-list]
-                {
-                    OpensDirective opensDirective = (OpensDirective) directive;
-                    writer.print("opens ");
-                    writer.print(opensDirective.getPackage().getQualifiedName());
-                    printModuleList(opensDirective.getTargetModules());
-                }
-                break;
-
-            case PROVIDES: // provides service-name with implementation-name
-                {
-                    ProvidesDirective providesDirective = (ProvidesDirective) directive;
-                    writer.print("provides ");
-                    writer.print(providesDirective.getService().getQualifiedName());
-                    writer.print(" with ");
-                    printNameableList(providesDirective.getImplementations());
-                }
-                break;
-
-            case REQUIRES: // requires (static|transitive)* module-name
-                {
-                    RequiresDirective requiresDirective = (RequiresDirective) directive;
-                    writer.print("requires ");
-                    if (requiresDirective.isStatic())
-                        writer.print("static ");
-                    if (requiresDirective.isTransitive())
-                        writer.print("transitive ");
-                    writer.print(requiresDirective.getDependency().getQualifiedName());
-                }
-                break;
-
-            case USES: // uses service-name
-                {
-                    UsesDirective usesDirective = (UsesDirective) directive;
-                    writer.print("uses ");
-                    writer.print(usesDirective.getService().getQualifiedName());
-                }
-                break;
-
-            default:
-                throw new UnsupportedOperationException("unknown directive " + directive);
-            }
+            (new PrintDirective(writer)).visit(directive);
             writer.println(";");
         }
 
-        private void printModuleList(List<? extends ModuleElement> modules) {
-            if (modules != null) {
-                writer.print(" to ");
-                printNameableList(modules);
+        private static class PrintDirective implements ModuleElement.DirectiveVisitor<Void, Void> {
+            private final PrintWriter writer;
+
+            PrintDirective(PrintWriter writer) {
+                this.writer = writer;
             }
-        }
 
-        private void printNameableList(List<? extends QualifiedNameable> nameables) {
+            @Override
+            public Void visitExports(ExportsDirective d, Void p) {
+                // "exports package-name [to module-name-list]"
+                writer.print("exports ");
+                writer.print(d.getPackage().getQualifiedName());
+                printModuleList(d.getTargetModules());
+                return null;
+            }
+
+            @Override
+            public Void visitOpens(OpensDirective d, Void p) {
+                // opens package-name [to module-name-list]
+                writer.print("opens ");
+                writer.print(d.getPackage().getQualifiedName());
+                printModuleList(d.getTargetModules());
+                return null;
+            }
+
+            @Override
+            public Void visitProvides(ProvidesDirective d, Void p) {
+                // provides service-name with implementation-name
+                writer.print("provides ");
+                writer.print(d.getService().getQualifiedName());
+                writer.print(" with ");
+                printNameableList(d.getImplementations());
+                return null;
+            }
+
+            @Override
+            public Void visitRequires(RequiresDirective d, Void p) {
+                // requires (static|transitive)* module-name
+                writer.print("requires ");
+                if (d.isStatic())
+                    writer.print("static ");
+                if (d.isTransitive())
+                    writer.print("transitive ");
+                writer.print(d.getDependency().getQualifiedName());
+                return null;
+            }
+
+            @Override
+            public Void visitUses(UsesDirective d, Void p) {
+                // uses service-name
+                writer.print("uses ");
+                writer.print(d.getService().getQualifiedName());
+                return null;
+            }
+
+            private void printModuleList(List<? extends ModuleElement> modules) {
+                if (modules != null) {
+                    writer.print(" to ");
+                    printNameableList(modules);
+                }
+            }
+
+            private void printNameableList(List<? extends QualifiedNameable> nameables) {
                 writer.print(nameables.stream().
                              map(QualifiedNameable::getQualifiedName).
                              collect(Collectors.joining(", ")));
+            }
         }
 
         public void flush() {