Skip to content

Commit

Permalink
Remove fields by reference, not name.
Browse files Browse the repository at this point in the history
This can matter, eg with Proguard -overloadaggressively.
  • Loading branch information
NathanSweet committed Feb 25, 2014
1 parent a251540 commit 5d9917d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/com/esotericsoftware/kryo/serializers/FieldSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand All @@ -30,9 +31,9 @@
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.util.IntArray;
import com.esotericsoftware.kryo.util.ObjectMap;

import com.esotericsoftware.kryo.util.Util;
import com.esotericsoftware.reflectasm.FieldAccess;

import static com.esotericsoftware.minlog.Log.*;

// BOZO - Make primitive serialization with ReflectASM configurable?
Expand All @@ -51,7 +52,7 @@ public class FieldSerializer<T> extends Serializer<T> implements Comparator<Fiel
final private TypeVariable[] typeParameters;
private CachedField[] fields = new CachedField[0];
private CachedField[] transientFields = new CachedField[0];
protected Map<String, String> removedFields = new HashMap<String, String>();
protected HashSet<CachedField> removedFields = new HashSet();
Object access;
private boolean fieldsCanBeNull = true, setFieldsAsAccessible = true;
private boolean ignoreSyntheticFields = true;
Expand Down Expand Up @@ -222,8 +223,8 @@ protected void rebuildCachedFields () {

if (genericsScope != null) kryo.popGenericsScope();

for (String fieldName : removedFields.keySet())
removeField(fieldName);
for (CachedField field : removedFields)
removeField(field);
}

private List<Field> buildValidFields (boolean transientFields, List<Field> allFields, ObjectMap context, IntArray useAsm) {
Expand Down Expand Up @@ -517,13 +518,29 @@ public void removeField (String fieldName) {
System.arraycopy(fields, 0, newFields, 0, i);
System.arraycopy(fields, i + 1, newFields, i, newFields.length - i);
fields = newFields;
removedFields.put(fieldName, fieldName);
removedFields.add(cachedField);
return;
}
}
throw new IllegalArgumentException("Field \"" + fieldName + "\" not found on class: " + type.getName());
}

/** Removes a field so that it won't be serialized. */
public void removeField (CachedField removeField) {
for (int i = 0; i < fields.length; i++) {
CachedField cachedField = fields[i];
if (cachedField == removeField) {
CachedField[] newFields = new CachedField[fields.length - 1];
System.arraycopy(fields, 0, newFields, 0, i);
System.arraycopy(fields, i + 1, newFields, i, newFields.length - i);
fields = newFields;
removedFields.add(cachedField);
return;
}
}
throw new IllegalArgumentException("Field \"" + removeField + "\" not found on class: " + type.getName());
}

public CachedField[] getFields () {
return fields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void initializeCachedFields () {
Field field = fields[i].getField();
if (field.getAnnotation(Tag.class) == null) {
if (TRACE) trace("kryo", "Ignoring field without tag: " + fields[i]);
super.removeField(field.getName());
super.removeField(fields[i]);
}
}
// Cache tag values.
Expand All @@ -52,7 +52,7 @@ protected void initializeCachedFields () {
writeFieldCount--;
}
}

this.removedFields.clear();
}

Expand All @@ -61,6 +61,11 @@ public void removeField (String fieldName) {
initializeCachedFields();
}

public void removeField (CachedField field) {
super.removeField(field);
initializeCachedFields();
}

public void write (Kryo kryo, Output output, T object) {
CachedField[] fields = getFields();
output.writeVarInt(writeFieldCount, true); // Can be used for null.
Expand Down

0 comments on commit 5d9917d

Please sign in to comment.