mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +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
|
### 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
|
```java
|
||||||
class AClass {
|
class AClass {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -21,7 +22,7 @@ class ObjectValueWriter implements ValueWriter {
|
||||||
@Override
|
@Override
|
||||||
public void write(Object value, WriterContext context) {
|
public void write(Object value, WriterContext context) {
|
||||||
Map<String, Object> to = new LinkedHashMap<String, Object>();
|
Map<String, Object> to = new LinkedHashMap<String, Object>();
|
||||||
Set<Field> fields = getFieldsForClass(value.getClass());
|
Set<Field> fields = getFields(value.getClass());
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
to.put(field.getName(), getFieldValue(field, value));
|
to.put(field.getName(), getFieldValue(field, value));
|
||||||
}
|
}
|
||||||
|
@ -34,32 +35,28 @@ class ObjectValueWriter implements ValueWriter {
|
||||||
return false;
|
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()));
|
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
|
private static void removeConstantsAndSyntheticFields(Set<Field> fields) {
|
||||||
Set<Field> prunedFields = new LinkedHashSet<Field>();
|
Iterator<Field> iterator = fields.iterator();
|
||||||
for (Field field : fields) {
|
while (iterator.hasNext()) {
|
||||||
if (!Modifier.isFinal(field.getModifiers())) {
|
Field field = iterator.next();
|
||||||
prunedFields.add(field);
|
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic()) {
|
||||||
|
iterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return prunedFields;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static private void getSuperClassFields(Class<?> cls, Set<Field> fields) {
|
private static Object getFieldValue(Field field, Object o) {
|
||||||
if (cls == Object.class) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fields.addAll(Arrays.asList(cls.getDeclaredFields()));
|
|
||||||
getSuperClassFields(cls.getSuperclass(), fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
static private Object getFieldValue(Field field, Object o) {
|
|
||||||
boolean isAccessible = field.isAccessible();
|
boolean isAccessible = field.isAccessible();
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
Object value = null;
|
Object value = null;
|
||||||
|
|
|
@ -41,21 +41,16 @@ public class TomlWriterTest {
|
||||||
@Test
|
@Test
|
||||||
public void should_write_primitive_types() {
|
public void should_write_primitive_types() {
|
||||||
class TestClass {
|
class TestClass {
|
||||||
public String aString;
|
public String aString = "hello";
|
||||||
int anInt;
|
int anInt = 4;
|
||||||
protected float aFloat;
|
protected float aFloat = 1.23f;
|
||||||
private double aDouble;
|
private double aDouble = -5.43;
|
||||||
boolean aBoolean;
|
final boolean aBoolean = false;
|
||||||
final int aFinalInt = 1; // Should be skipped
|
static final int aFinalInt = 1; // Should be skipped
|
||||||
Date aDate;
|
Date aDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
TestClass o = new TestClass();
|
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 calendar = Calendar.getInstance(TimeZone.getTimeZone("Africa/Johannesburg"));
|
||||||
calendar.set(2015, Calendar.JULY, 1, 11, 5, 30);
|
calendar.set(2015, Calendar.JULY, 1, 11, 5, 30);
|
||||||
|
|
Loading…
Reference in a new issue