8040319: Clean up type annotation exception index generating code in Code.java
authoremc
Thu, 17 Apr 2014 12:17:02 -0400
changeset 23975 c7c81595aea9
parent 23974 d53628eda3d1
child 23976 16ec7c58cdea
child 24062 09047fd2a43e
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
langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
langtools/src/share/classes/com/sun/tools/javac/jvm/Code.java
langtools/src/share/classes/com/sun/tools/javac/jvm/Gen.java
--- a/langtools/src/share/classes/com/sun/tools/javac/code/TypeAnnotationPosition.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/langtools/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
-
-    // 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;
+    public final 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/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/langtools/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/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/langtools/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/langtools/src/share/classes/com/sun/tools/javac/jvm/Code.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/langtools/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/langtools/src/share/classes/com/sun/tools/javac/jvm/Gen.java	Wed Apr 16 19:21:59 2014 -0700
+++ b/langtools/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);
                             }
                         }
                     }