Adding support for modifiers. jlahoda-tree-builder
authorjlahoda
Tue, 02 Apr 2019 16:51:25 +0200
branchjlahoda-tree-builder
changeset 57300 c79e191854e4
parent 57298 72d5f7163f12
Adding support for modifiers.
src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java
src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java
test/langtools/tools/javac/api/ast/ASTBuilder.java
test/langtools/tools/javac/api/ast/CodeBuilder.java
--- a/src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java	Mon Apr 01 14:46:17 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java	Tue Apr 02 16:51:25 2019 +0200
@@ -151,13 +151,6 @@
         }
     }
 
-//    interface TypeArgs { //???
-//        TypeArgs type(Consumer<Type> t);
-//        TypeArgs _extends(Consumer<Type> t);
-//        TypeArgs _super(Consumer<Type> t);
-//        TypeArgs unbound();
-//    }
-//
     interface Expression {
         void equal_to(Consumer<Expression> lhs, Consumer<Expression> rhs);
         void minusminus(Consumer<Expression> expr);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java	Mon Apr 01 14:46:17 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java	Tue Apr 02 16:51:25 2019 +0200
@@ -27,11 +27,13 @@
 import java.util.function.Consumer;
 import java.util.function.Function;
 
+import javax.lang.model.element.Modifier;
 import javax.tools.JavaFileObject;
 
 import com.sun.source.doctree.DocTree;
 import com.sun.source.tree.CompilationUnitTree;
 import com.sun.source.util.TreeBuilder;
+import com.sun.tools.javac.code.Flags;
 import com.sun.tools.javac.code.TypeTag;
 import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
@@ -47,6 +49,7 @@
 import com.sun.tools.javac.parser.Parser;
 import com.sun.tools.javac.parser.ParserFactory;
 import com.sun.tools.javac.tree.JCTree.JCBlock;
+import com.sun.tools.javac.tree.JCTree.JCModifiers;
 import com.sun.tools.javac.tree.JCTree.JCStatement;
 import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.Log.DiagnosticHandler;
@@ -159,7 +162,8 @@
 
         @Override
         public Class modifiers(Consumer<Modifiers> modifiers) {
-            throw new UnsupportedOperationException("Not supported yet.");
+            modifiers.accept(new ModifiersImpl(result.mods));
+            return this;
         }
 
         @Override
@@ -240,7 +244,8 @@
 
         @Override
         public Variable modifiers(Consumer<Modifiers> modifiers) {
-            throw new UnsupportedOperationException("Not supported yet.");
+            modifiers.accept(new ModifiersImpl(result.mods));
+            return this;
         }
 
         @Override
@@ -291,7 +296,8 @@
 
         @Override
         public Method modifiers(Consumer<Modifiers> modifiers) {
-            throw new UnsupportedOperationException("Not supported yet.");
+            modifiers.accept(new ModifiersImpl(result.mods));
+            return this;
         }
 
         @Override
@@ -481,6 +487,44 @@
 
     }
 
+    private final class ModifiersImpl implements Modifiers {
+
+        private final JCModifiers mods;
+
+        public ModifiersImpl(JCModifiers mods) {
+            this.mods = mods;
+        }
+
+        @Override
+        public Modifiers modifier(Modifier modifier) {
+            long flag;
+            switch (modifier) {
+                case PUBLIC: flag = Flags.PUBLIC; break;
+                case PROTECTED: flag = Flags.PROTECTED; break;
+                case PRIVATE: flag = Flags.PRIVATE; break;
+                case ABSTRACT: flag = Flags.ABSTRACT; break;
+                case STATIC: flag = Flags.STATIC; break;
+                case FINAL: flag = Flags.FINAL; break;
+                case TRANSIENT: flag = Flags.TRANSIENT; break;
+                case VOLATILE: flag = Flags.VOLATILE; break;
+                case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break;
+                case NATIVE: flag = Flags.NATIVE; break;
+                case STRICTFP: flag = Flags.STRICTFP; break;
+                case DEFAULT: flag = Flags.DEFAULT; break;
+                default:
+                    throw new IllegalArgumentException("Unknown modifier: " + modifier);
+            }
+            mods.flags |= flag;
+            return this;
+        }
+
+        @Override
+        public Modifiers annotation(Consumer<Annotation> annotation) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+    }
+
     private JCExpression visitExpression(Consumer<Expression> c) {
         ExpressionImpl expr = new ExpressionImpl();
 
--- a/test/langtools/tools/javac/api/ast/ASTBuilder.java	Mon Apr 01 14:46:17 2019 +0200
+++ b/test/langtools/tools/javac/api/ast/ASTBuilder.java	Tue Apr 02 16:51:25 2019 +0200
@@ -35,8 +35,10 @@
 
 import java.net.URI;
 import java.util.Arrays;
+import java.util.EnumSet;
 import java.util.function.Consumer;
 
+import javax.lang.model.element.Modifier;
 import javax.tools.JavaCompiler;
 import javax.tools.JavaFileObject;
 import javax.tools.SimpleJavaFileObject;
@@ -57,16 +59,27 @@
 import com.sun.source.tree.MemberReferenceTree;
 import com.sun.source.tree.MemberSelectTree;
 import com.sun.source.tree.MethodTree;
+import com.sun.source.tree.ModifiersTree;
 import com.sun.source.tree.VariableTree;
 import toolbox.ToolBox.MemoryFileManager;
 
 public class ASTBuilder {
     
     public static void main(String[] args) throws Exception {
-        runTest("class Test {" +
-                "    int x;" +
+        runTest("public class Test {" +
+                "    public static volatile int x;" +
                 "}",
-                U -> U._class("Test", C -> C.field("x", Type::_int)));
+                U -> U._class("Test",
+                              C -> C.modifiers(M -> M.modifier(Modifier.PUBLIC))
+                                    .field("x",
+                                           Type::_int,
+                                           F -> F.modifiers(M -> M.modifier(Modifier.PUBLIC)
+                                                                  .modifier(Modifier.STATIC)
+                                                                  .modifier(Modifier.VOLATILE)
+                                                           )
+                                          )
+                             )
+                );
         runTest("class Test extends Exception implements java.util.List<Map<String, String>>, CharSequence {" +
                 "    int x1 = 2;" +
                 "    int x2 = 2 + x1;" +
@@ -239,6 +252,11 @@
                 result.append(node.getName());
                 return super.visitVariable(node, p);
             }
+            @Override
+            public Void visitModifiers(ModifiersTree node, Void p) {
+                result.append(node.getFlags());
+                return super.visitModifiers(node, p);
+            }
         }.scan(t, null);
         return result.toString();
     }
--- a/test/langtools/tools/javac/api/ast/CodeBuilder.java	Mon Apr 01 14:46:17 2019 +0200
+++ b/test/langtools/tools/javac/api/ast/CodeBuilder.java	Tue Apr 02 16:51:25 2019 +0200
@@ -187,14 +187,6 @@
                 return null;
             }
 
-//            @Override
-//            public Void visitBlock(BlockTree node, Void p) {
-////                for (StatementTree st : node.getStatements()) {
-////                    result.append(curr)
-////                }
-//                return super.visitBlock(node, p);
-//            }
-
             @Override
             public Void visitIf(IfTree node, Void p) {
                 result.append(currentBuilder() + "._if(");