changeset 2274:20cb3559e19c

Unify enterAnnotation and enterTypeAnnotation implementations and ensure that both type and attribute are always correctly set.
author wmdietl
date Thu, 28 Feb 2013 14:36:32 -0800
parents 8c7da944ce32
children 0b7e49fa5f26
files src/share/classes/com/sun/tools/javac/comp/Annotate.java
diffstat 1 files changed, 42 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Mon Feb 25 21:02:13 2013 -0800
+++ b/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Thu Feb 28 14:36:32 2013 -0800
@@ -216,18 +216,42 @@
     Attribute.Compound enterAnnotation(JCAnnotation a,
                                        Type expected,
                                        Env<AttrContext> env) {
+        return enterAnnotation(a, expected, env, false);
+    }
+
+    Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a,
+            Type expected,
+            Env<AttrContext> env) {
+        return (Attribute.TypeCompound) enterAnnotation(a, expected, env, true);
+    }
+
+    // boolean typeAnnotation determines whether the method returns
+    // a Compound (false) or TypeCompound (true).
+    Attribute.Compound enterAnnotation(JCAnnotation a,
+            Type expected,
+            Env<AttrContext> env,
+            boolean typeAnnotation) {
         // The annotation might have had its type attributed (but not checked)
         // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
         // need to do it again.
         Type at = (a.annotationType.type != null ? a.annotationType.type
                   : attr.attribType(a.annotationType, env));
         a.type = chk.checkType(a.annotationType.pos(), at, expected);
-        if (a.type.isErroneous())
-            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
+        if (a.type.isErroneous()) {
+            if (typeAnnotation) {
+                return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null);
+            } else {
+                return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
+            }
+        }
         if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
             log.error(a.annotationType.pos(),
                       "not.annotation.type", a.type.toString());
-            return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
+            if (typeAnnotation) {
+                return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null);
+            } else {
+                return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
+            }
         }
         List<JCExpression> args = a.args;
         if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
@@ -266,11 +290,21 @@
                            ((MethodSymbol)method, value));
             t.type = result;
         }
-        // TODO: this should be a TypeCompound if "a" has Tag.TYPE_ANNOTATION.
-        // Also, see enterTypeAnnotation.
-        Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList());
-        a.attribute = ac;
-        return ac;
+        if (typeAnnotation) {
+            if (a.attribute == null || !(a.attribute instanceof Attribute.TypeCompound)) {
+                // Create a new TypeCompound
+                Attribute.TypeCompound tc = new Attribute.TypeCompound(a.type, buf.toList(), new TypeAnnotationPosition());
+                a.attribute = tc;
+                return tc;
+            } else {
+                // Use an existing TypeCompound
+                return a.attribute;
+            }
+        } else {
+            Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList());
+            a.attribute = ac;
+            return ac;
+        }
     }
 
     Attribute enterAttributeValue(Type expected,
@@ -353,19 +387,6 @@
         return new Attribute.Error(attr.attribExpr(tree, env, expected));
     }
 
-    Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a,
-            Type expected,
-            Env<AttrContext> env) {
-        if (a.attribute == null) {
-            Attribute.Compound c = enterAnnotation(a, expected, env);
-            Attribute.TypeCompound tc = new Attribute.TypeCompound(c.type, c.values, new TypeAnnotationPosition());
-            a.attribute = tc;
-            return tc;
-        } else {
-            return (Attribute.TypeCompound) a.attribute;
-        }
-    }
-
     /* *********************************
      * Support for repeating annotations
      ***********************************/