Skip to content

Commit

Permalink
Use declaring class to access fields.
Browse files Browse the repository at this point in the history
This fixes finding the wrong field when a subclass has the same field name as a super class.
  • Loading branch information
NathanSweet committed Jul 16, 2017
1 parent ee6bb50 commit 9e27555
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/com/esotericsoftware/reflectasm/FieldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ static private void insertSetObject (ClassWriter cw, String classNameInternal, A
break;
}

mv.visitFieldInsn(PUTFIELD, classNameInternal, field.getName(), fieldType.getDescriptor());
mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
fieldType.getDescriptor());
mv.visitInsn(RETURN);
}

Expand Down Expand Up @@ -293,7 +294,8 @@ static private void insertGetObject (ClassWriter cw, String classNameInternal, A
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
mv.visitFieldInsn(GETFIELD, classNameInternal, field.getName(), Type.getDescriptor(field.getType()));
mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
Type.getDescriptor(field.getType()));

Type fieldType = Type.getType(field.getType());
switch (fieldType.getSort()) {
Expand Down Expand Up @@ -358,11 +360,13 @@ static private void insertGetString (ClassWriter cw, String classNameInternal, A

for (int i = 0, n = labels.length; i < n; i++) {
if (!labels[i].equals(labelForInvalidTypes)) {
Field field = fields.get(i);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
mv.visitFieldInsn(GETFIELD, classNameInternal, fields.get(i).getName(), "Ljava/lang/String;");
mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
"Ljava/lang/String;");
mv.visitInsn(ARETURN);
}
}
Expand Down Expand Up @@ -451,12 +455,14 @@ static private void insertSetPrimitive (ClassWriter cw, String classNameInternal

for (int i = 0, n = labels.length; i < n; i++) {
if (!labels[i].equals(labelForInvalidTypes)) {
Field field = fields.get(i);
mv.visitLabel(labels[i]);
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
mv.visitVarInsn(loadValueInstruction, 3);
mv.visitFieldInsn(PUTFIELD, classNameInternal, fields.get(i).getName(), typeNameInternal);
mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
typeNameInternal);
mv.visitInsn(RETURN);
}
}
Expand Down Expand Up @@ -546,7 +552,8 @@ static private void insertGetPrimitive (ClassWriter cw, String classNameInternal
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, classNameInternal);
mv.visitFieldInsn(GETFIELD, classNameInternal, field.getName(), typeNameInternal);
mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
typeNameInternal);
mv.visitInsn(returnValueInstruction);
}
}
Expand Down

0 comments on commit 9e27555

Please sign in to comment.