FileDocCategorySizeDatePackage
Unsafe.javaAPI DocAndroid 1.5 API11868Wed May 06 22:41:06 BST 2009sun.misc

Unsafe

public final class Unsafe extends Object
The package name notwithstanding, this class is the quasi-standard way for Java code to gain access to and use functionality which, when unsupervised, would allow one to break the pointer/type safety of Java.

Fields Summary
private static final Unsafe
THE_ONE
non-null; unique instance of this class
private final org.apache.harmony.kernel.vm.LangAccess
lang
non-null; the lang-access utility instance
Constructors Summary
private Unsafe()
This class is only privately instantiable.

    
               
      
        lang = LangAccess.getInstance();
    
Methods Summary
public intarrayBaseOffset(java.lang.Class clazz)
Gets the offset from the start of an array object's memory to the memory used to store its initial (zeroeth) element.

param
clazz non-null; class in question; must be an array class
return
the offset to the initial element

        if (! clazz.isArray()) {
            throw new IllegalArgumentException(
                    "valid for array classes only");
        }

        return arrayBaseOffset0(clazz);
    
private static native intarrayBaseOffset0(java.lang.Class clazz)
Helper for {@link #arrayBaseOffset}, which does all the work, assuming the parameter is deemed valid.

param
field non-null; the instance field
return
the offset to the field

public intarrayIndexScale(java.lang.Class clazz)
Gets the size of each element of the given array class.

param
clazz non-null; class in question; must be an array class
return
> 0; the size of each element of the array

        if (! clazz.isArray()) {
            throw new IllegalArgumentException(
                    "valid for array classes only");
        }

        return arrayIndexScale0(clazz);
    
private static native intarrayIndexScale0(java.lang.Class clazz)
Helper for {@link #arrayIndexScale}, which does all the work, assuming the parameter is deemed valid.

param
field non-null; the instance field
return
the offset to the field

public native booleancompareAndSwapInt(java.lang.Object obj, long offset, int expectedValue, int newValue)
Performs a compare-and-set operation on an int field within the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
expectedValue expected value of the field
param
newValue new value to store in the field if the contents are as expected
return
true if the new value was in fact stored, and false if not

public native booleancompareAndSwapLong(java.lang.Object obj, long offset, long expectedValue, long newValue)
Performs a compare-and-set operation on a long field within the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
expectedValue expected value of the field
param
newValue new value to store in the field if the contents are as expected
return
true if the new value was in fact stored, and false if not

public native booleancompareAndSwapObject(java.lang.Object obj, long offset, java.lang.Object expectedValue, java.lang.Object newValue)
Performs a compare-and-set operation on an Object field (that is, a reference field) within the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
expectedValue expected value of the field
param
newValue new value to store in the field if the contents are as expected
return
true if the new value was in fact stored, and false if not

public native intgetInt(java.lang.Object obj, long offset)
Gets an int field from the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public native intgetIntVolatile(java.lang.Object obj, long offset)
Gets an int field from the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public native longgetLong(java.lang.Object obj, long offset)
Gets a long field from the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public native longgetLongVolatile(java.lang.Object obj, long offset)
Gets a long field from the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public native java.lang.ObjectgetObject(java.lang.Object obj, long offset)
Gets an Object field from the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public native java.lang.ObjectgetObjectVolatile(java.lang.Object obj, long offset)
Gets an Object field from the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
return
the retrieved value

public static sun.misc.UnsafegetUnsafe()
Gets the unique instance of this class. This is only allowed in very limited situations.

        /*
         * Only code on the bootclasspath is allowed to get at the
         * Unsafe instance.
         */
        ClassLoader calling = VMStack.getCallingClassLoader2();
        ClassLoader current = Unsafe.class.getClassLoader();

        if ((calling != null) && (calling != Unsafe.class.getClassLoader())) {
            throw new SecurityException("Unsafe access denied");
        }

        return THE_ONE;
    
public longobjectFieldOffset(java.lang.reflect.Field field)
Gets the raw byte offset from the start of an object's memory to the memory used to store the indicated instance field.

param
field non-null; the field in question, which must be an instance field
return
the offset to the field

        if (Modifier.isStatic(field.getModifiers())) {
            throw new IllegalArgumentException(
                    "valid for instance fields only");
        }

        return objectFieldOffset0(field);
    
private static native longobjectFieldOffset0(java.lang.reflect.Field field)
Helper for {@link #objectFieldOffset}, which does all the work, assuming the parameter is deemed valid.

param
field non-null; the instance field
return
the offset to the field

public voidpark(boolean absolute, long time)
Parks the calling thread for the specified amount of time, unless the "permit" for the thread is already available (due to a previous call to {@link #unpark}. This method may also return spuriously (that is, without the thread being told to unpark and without the indicated amount of time elapsing).

See {@link java.util.concurrent.locks.LockSupport} for more in-depth information of the behavior of this method.

param
absolute whether the given time value is absolute milliseconds-since-the-epoch (true) or relative nanoseconds-from-now (false)
param
time the (absolute millis or relative nanos) time value

        if (absolute) {
            lang.parkUntil(time);
        } else {
            lang.parkFor(time);
        }
    
public native voidputInt(java.lang.Object obj, long offset, int newValue)
Stores an int field into the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public native voidputIntVolatile(java.lang.Object obj, long offset, int newValue)
Stores an int field into the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public native voidputLong(java.lang.Object obj, long offset, long newValue)
Stores a long field into the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public native voidputLongVolatile(java.lang.Object obj, long offset, long newValue)
Stores a long field into the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public native voidputObject(java.lang.Object obj, long offset, java.lang.Object newValue)
Stores an Object field into the given object.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public native voidputObjectVolatile(java.lang.Object obj, long offset, java.lang.Object newValue)
Stores an Object field into the given object, using volatile semantics.

param
obj non-null; object containing the field
param
offset offset to the field within obj
param
newValue the value to store

public voidunpark(java.lang.Object obj)
Unparks the given object, which must be a {@link Thread}.

See {@link java.util.concurrent.locks.LockSupport} for more in-depth information of the behavior of this method.

param
obj non-null; the object to unpark

        if (obj instanceof Thread) {
            lang.unpark((Thread) obj);
        } else {
            throw new IllegalArgumentException("valid for Threads only");
        }