Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe field access #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/com/esotericsoftware/reflectasm/FieldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
Expand All @@ -26,8 +26,10 @@
import org.objectweb.asm.Type;

public abstract class FieldAccess {
private String[] fieldNames;
private Class[] fieldTypes;
private static final Object theUnsafe;

protected String[] fieldNames;
protected Class[] fieldTypes;

public int getIndex (String fieldName) {
for (int i = 0, n = fieldNames.length; i < n; i++)
Expand Down Expand Up @@ -572,5 +574,25 @@ static private MethodVisitor insertThrowExceptionForFieldType (MethodVisitor mv,
mv.visitInsn(ATHROW);
return mv;
}


static public FieldAccess getAccessUnsafe(Class<?> type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer new FieldAccessUnsafe(Class) over putting unsafe related code in FieldAccess.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right.And thanks for your notice! I'll try to improve it. Would you like me to write an inner-class in FieldAccess?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsafe would need to be completely separate and accessed in a way that still works when unsafe is not available. However, with unsafe being unsupported in future Java versions, I'm not sure it's worth it.

{
if(theUnsafe == null)
throw new UnsupportedOperationException();

return new FieldAccessUnsafe(type, (sun.misc.Unsafe)theUnsafe);
}

static {
Object unsafe = null;
try {
Class<?> clazz = Class.forName("sun.misc.Unsafe");
Field field = clazz.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = field.get(null);
} catch (Exception e) {
} finally {
theUnsafe = unsafe;
}
}
}
144 changes: 144 additions & 0 deletions src/com/esotericsoftware/reflectasm/FieldAccessUnsafe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.esotericsoftware.reflectasm;

import sun.misc.Unsafe;

import java.lang.reflect.*;

@SuppressWarnings("restriction")
class FieldAccessUnsafe extends FieldAccess {
FieldAccessUnsafe(Class<?> clazz, Unsafe unsafe)
{
this.unsafe = unsafe;

Field[] fields = clazz.getFields();

super.fieldNames = new String[fields.length];
super.fieldTypes = new Class<?>[fields.length];
this.addresses = new long[fields.length];

for(int i = 0; i < fields.length; i++)
{
super.fieldNames[i] = fields[i].getName();
super.fieldTypes[i] = fields[i].getType();
this.addresses[i] = unsafe.objectFieldOffset(fields[i]);
}
}

@Override
public void set(Object instance, int fieldIndex, Object value)
{
unsafe.putObject(instance, addresses[fieldIndex], value);
}

@Override
public void setBoolean(Object instance, int fieldIndex, boolean value)
{
unsafe.putBoolean(instance, addresses[fieldIndex], value);
}

@Override
public void setByte(Object instance, int fieldIndex, byte value)
{
unsafe.putByte(instance, addresses[fieldIndex], value);
}

@Override
public void setShort(Object instance, int fieldIndex, short value)
{
unsafe.putShort(instance, addresses[fieldIndex], value);
}

@Override
public void setInt(Object instance, int fieldIndex, int value)
{
unsafe.putInt(instance, addresses[fieldIndex], value);
}

@Override
public void setLong(Object instance, int fieldIndex, long value)
{
unsafe.putLong(instance, addresses[fieldIndex], value);
}

@Override
public void setDouble(Object instance, int fieldIndex, double value)
{
unsafe.putDouble(instance, addresses[fieldIndex], value);
}

@Override
public void setFloat(Object instance, int fieldIndex, float value)
{
unsafe.putFloat(instance, addresses[fieldIndex], value);
}

@Override
public void setChar(Object instance, int fieldIndex, char value)
{
unsafe.putChar(instance, addresses[fieldIndex], value);
}

@Override
public Object get(Object instance, int fieldIndex)
{
return unsafe.getObject(instance, addresses[fieldIndex]);
}

@Override
public String getString(Object instance, int fieldIndex)
{
return (String)unsafe.getObject(instance, addresses[fieldIndex]);
}

@Override
public char getChar(Object instance, int fieldIndex)
{
return unsafe.getChar(instance, addresses[fieldIndex]);
}

@Override
public boolean getBoolean(Object instance, int fieldIndex)
{
return unsafe.getBoolean(instance, addresses[fieldIndex]);
}

@Override
public byte getByte(Object instance, int fieldIndex)
{
return unsafe.getByte(instance, addresses[fieldIndex]);
}

@Override
public short getShort(Object instance, int fieldIndex)
{
return unsafe.getShort(instance, addresses[fieldIndex]);
}

@Override
public int getInt(Object instance, int fieldIndex)
{
return unsafe.getInt(instance, addresses[fieldIndex]);
}

@Override
public long getLong(Object instance, int fieldIndex)
{
return unsafe.getLong(instance, addresses[fieldIndex]);
}

@Override
public double getDouble(Object instance, int fieldIndex)
{
return unsafe.getDouble(instance, addresses[fieldIndex]);
}

@Override
public float getFloat(Object instance, int fieldIndex)
{
return unsafe.getFloat(instance, addresses[fieldIndex]);
}

long[] addresses;

private final Unsafe unsafe;
}