src/share/classes/java/math/BigDecimal.java

Print this page

        

@@ -3714,30 +3714,32 @@
         } else if (val[1].scale < val[0].scale) {
             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
         }
     }
 
-    private static final sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
+    private static class UnsafeHolder {
+        private static final sun.misc.Unsafe unsafe;
     private static final long intCompactOffset;
     private static final long intValOffset;
     static {
         try {
+                unsafe = sun.misc.Unsafe.getUnsafe();
             intCompactOffset = unsafe.objectFieldOffset
                 (BigDecimal.class.getDeclaredField("intCompact"));
             intValOffset = unsafe.objectFieldOffset
                 (BigDecimal.class.getDeclaredField("intVal"));
         } catch (Exception ex) {
-            throw new Error(ex);
+                throw new ExceptionInInitializerError(ex);
         }
     }
-
-    private void setIntCompactVolatile(long val) {
-        unsafe.putLongVolatile(this, intCompactOffset, val);
+        private static void setIntCompactVolatile(BigDecimal bd, long val) {
+            unsafe.putLongVolatile(bd, intCompactOffset, val);
     }
 
-    private void setIntValVolatile(BigInteger val) {
-        unsafe.putObjectVolatile(this, intValOffset, val);
+        private static void setIntValVolatile(BigDecimal bd, BigInteger val) {
+            unsafe.putObjectVolatile(bd, intValOffset, val);
+        }
     }
 
     /**
      * Reconstitute the {@code BigDecimal} instance from a stream (that is,
      * deserialize it).

@@ -3752,11 +3754,11 @@
         if (intVal == null) {
             String message = "BigDecimal: null intVal in stream";
             throw new java.io.StreamCorruptedException(message);
         // [all values of scale are now allowed]
         }
-        setIntCompactVolatile(compactValFor(intVal));
+        UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal));
     }
 
    /**
     * Serialize this {@code BigDecimal} to the stream in question
     *

@@ -3764,11 +3766,11 @@
     */
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
        // Must inflate to maintain compatible serial form.
        if (this.intVal == null)
-           this.setIntValVolatile(BigInteger.valueOf(this.intCompact));
+           UnsafeHolder.setIntValVolatile(this, BigInteger.valueOf(this.intCompact));
        // Could reset intVal back to null if it has to be set.
        s.defaultWriteObject();
    }
 
     /**