8222817: Refactor printing processor to use streams
authordarcy
Tue, 23 Apr 2019 14:56:39 -0700
changeset 54602 848a2f381e2c
parent 54601 c40b2a190173
child 54603 cb8e16d6ff1e
8222817: Refactor printing processor to use streams Reviewed-by: jjg
src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java	Tue Apr 23 22:55:09 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java	Tue Apr 23 14:56:39 2019 -0700
@@ -424,7 +424,8 @@
         private void printModifiers(Element e) {
             ElementKind kind = e.getKind();
             if (kind == PARAMETER) {
-                printAnnotationsInline(e);
+                // Print annotation inline
+                writer.print(annotationsToString(e));
             } else {
                 printAnnotations(e);
                 indent();
@@ -460,39 +461,32 @@
                 break;
 
             }
-
-            for(Modifier m: modifiers) {
-                writer.print(m.toString() + " ");
+            if (!modifiers.isEmpty()) {
+                writer.print(modifiers.stream()
+                             .map(Modifier::toString)
+                             .collect(Collectors.joining(" ", "", " ")));
             }
         }
 
         private void printFormalTypeParameters(Parameterizable e,
                                                boolean pad) {
             List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
-            if (typeParams.size() > 0) {
-                writer.print("<");
-
-                boolean first = true;
-                for(TypeParameterElement tpe: typeParams) {
-                    if (!first)
-                        writer.print(", ");
-                    printAnnotationsInline(tpe);
-                    writer.print(tpe.toString());
-                    first = false;
-                }
-
-                writer.print(">");
+            if (!typeParams.isEmpty()) {
+                writer.print(typeParams.stream()
+                             .map(tpe -> annotationsToString(tpe) + tpe.toString())
+                             .collect(Collectors.joining(", ", "<", ">")));
                 if (pad)
                     writer.print(" ");
             }
         }
 
-        private void printAnnotationsInline(Element e) {
-            List<? extends AnnotationMirror> annots = e.getAnnotationMirrors();
-            for(AnnotationMirror annotationMirror : annots) {
-                writer.print(annotationMirror);
-                writer.print(" ");
-            }
+        private String annotationsToString(Element e) {
+            List<? extends AnnotationMirror> annotations = e.getAnnotationMirrors();
+            return annotations.isEmpty() ?
+                "" :
+                annotations.stream()
+                .map(AnnotationMirror::toString)
+                .collect(Collectors.joining(" ", "", " "));
         }
 
         private void printAnnotations(Element e) {
@@ -569,17 +563,11 @@
 
             if(kind != ANNOTATION_TYPE) {
                 List<? extends TypeMirror> interfaces = e.getInterfaces();
-                if (interfaces.size() > 0) {
-                    writer.print((kind.isClass() ? " implements" : " extends"));
-
-                    boolean first = true;
-                    for(TypeMirror interf: interfaces) {
-                        if (!first)
-                            writer.print(",");
-                        writer.print(" ");
-                        writer.print(interf.toString());
-                        first = false;
-                    }
+                if (!interfaces.isEmpty()) {
+                    writer.print((kind.isClass() ? " implements " : " extends "));
+                    writer.print(interfaces.stream()
+                                 .map(TypeMirror::toString)
+                                 .collect(Collectors.joining(", ")));
                 }
             }
         }