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

Print this page

        

@@ -3714,10 +3714,11 @@
         } else if (val[1].scale < val[0].scale) {
             val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
         }
     }
 
+    private static class UnsafeHolder {
     private static final sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe();
     private static final long intCompactOffset;
     private static final long intValOffset;
     static {
         try {

@@ -3727,17 +3728,17 @@
                 (BigDecimal.class.getDeclaredField("intVal"));
         } catch (Exception ex) {
             throw new Error(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 +3753,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 +3765,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();
    }
 
     /**