All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.freshvanilla.lang.misc.UnsafeObjectFieldAccessor Maven / Gradle / Ivy

Go to download

An implementation of OSGi Remote Service Admin, enhanced to provide support for OSGi Promises and streaming using OSGi PushStreams.

The newest version!
package org.freshvanilla.lang.misc;

@SuppressWarnings("restriction")
class UnsafeObjectFieldAccessor implements FieldAccessor {
    private final long offset;

    UnsafeObjectFieldAccessor(long offset) {
        this.offset = offset;
    }

    public  Object getField(Pojo pojo) {
        return Unsafe.unsafe.getObject(pojo, offset);
    }

    public  boolean getBoolean(Pojo pojo) {
        return Boolean.TRUE.equals(Unsafe.unsafe.getObject(pojo, offset));
    }

    public  long getNum(Pojo pojo) {
        Object obj = Unsafe.unsafe.getObject(pojo, offset);
        if (obj instanceof Number) return ((Number)obj).longValue();
        throw new AssertionError("Cannot convert " + obj + " to long.");
    }

    public  double getDouble(Pojo pojo) {
        Object obj = Unsafe.unsafe.getObject(pojo, offset);
        if (obj instanceof Number) return ((Number)obj).doubleValue();
        throw new AssertionError("Cannot convert " + obj + " to double.");
    }

    public  void setField(Pojo pojo, Object value) {
        Unsafe.unsafe.putObject(pojo, offset, value);
    }

    public  void setBoolean(Pojo pojo, boolean value) {
        Unsafe.unsafe.putObject(pojo, offset, value);
    }

    public  void setNum(Pojo pojo, long value) {
        Unsafe.unsafe.putObject(pojo, offset, value);
    }

    public  void setDouble(Pojo pojo, double value) {
        Unsafe.unsafe.putObject(pojo, offset, value);
    }
}