mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 03:30:00 +00:00
Include final fields but ignore constants
This commit is contained in:
parent
912ab61158
commit
50522d6a8c
3 changed files with 24 additions and 32 deletions
|
@ -235,7 +235,7 @@ toml.containsTableArray("a"); // false
|
|||
|
||||
### Converting Objects To TOML
|
||||
|
||||
You can write any arbitrary object to a TOML `String`, `File`, `Writer`, or `OutputStream` with a `TomlWriter`. Each TomlWriter instance is customisable, immutable and threadsafe, so it can be reused and passed around.
|
||||
You can write any arbitrary object to a TOML `String`, `File`, `Writer`, or `OutputStream` with a `TomlWriter`. Each TomlWriter instance is customisable, immutable and threadsafe, so it can be reused and passed around. Constants are ignored.
|
||||
|
||||
```java
|
||||
class AClass {
|
||||
|
|
|
@ -5,6 +5,7 @@ import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
|
|||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
|
@ -21,7 +22,7 @@ class ObjectValueWriter implements ValueWriter {
|
|||
@Override
|
||||
public void write(Object value, WriterContext context) {
|
||||
Map<String, Object> to = new LinkedHashMap<String, Object>();
|
||||
Set<Field> fields = getFieldsForClass(value.getClass());
|
||||
Set<Field> fields = getFields(value.getClass());
|
||||
for (Field field : fields) {
|
||||
to.put(field.getName(), getFieldValue(field, value));
|
||||
}
|
||||
|
@ -34,32 +35,28 @@ class ObjectValueWriter implements ValueWriter {
|
|||
return false;
|
||||
}
|
||||
|
||||
static private Set<Field> getFieldsForClass(Class<?> cls) {
|
||||
private static Set<Field> getFields(Class<?> cls) {
|
||||
Set<Field> fields = new LinkedHashSet<Field>(Arrays.asList(cls.getDeclaredFields()));
|
||||
while (cls != Object.class) {
|
||||
fields.addAll(Arrays.asList(cls.getDeclaredFields()));
|
||||
cls = cls.getSuperclass();
|
||||
}
|
||||
removeConstantsAndSyntheticFields(fields);
|
||||
|
||||
getSuperClassFields(cls.getSuperclass(), fields);
|
||||
return fields;
|
||||
}
|
||||
|
||||
// Skip final fields
|
||||
Set<Field> prunedFields = new LinkedHashSet<Field>();
|
||||
for (Field field : fields) {
|
||||
if (!Modifier.isFinal(field.getModifiers())) {
|
||||
prunedFields.add(field);
|
||||
private static void removeConstantsAndSyntheticFields(Set<Field> fields) {
|
||||
Iterator<Field> iterator = fields.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Field field = iterator.next();
|
||||
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic()) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return prunedFields;
|
||||
}
|
||||
|
||||
static private void getSuperClassFields(Class<?> cls, Set<Field> fields) {
|
||||
if (cls == Object.class) {
|
||||
return;
|
||||
}
|
||||
|
||||
fields.addAll(Arrays.asList(cls.getDeclaredFields()));
|
||||
getSuperClassFields(cls.getSuperclass(), fields);
|
||||
}
|
||||
|
||||
static private Object getFieldValue(Field field, Object o) {
|
||||
private static Object getFieldValue(Field field, Object o) {
|
||||
boolean isAccessible = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
Object value = null;
|
||||
|
|
|
@ -41,21 +41,16 @@ public class TomlWriterTest {
|
|||
@Test
|
||||
public void should_write_primitive_types() {
|
||||
class TestClass {
|
||||
public String aString;
|
||||
int anInt;
|
||||
protected float aFloat;
|
||||
private double aDouble;
|
||||
boolean aBoolean;
|
||||
final int aFinalInt = 1; // Should be skipped
|
||||
public String aString = "hello";
|
||||
int anInt = 4;
|
||||
protected float aFloat = 1.23f;
|
||||
private double aDouble = -5.43;
|
||||
final boolean aBoolean = false;
|
||||
static final int aFinalInt = 1; // Should be skipped
|
||||
Date aDate;
|
||||
}
|
||||
|
||||
TestClass o = new TestClass();
|
||||
o.aString = "hello";
|
||||
o.anInt = 4;
|
||||
o.aFloat = 1.23f;
|
||||
o.aDouble = -5.43;
|
||||
o.aBoolean = false;
|
||||
|
||||
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Africa/Johannesburg"));
|
||||
calendar.set(2015, Calendar.JULY, 1, 11, 5, 30);
|
||||
|
|
Loading…
Reference in a new issue