changeset 2369:9d01c8d49aa1

8040319: Clean up type annotation exception index generating code in Code.java Summary: Remove bad code from exception index generation for type annotations and make type_index final Reviewed-by: jjg, vromero
author emc
date Thu, 17 Apr 2014 12:17:02 -0400
parents 81f0b8ffce68
children 7736a820af6f 95f5329ecbbb
files src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java src/share/classes/com/sun/tools/javac/jvm/ClassReader.java src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java src/share/classes/com/sun/tools/javac/jvm/Code.java src/share/classes/com/sun/tools/javac/jvm/Gen.java
diffstat 5 files changed, 49 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java	Thu Apr 17 12:17:02 2014 -0400
@@ -151,18 +151,14 @@
     public final int parameter_index;
 
     // For class extends, implements, and throws clauses
+    public final int type_index;
 
-    // This field is effectively final.  However, it needs to be
-    // modified by Gen for the time being.  Do not introduce new
-    // mutations.
-    public int type_index;
-
-    // For exception parameters, index into exception table.
-    // In com.sun.tools.javac.jvm.Gen.genCatch we first set the type_index
-    // to the catch type index - that value is only temporary.
-    // Then in com.sun.tools.javac.jvm.Code.fillExceptionParameterPositions
-    // we use that value to determine the exception table index.
-    public int exception_index = Integer.MIN_VALUE;
+    // For exception parameters, index into exception table.  In
+    // com.sun.tools.javac.jvm.Gen.genCatch, we first use this to hold
+    // the catch type index.  Then in
+    // com.sun.tools.javac.jvm.Code.fillExceptionParameterPositions we
+    // use that value to determine the exception table index.
+    private int exception_index = Integer.MIN_VALUE;
 
     // If this type annotation is within a lambda expression,
     // store a pointer to the lambda expression tree in order
@@ -309,6 +305,38 @@
         isValidOffset = true;
     }
 
+    public boolean hasExceptionIndex() {
+        return exception_index >= 0;
+    }
+
+    public int getExceptionIndex() {
+        Assert.check(exception_index >= 0, "exception_index does not contain a bytecode offset");
+        return exception_index;
+    }
+
+    public void setExceptionIndex(final int exception_index) {
+        Assert.check(hasCatchType(), "exception_index already contains a bytecode offset");
+        Assert.check(exception_index >= 0, "Expected a valid bytecode offset");
+        this.exception_index = exception_index;
+    }
+
+    public boolean hasCatchType() {
+        return exception_index < 0 && exception_index != Integer.MIN_VALUE;
+    }
+
+    public int getCatchType() {
+        Assert.check(hasCatchType(),
+                     "exception_index does not contain a valid catch type");
+        return (-this.exception_index) - 1 ;
+    }
+
+    public void setCatchType(final int catchType) {
+        Assert.check(this.exception_index < 0,
+                     "exception_index already contains a bytecode index");
+        Assert.check(catchType >= 0, "Expected a valid catch type");
+        this.exception_index = -(catchType + 1);
+    }
+
     /**
      * Decode the binary representation for a type path and set
      * the {@code location} field.
--- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Apr 17 12:17:02 2014 -0400
@@ -1542,7 +1542,7 @@
             final int exception_index = nextChar();
             final TypeAnnotationPosition position =
                 TypeAnnotationPosition.exceptionParameter(readTypePath());
-            position.exception_index = exception_index;
+            position.setExceptionIndex(exception_index);
             return position;
         }
         // method receiver
--- a/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Thu Apr 17 12:17:02 2014 -0400
@@ -925,7 +925,7 @@
             break;
         // exception parameter
         case EXCEPTION_PARAMETER:
-            databuf.appendChar(p.exception_index);
+            databuf.appendChar(p.getExceptionIndex());
             break;
         // method receiver
         case METHOD_RECEIVER:
--- a/src/share/classes/com/sun/tools/javac/jvm/Code.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Code.java	Thu Apr 17 12:17:02 2014 -0400
@@ -2148,18 +2148,12 @@
 
             for (Attribute.TypeCompound ta : lv.sym.getRawTypeAttributes()) {
                 TypeAnnotationPosition p = ta.position;
-                // At this point p.type_index contains the catch type index.
-                // Use that index to determine the exception table index.
-                // We can afterwards discard the type_index.
-                // A TA position is shared for all type annotations in the
-                // same location; updating one is enough.
-                // Use -666 as a marker that the exception_index was already updated.
-                Assert.check(p.type_index != Integer.MIN_VALUE,
-                             "type_index of exception parameter type annotation " +
-                             ta + " was not set correctly");
-                if (p.type_index != -666) {
-                    p.exception_index = findExceptionIndex(p.type_index);
-                    p.type_index = -666;
+                if (p.hasCatchType()) {
+                    final int idx = findExceptionIndex(p.getCatchType());
+                    if (idx == -1)
+                        Assert.error("Could not find exception index for type annotation " +
+                                     ta + " on exception parameter");
+                    p.setExceptionIndex(idx);
                 }
             }
         }
--- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Apr 17 12:17:02 2014 -0400
@@ -1647,7 +1647,7 @@
                         if (subCatch.type.isAnnotated()) {
                             for (Attribute.TypeCompound tc :
                                      subCatch.type.getAnnotationMirrors()) {
-                                tc.position.type_index = catchType;
+                                tc.position.setCatchType(catchType);
                             }
                         }
                     }
@@ -1664,7 +1664,7 @@
                         if (subCatch.type.isAnnotated()) {
                             for (Attribute.TypeCompound tc :
                                      subCatch.type.getAnnotationMirrors()) {
-                                tc.position.type_index = catchType;
+                                tc.position.setCatchType(catchType);
                             }
                         }
                     }