-
Notifications
You must be signed in to change notification settings - Fork 222
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
Kumonda221-CrO3
wants to merge
4
commits into
EsotericSoftware:master
Choose a base branch
from
Kumonda221-CrO3:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/com/esotericsoftware/reflectasm/FieldAccessUnsafe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.