changeset 2850:6f595e036db7

Improve encapsulation of AnnotatedType - make fields private and only use accessor methods.
author wmdietl
date Sun, 15 Sep 2013 00:27:25 -0700
parents 3a3427ab11fb
children e1772bcdf768
files src/share/classes/com/sun/tools/javac/code/Printer.java src/share/classes/com/sun/tools/javac/code/Type.java src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java src/share/classes/com/sun/tools/javac/code/Types.java src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/Lower.java src/share/classes/com/sun/tools/javac/tree/JCTree.java test/tools/javac/annotations/typeAnnotations/failures/LintCast.out test/tools/javac/lib/DPrinter.java
diffstat 9 files changed, 44 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Printer.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/Printer.java	Sun Sep 15 00:27:25 2013 -0700
@@ -260,24 +260,23 @@
 
     @Override
     public String visitAnnotatedType(AnnotatedType t, Locale locale) {
-        if (t.typeAnnotations != null &&
-                t.typeAnnotations.nonEmpty()) {
-            if (t.underlyingType.hasTag(TypeTag.ARRAY)) {
+        if (t.getAnnotationMirrors().nonEmpty()) {
+            if (t.unannotatedType().hasTag(TypeTag.ARRAY)) {
                 StringBuilder res = new StringBuilder();
                 printBaseElementType(t, res, locale);
                 printBrackets(t, res, locale);
                 return res.toString();
-            } else if (t.underlyingType.hasTag(TypeTag.CLASS) &&
-                    t.underlyingType.getEnclosingType() != Type.noType) {
-                return visit(t.underlyingType.getEnclosingType(), locale) +
+            } else if (t.unannotatedType().hasTag(TypeTag.CLASS) &&
+                    t.unannotatedType().getEnclosingType() != Type.noType) {
+                return visit(t.unannotatedType().getEnclosingType(), locale) +
                         ". " +
-                        t.typeAnnotations +
-                        " " + className((ClassType)t.underlyingType, false, locale);
+                        t.getAnnotationMirrors() +
+                        " " + className((ClassType)t.unannotatedType(), false, locale);
             } else {
-                return t.typeAnnotations + " " + visit(t.underlyingType, locale);
+                return t.getAnnotationMirrors() + " " + visit(t.unannotatedType(), locale);
             }
         } else {
-            return visit(t.underlyingType, locale);
+            return visit(t.unannotatedType(), locale);
         }
     }
 
--- a/src/share/classes/com/sun/tools/javac/code/Type.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/Type.java	Sun Sep 15 00:27:25 2013 -0700
@@ -1806,25 +1806,19 @@
                 javax.lang.model.type.WildcardType {
         /** The type annotations on this type.
          */
-        public List<Attribute.TypeCompound> typeAnnotations;
+        private List<Attribute.TypeCompound> typeAnnotations;
 
         /** The underlying type that is annotated.
          */
-        public Type underlyingType;
+        private Type underlyingType;
 
-        public AnnotatedType(Type underlyingType) {
-            super(underlyingType.tsym);
-            this.typeAnnotations = List.nil();
-            this.underlyingType = underlyingType;
-            Assert.check(!underlyingType.isAnnotated(),
-                    "Can't annotate already annotated type: " + underlyingType);
-        }
-
-        public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
+        protected AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
                 Type underlyingType) {
             super(underlyingType.tsym);
             this.typeAnnotations = typeAnnotations;
             this.underlyingType = underlyingType;
+            Assert.check(typeAnnotations != null && typeAnnotations.nonEmpty(),
+                    "Can't create AnnotatedType without annotations: " + underlyingType);
             Assert.check(!underlyingType.isAnnotated(),
                     "Can't annotate already annotated type: " + underlyingType +
                     "; adding: " + typeAnnotations);
--- a/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Sun Sep 15 00:27:25 2013 -0700
@@ -368,7 +368,7 @@
                 } else {
                     toreturn = tomodify;
                 }
-                
+
                 JCArrayTypeTree arTree = arrayTypeTree(typetree);
 
                 ListBuffer<TypePathEntry> depth = ListBuffer.lb();
@@ -376,11 +376,10 @@
                 while (arType.elemtype.hasTag(TypeTag.ARRAY)) {
                     if (arType.elemtype.isAnnotated()) {
                         Type.AnnotatedType aelemtype = (Type.AnnotatedType) arType.elemtype;
-                        Type.AnnotatedType newAT = (Type.AnnotatedType) aelemtype.underlyingType.annotatedType(arType.elemtype.getAnnotationMirrors());
-                        tomodify.elemtype = newAT;
-                        arType = (Type.ArrayType) aelemtype.underlyingType;
+                        arType = (Type.ArrayType) aelemtype.unannotatedType();
+                        ArrayType prevToMod = tomodify;
                         tomodify = new Type.ArrayType(null, arType.tsym);
-                        newAT.underlyingType = tomodify;
+                        prevToMod.elemtype = (Type.AnnotatedType) tomodify.annotatedType(arType.elemtype.getAnnotationMirrors());
                     } else {
                         arType = (Type.ArrayType) arType.elemtype;
                         tomodify.elemtype = new Type.ArrayType(null, arType.tsym);
@@ -546,7 +545,7 @@
 
                 @Override
                 public Type visitAnnotatedType(AnnotatedType t, List<TypeCompound> s) {
-                    return t.underlyingType.accept(this, s).annotatedType(t.typeAnnotations);
+                    return t.unannotatedType().accept(this, s).annotatedType(t.getAnnotationMirrors());
                 }
 
                 @Override
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Sun Sep 15 00:27:25 2013 -0700
@@ -1274,7 +1274,7 @@
                     return false;
                 if (!s.getAnnotationMirrors().containsAll(t.getAnnotationMirrors()))
                     return false;
-                return visit(t.underlyingType, s);
+                return visit(t.unannotatedType(), s);
             }
         };
     // </editor-fold>
@@ -2208,15 +2208,15 @@
 
             @Override
             public Type visitAnnotatedType(AnnotatedType t, Boolean recurse) {
-                Type erased = erasure(t.underlyingType, recurse);
+                Type erased = erasure(t.unannotatedType(), recurse);
                 if (erased.isAnnotated()) {
                     // This can only happen when the underlying type is a
                     // type variable and the upper bound of it is annotated.
                     // The annotation on the type variable overrides the one
                     // on the bound.
-                    erased = ((AnnotatedType)erased).underlyingType;
+                    erased = ((AnnotatedType)erased).unannotatedType();
                 }
-                return erased.annotatedType(t.typeAnnotations);
+                return erased.annotatedType(t.getAnnotationMirrors());
             }
         };
 
@@ -4387,7 +4387,7 @@
         public R visitUndetVar(UndetVar t, S s)         { return visitType(t, s); }
         public R visitErrorType(ErrorType t, S s)       { return visitType(t, s); }
         // Pretend annotations don't exist
-        public R visitAnnotatedType(AnnotatedType t, S s) { return visit(t.underlyingType, s); }
+        public R visitAnnotatedType(AnnotatedType t, S s) { return visit(t.unannotatedType(), s); }
     }
 
     /**
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Sun Sep 15 00:27:25 2013 -0700
@@ -3525,15 +3525,6 @@
                         Type normOuter = site;
                         if (normOuter.hasTag(CLASS)) {
                             normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
-                            if (site.isAnnotated()) {
-                                // Propagate any type annotations.
-                                // TODO: should asEnclosingSuper do this?
-                                // Note that the type annotations in site will be updated
-                                // by annotateType. Therefore, modify site instead
-                                // of creating a new AnnotatedType.
-                                ((AnnotatedType)site).underlyingType = normOuter;
-                                normOuter = site;
-                            }
                         }
                         if (normOuter == null) // perhaps from an import
                             normOuter = types.erasure(ownOuter);
@@ -3892,12 +3883,6 @@
                     }
                 }
                 owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
-                if (clazztype.isAnnotated()) {
-                    // Use the same AnnotatedType, because it will have
-                    // its annotations set later.
-                    ((AnnotatedType)clazztype).underlyingType = owntype;
-                    owntype = clazztype;
-                }
             } else {
                 if (formals.length() != 0) {
                     log.error(tree.pos(), "wrong.number.type.args",
@@ -3963,9 +3948,7 @@
         TypeVar typeVar = (TypeVar) tree.type;
 
         if (tree.annotations != null && tree.annotations.nonEmpty()) {
-            Type antype = typeVar.annotatedType(List.<Attribute.TypeCompound>nil());
-            annotateType(antype, tree.annotations);
-            tree.type = antype;
+            annotateType(tree, tree.annotations);
         }
 
         if (!typeVar.bound.isErroneous()) {
@@ -4066,27 +4049,28 @@
     public void visitAnnotatedType(JCAnnotatedType tree) {
         Type underlyingType = attribType(tree.getUnderlyingType(), env);
         this.attribAnnotationTypes(tree.annotations, env);
-        Type antype = underlyingType.annotatedType(List.<Attribute.TypeCompound>nil());
-        annotateType(antype, tree.annotations);
-        result = tree.type = antype;
+        annotateType(tree, tree.annotations);
+        result = tree.type = underlyingType;
     }
 
     /**
      * Apply the annotations to the particular type.
      */
-    public void annotateType(final Type type, final List<JCAnnotation> annotations) {
-        Assert.check(type instanceof AnnotatedType);
-        if (annotations.isEmpty())
-            return;
+    public void annotateType(final JCTree tree, final List<JCAnnotation> annotations) {
+        // Callers ensure this.
+        // Assert.check(annotations != null && annotations.nonEmpty());
         annotate.typeAnnotation(new Annotate.Annotator() {
             @Override
             public String toString() {
-                return "annotate " + annotations + " onto " + type;
+                return "annotate " + annotations + " onto " + tree;
             }
             @Override
             public void enterAnnotation() {
                 List<Attribute.TypeCompound> compounds = fromAnnotations(annotations);
-                ((AnnotatedType) type).typeAnnotations = compounds;
+                if (annotations.size() == compounds.size()) {
+                    // All annotations were successfully converted into compounds
+                    tree.type = tree.type.unannotatedType().annotatedType(compounds);
+                }
             }
         });
     }
--- a/src/share/classes/com/sun/tools/javac/comp/Lower.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java	Sun Sep 15 00:27:25 2013 -0700
@@ -2800,19 +2800,9 @@
         tree.underlyingType = translate(tree.underlyingType);
         // but maintain type annotations in the type.
         if (tree.type.isAnnotated()) {
-            if (tree.underlyingType.type.isAnnotated()) {
-                // The erasure of a type variable might be annotated.
-                // Merge all annotations.
-                AnnotatedType newat = (AnnotatedType) tree.underlyingType.type;
-                AnnotatedType at = (AnnotatedType) tree.type;
-                at.underlyingType = newat.underlyingType;
-                newat.typeAnnotations = at.typeAnnotations.appendList(newat.typeAnnotations);
-                tree.type = newat;
-            } else {
-                // Create a new AnnotatedType to have the correct tag.
-                AnnotatedType oldat = (AnnotatedType) tree.type;
-                tree.type = tree.underlyingType.type.annotatedType(oldat.typeAnnotations);
-            }
+            tree.type = tree.underlyingType.type.unannotatedType().annotatedType(tree.type.getAnnotationMirrors());
+        } else if (tree.underlyingType.type.isAnnotated()) {
+            tree.type = tree.underlyingType.type;
         }
         result = tree;
     }
--- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java	Sun Sep 15 00:27:25 2013 -0700
@@ -2360,6 +2360,7 @@
         public JCExpression underlyingType;
 
         protected JCAnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
+            Assert.check(annotations != null && annotations.nonEmpty());
             this.annotations = annotations;
             this.underlyingType = underlyingType;
         }
--- a/test/tools/javac/annotations/typeAnnotations/failures/LintCast.out	Sun Sep 15 00:23:06 2013 -0700
+++ b/test/tools/javac/annotations/typeAnnotations/failures/LintCast.out	Sun Sep 15 00:27:25 2013 -0700
@@ -1,11 +1,11 @@
 LintCast.java:15:21: compiler.warn.redundant.cast: java.lang.String
 LintCast.java:21:27: compiler.warn.redundant.cast: java.util.List<java.lang.String>
-LintCast.java:27:20: compiler.warn.redundant.cast: int @A []
+LintCast.java:27:20: compiler.warn.redundant.cast: int[]
 LintCast.java:39:24: compiler.warn.redundant.cast: java.lang.String
 LintCast.java:40:26: compiler.warn.redundant.cast: java.lang.String
-LintCast.java:45:23: compiler.warn.redundant.cast: java.lang.Object @A []
+LintCast.java:45:23: compiler.warn.redundant.cast: java.lang.Object[]
 LintCast.java:49:27: compiler.warn.redundant.cast: java.util.List<java.lang.String>
-LintCast.java:53:27: compiler.warn.redundant.cast: java.util.List<@A java.lang.String>
+LintCast.java:53:27: compiler.warn.redundant.cast: java.util.List<java.lang.String>
 LintCast.java:57:21: compiler.warn.redundant.cast: java.lang.Object
 LintCast.java:61:27: compiler.warn.redundant.cast: LintCast.Outer.Inner
 10 warnings
\ No newline at end of file
--- a/test/tools/javac/lib/DPrinter.java	Sun Sep 15 00:23:06 2013 -0700
+++ b/test/tools/javac/lib/DPrinter.java	Sun Sep 15 00:27:25 2013 -0700
@@ -946,8 +946,8 @@
      */
     public class TypeVisitor implements Type.Visitor<Void,Void> {
         public Void visitAnnotatedType(AnnotatedType type, Void ignore) {
-            printList("typeAnnotations", type.typeAnnotations);
-            printType("underlyingType", type.underlyingType, Details.FULL);
+            printList("typeAnnotations", type.getAnnotationMirrors());
+            printType("underlyingType", type.unannotatedType(), Details.FULL);
             return visitType(type, null);
         }