Transient fields are not written to TOML files by TomlWriter

Fixes #30
This commit is contained in:
Moandji Ezana 2016-06-14 19:51:22 -04:00
parent ab05197c9d
commit a37dfd80a8
3 changed files with 18 additions and 2 deletions

View file

@ -1,11 +1,15 @@
# toml4j Changelog # toml4j Changelog
## 0.6.0 / 2016-05-09 ## 0.6.0 / 2016-06-14
## Added ## Added
* Toml#toMap() convenience method (thanks to __[andytill](https://github.com/andytill)__ and __[Gyscos](https://github.com/Gyscos)) * Toml#toMap() convenience method (thanks to __[andytill](https://github.com/andytill)__ and __[Gyscos](https://github.com/Gyscos))
## Fixed
* Transient fields are not written to TOML files (thanks to __[lare96](https://github.com/lare96)__)
## 0.5.1 / 2016-01-24 ## 0.5.1 / 2016-01-24
### Fixed ### Fixed

View file

@ -50,7 +50,7 @@ class ObjectValueWriter implements ValueWriter {
Iterator<Field> iterator = fields.iterator(); Iterator<Field> iterator = fields.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Field field = iterator.next(); Field field = iterator.next();
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic()) { if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic() || Modifier.isTransient(field.getModifiers())) {
iterator.remove(); iterator.remove();
} }
} }

View file

@ -456,6 +456,11 @@ public class TomlWriterTest {
int a = 1; int a = 1;
} }
private static class TransientClass {
int a = 2;
transient int b = 3;
}
@Test @Test
public void should_write_to_writer() throws IOException { public void should_write_to_writer() throws IOException {
StringWriter output = new StringWriter(); StringWriter output = new StringWriter();
@ -480,6 +485,13 @@ public class TomlWriterTest {
assertEquals("a = 1\n", readFile(output)); assertEquals("a = 1\n", readFile(output));
} }
@Test
public void should_skip_transient_fields() throws Exception {
String toml = new TomlWriter().write(new TransientClass());
assertEquals("a = 2\n", toml);
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_string_fragment() { public void should_refuse_to_write_string_fragment() {
new TomlWriter().write("fragment"); new TomlWriter().write("fragment");