Skip to content

Commit

Permalink
Added FieldAccess#getIndex(Field).
Browse files Browse the repository at this point in the history
This can be used to avoid accessing fields by name.
  • Loading branch information
NathanSweet committed Nov 9, 2016
1 parent fba5d5e commit 16bad2e
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/com/esotericsoftware/reflectasm/FieldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@
public abstract class FieldAccess {
private String[] fieldNames;
private Class[] fieldTypes;
private Field[] fields;

public int getIndex (String fieldName) {
for (int i = 0, n = fieldNames.length; i < n; i++)
if (fieldNames[i].equals(fieldName)) return i;
throw new IllegalArgumentException("Unable to find non-private field: " + fieldName);
}

public int getIndex (Field field) {
for (int i = 0, n = fields.length; i < n; i++)
if (fields[i].equals(field)) return i;
throw new IllegalArgumentException("Unable to find non-private field: " + field);
}

public void set (Object instance, String fieldName, Object value) {
set(instance, getIndex(fieldName), value);
}
Expand All @@ -55,6 +62,14 @@ public int getFieldCount () {
return fieldTypes.length;
}

public Field[] getFields () {
return fields;
}

public void setFields (Field[] fields) {
this.fields = fields;
}

abstract public void set (Object instance, int fieldIndex, Object value);

abstract public void setBoolean (Object instance, int fieldIndex, boolean value);
Expand Down Expand Up @@ -130,10 +145,10 @@ static public FieldAccess get (Class type) {
} catch (ClassNotFoundException ignored2) {
String accessClassNameInternal = accessClassName.replace('.', '/');
String classNameInternal = className.replace('.', '/');

ClassWriter cw = new ClassWriter(0);
cw.visit(V1_1, ACC_PUBLIC + ACC_SUPER, accessClassNameInternal, null, "com/esotericsoftware/reflectasm/FieldAccess",
null);
cw.visit(V1_1, ACC_PUBLIC + ACC_SUPER, accessClassNameInternal, null,
"com/esotericsoftware/reflectasm/FieldAccess", null);
insertConstructor(cw);
insertGetObject(cw, classNameInternal, fields);
insertSetObject(cw, classNameInternal, fields);
Expand Down Expand Up @@ -163,6 +178,7 @@ static public FieldAccess get (Class type) {
FieldAccess access = (FieldAccess)accessClass.newInstance();
access.fieldNames = fieldNames;
access.fieldTypes = fieldTypes;
access.fields = fields.toArray(new Field[fields.size()]);
return access;
} catch (Throwable t) {
throw new RuntimeException("Error constructing field access class: " + accessClassName, t);
Expand Down Expand Up @@ -365,7 +381,8 @@ static private void insertGetString (ClassWriter cw, String classNameInternal, A
mv.visitEnd();
}

static private void insertSetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields, Type primitiveType) {
static private void insertSetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
Type primitiveType) {
int maxStack = 6;
int maxLocals = 4; // See correction below for LLOAD and DLOAD
final String setterMethodName;
Expand Down Expand Up @@ -458,7 +475,8 @@ static private void insertSetPrimitive (ClassWriter cw, String classNameInternal
mv.visitEnd();
}

static private void insertGetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields, Type primitiveType) {
static private void insertGetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
Type primitiveType) {
int maxStack = 6;
final String getterMethodName;
final String typeNameInternal = primitiveType.getDescriptor();
Expand Down

0 comments on commit 16bad2e

Please sign in to comment.