changeset 2166:d2fa3f7e964e

8006732: support correct bytecode storage of type annotations in multicatch Summary: Fix issue with annotations being added before attribution, which causes multicatch not to work right and several tests to fail. Reviewed-by: jfranck, jjg
author emc
date Wed, 23 Oct 2013 23:20:32 -0400
parents 31fe30e2deac
children 119747cd9f25
files src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/MemberEnter.java src/share/classes/com/sun/tools/javac/jvm/Gen.java test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.java test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.out test/tools/javac/annotations/typeAnnotations/newlocations/MultiCatch.java test/tools/javac/annotations/typeAnnotations/referenceinfos/MultiCatch.java
diffstat 7 files changed, 115 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Oct 23 23:20:32 2013 -0400
@@ -4096,8 +4096,9 @@
     }
 
     private static List<Attribute.TypeCompound> fromAnnotations(List<JCAnnotation> annotations) {
-        if (annotations.isEmpty())
+        if (annotations.isEmpty()) {
             return List.nil();
+        }
 
         ListBuffer<Attribute.TypeCompound> buf = new ListBuffer<>();
         for (JCAnnotation anno : annotations) {
@@ -4109,6 +4110,10 @@
                 // Any better solutions?
                 buf.append((Attribute.TypeCompound) anno.attribute);
             }
+            // Eventually we will want to throw an exception here, but
+            // we can't do that just yet, because it gets triggered
+            // when attempting to attach an annotation that isn't
+            // defined.
         }
         return buf.toList();
     }
--- a/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java	Wed Oct 23 23:20:32 2013 -0400
@@ -573,45 +573,51 @@
 
         Env<AttrContext> localEnv = methodEnv(tree, env);
 
-        DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
+        annotate.enterStart();
         try {
-            // Compute the method type
-            m.type = signature(m, tree.typarams, tree.params,
-                               tree.restype, tree.recvparam,
-                               tree.thrown,
-                               localEnv);
+            DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
+            try {
+                // Compute the method type
+                m.type = signature(m, tree.typarams, tree.params,
+                                   tree.restype, tree.recvparam,
+                                   tree.thrown,
+                                   localEnv);
+            } finally {
+                deferredLintHandler.setPos(prevLintPos);
+            }
+
+            if (types.isSignaturePolymorphic(m)) {
+                m.flags_field |= SIGNATURE_POLYMORPHIC;
+            }
+
+            // Set m.params
+            ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
+            JCVariableDecl lastParam = null;
+            for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
+                JCVariableDecl param = lastParam = l.head;
+                params.append(Assert.checkNonNull(param.sym));
+            }
+            m.params = params.toList();
+
+            // mark the method varargs, if necessary
+            if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
+                m.flags_field |= Flags.VARARGS;
+
+            localEnv.info.scope.leave();
+            if (chk.checkUnique(tree.pos(), m, enclScope)) {
+            enclScope.enter(m);
+            }
+
+            annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
+            // Visit the signature of the method. Note that
+            // TypeAnnotate doesn't descend into the body.
+            typeAnnotate(tree, localEnv, m, tree.pos());
+
+            if (tree.defaultValue != null)
+                annotateDefaultValueLater(tree.defaultValue, localEnv, m);
         } finally {
-            deferredLintHandler.setPos(prevLintPos);
+            annotate.enterDone();
         }
-
-        if (types.isSignaturePolymorphic(m)) {
-            m.flags_field |= SIGNATURE_POLYMORPHIC;
-        }
-
-        // Set m.params
-        ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
-        JCVariableDecl lastParam = null;
-        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
-            JCVariableDecl param = lastParam = l.head;
-            params.append(Assert.checkNonNull(param.sym));
-        }
-        m.params = params.toList();
-
-        // mark the method varargs, if necessary
-        if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
-            m.flags_field |= Flags.VARARGS;
-
-        localEnv.info.scope.leave();
-        if (chk.checkUnique(tree.pos(), m, enclScope)) {
-            enclScope.enter(m);
-        }
-        annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
-        // Visit the signature of the method. Note that
-        // TypeAnnotate doesn't descend into the body.
-        typeAnnotate(tree, localEnv, m, tree.pos());
-
-        if (tree.defaultValue != null)
-            annotateDefaultValueLater(tree.defaultValue, localEnv, m);
     }
 
     /** Create a fresh environment for method bodies.
@@ -639,61 +645,68 @@
             localEnv.info.staticLevel++;
         }
         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
+        annotate.enterStart();
         try {
-            if (TreeInfo.isEnumInit(tree)) {
-                attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
-            } else {
-                attr.attribType(tree.vartype, localEnv);
-                if (tree.nameexpr != null) {
-                    attr.attribExpr(tree.nameexpr, localEnv);
-                    MethodSymbol m = localEnv.enclMethod.sym;
-                    if (m.isConstructor()) {
-                        Type outertype = m.owner.owner.type;
-                        if (outertype.hasTag(TypeTag.CLASS)) {
-                            checkType(tree.vartype, outertype, "incorrect.constructor.receiver.type");
-                            checkType(tree.nameexpr, outertype, "incorrect.constructor.receiver.name");
+            try {
+                if (TreeInfo.isEnumInit(tree)) {
+                    attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
+                } else {
+                    attr.attribType(tree.vartype, localEnv);
+                    if (tree.nameexpr != null) {
+                        attr.attribExpr(tree.nameexpr, localEnv);
+                        MethodSymbol m = localEnv.enclMethod.sym;
+                        if (m.isConstructor()) {
+                            Type outertype = m.owner.owner.type;
+                            if (outertype.hasTag(TypeTag.CLASS)) {
+                                checkType(tree.vartype, outertype, "incorrect.constructor.receiver.type");
+                                checkType(tree.nameexpr, outertype, "incorrect.constructor.receiver.name");
+                            } else {
+                                log.error(tree, "receiver.parameter.not.applicable.constructor.toplevel.class");
+                            }
                         } else {
-                            log.error(tree, "receiver.parameter.not.applicable.constructor.toplevel.class");
+                            checkType(tree.vartype, m.owner.type, "incorrect.receiver.type");
+                            checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name");
                         }
-                    } else {
-                        checkType(tree.vartype, m.owner.type, "incorrect.receiver.type");
-                        checkType(tree.nameexpr, m.owner.type, "incorrect.receiver.name");
                     }
                 }
+            } finally {
+                deferredLintHandler.setPos(prevLintPos);
             }
+
+            if ((tree.mods.flags & VARARGS) != 0) {
+                //if we are entering a varargs parameter, we need to
+                //replace its type (a plain array type) with the more
+                //precise VarargsType --- we need to do it this way
+                //because varargs is represented in the tree as a
+                //modifier on the parameter declaration, and not as a
+                //distinct type of array node.
+                ArrayType atype = (ArrayType)tree.vartype.type.unannotatedType();
+                tree.vartype.type = atype.makeVarargs();
+            }
+            Scope enclScope = enter.enterScope(env);
+            VarSymbol v =
+                new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
+            v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
+            tree.sym = v;
+            if (tree.init != null) {
+                v.flags_field |= HASINIT;
+                if ((v.flags_field & FINAL) != 0 &&
+                    needsLazyConstValue(tree.init)) {
+                    Env<AttrContext> initEnv = getInitEnv(tree, env);
+                    initEnv.info.enclVar = v;
+                    v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
+                }
+            }
+            if (chk.checkUnique(tree.pos(), v, enclScope)) {
+                chk.checkTransparentVar(tree.pos(), v, enclScope);
+                enclScope.enter(v);
+            }
+            annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
+            typeAnnotate(tree.vartype, env, v, tree.pos());
+            v.pos = tree.pos;
         } finally {
-            deferredLintHandler.setPos(prevLintPos);
+            annotate.enterDone();
         }
-
-        if ((tree.mods.flags & VARARGS) != 0) {
-            //if we are entering a varargs parameter, we need to replace its type
-            //(a plain array type) with the more precise VarargsType --- we need
-            //to do it this way because varargs is represented in the tree as a modifier
-            //on the parameter declaration, and not as a distinct type of array node.
-            ArrayType atype = (ArrayType)tree.vartype.type.unannotatedType();
-            tree.vartype.type = atype.makeVarargs();
-        }
-        Scope enclScope = enter.enterScope(env);
-        VarSymbol v =
-            new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
-        v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
-        tree.sym = v;
-        if (tree.init != null) {
-            v.flags_field |= HASINIT;
-            if ((v.flags_field & FINAL) != 0 &&
-                needsLazyConstValue(tree.init)) {
-                Env<AttrContext> initEnv = getInitEnv(tree, env);
-                initEnv.info.enclVar = v;
-                v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
-            }
-        }
-        if (chk.checkUnique(tree.pos(), v, enclScope)) {
-            chk.checkTransparentVar(tree.pos(), v, enclScope);
-            enclScope.enter(v);
-        }
-        annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
-        typeAnnotate(tree.vartype, env, v, tree.pos());
-        v.pos = tree.pos;
     }
     // where
     void checkType(JCTree tree, Type type, String diag) {
--- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Wed Oct 23 23:20:32 2013 -0400
@@ -1652,9 +1652,10 @@
                                       startpc,  end, code.curCP(),
                                       catchType);
                         if (subCatch.type.isAnnotated()) {
-                            // All compounds share the same position, simply update the
-                            // first one.
-                            subCatch.type.getAnnotationMirrors().head.position.type_index = catchType;
+                            for (Attribute.TypeCompound tc :
+                                     subCatch.type.getAnnotationMirrors()) {
+                                tc.position.type_index = catchType;
+                            }
                         }
                     }
                     gaps = gaps.tail;
@@ -1668,9 +1669,10 @@
                                       startpc, endpc, code.curCP(),
                                       catchType);
                         if (subCatch.type.isAnnotated()) {
-                            // All compounds share the same position, simply update the
-                            // first one.
-                            subCatch.type.getAnnotationMirrors().head.position.type_index = catchType;
+                            for (Attribute.TypeCompound tc :
+                                     subCatch.type.getAnnotationMirrors()) {
+                                tc.position.type_index = catchType;
+                            }
                         }
                     }
                 }
--- a/test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.java	Wed Oct 23 23:20:32 2013 -0400
@@ -3,7 +3,6 @@
  * @bug 8006733 8006775
  * @summary Ensure behavior for nested types is correct.
  * @author Werner Dietl
- * @ignore
  * @compile/fail/ref=CantAnnotateStaticClass2.out -XDrawDiagnostics CantAnnotateStaticClass2.java
  */
 
--- a/test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.out	Wed Oct 23 15:45:18 2013 -0700
+++ b/test/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.out	Wed Oct 23 23:20:32 2013 -0400
@@ -24,10 +24,6 @@
 CantAnnotateStaticClass2.java:58:12: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:65:12: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:66:12: compiler.err.cant.type.annotate.scoping.1: @Top.TB
-CantAnnotateStaticClass2.java:105:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
-CantAnnotateStaticClass2.java:107:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
-CantAnnotateStaticClass2.java:112:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
-CantAnnotateStaticClass2.java:114:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:120:14: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:121:14: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:128:14: compiler.err.cant.type.annotate.scoping.1: @Top.TB
@@ -50,6 +46,10 @@
 CantAnnotateStaticClass2.java:167:22: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:169:22: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:171:22: compiler.err.cant.type.annotate.scoping.1: @Top.TB
+CantAnnotateStaticClass2.java:105:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
+CantAnnotateStaticClass2.java:107:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
+CantAnnotateStaticClass2.java:112:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
+CantAnnotateStaticClass2.java:114:18: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:184:35: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:186:41: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:187:41: compiler.err.cant.type.annotate.scoping.1: @Top.TB
@@ -62,4 +62,4 @@
 CantAnnotateStaticClass2.java:202:44: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:203:44: compiler.err.cant.type.annotate.scoping.1: @Top.TB
 CantAnnotateStaticClass2.java:204:49: compiler.err.cant.type.annotate.scoping: @Top.TA,@Top.TB,@Top.TC
-64 errors
\ No newline at end of file
+64 errors
--- a/test/tools/javac/annotations/typeAnnotations/newlocations/MultiCatch.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/test/tools/javac/annotations/typeAnnotations/newlocations/MultiCatch.java	Wed Oct 23 23:20:32 2013 -0400
@@ -25,7 +25,6 @@
 
 /*
  * @test
- * @ignore 8008762 Type annotations failures
  * @bug 8006775
  * @summary new type annotation location: multicatch
  * @author Werner Dietl
--- a/test/tools/javac/annotations/typeAnnotations/referenceinfos/MultiCatch.java	Wed Oct 23 15:45:18 2013 -0700
+++ b/test/tools/javac/annotations/typeAnnotations/referenceinfos/MultiCatch.java	Wed Oct 23 23:20:32 2013 -0400
@@ -25,7 +25,6 @@
 
 /*
  * @test
- * @ignore 8008762 Type annotation failures
  * @bug 8006732 8006775
  * @summary Test population of reference info for multicatch exception parameters
  * @author Werner Dietl