-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unable to serialize
AtomicBoolean
on Java 17 (#7270)
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
core/src/main/java/jenkins/util/xstream/AtomicBooleanConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jenkins.util.xstream; | ||
|
||
import com.thoughtworks.xstream.converters.Converter; | ||
import com.thoughtworks.xstream.converters.MarshallingContext; | ||
import com.thoughtworks.xstream.converters.SingleValueConverterWrapper; | ||
import com.thoughtworks.xstream.converters.UnmarshallingContext; | ||
import com.thoughtworks.xstream.converters.basic.BooleanConverter; | ||
import com.thoughtworks.xstream.io.HierarchicalStreamReader; | ||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
||
/** Converts an {@link AtomicBoolean}. */ | ||
@Restricted(NoExternalUse.class) | ||
public class AtomicBooleanConverter implements Converter { | ||
|
||
@Override | ||
public boolean canConvert(Class type) { | ||
return type != null && AtomicBoolean.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public void marshal( | ||
Object source, HierarchicalStreamWriter writer, MarshallingContext context) { | ||
final AtomicBoolean atomicBoolean = (AtomicBoolean) source; | ||
writer.startNode("value"); | ||
context.convertAnother( | ||
atomicBoolean.get(), new SingleValueConverterWrapper(BooleanConverter.BINARY)); | ||
writer.endNode(); | ||
} | ||
|
||
@Override | ||
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { | ||
reader.moveDown(); | ||
Object item = | ||
context.convertAnother( | ||
null, | ||
Boolean.class, | ||
new SingleValueConverterWrapper(BooleanConverter.BINARY)); | ||
boolean value = item instanceof Boolean ? ((Boolean) item).booleanValue() : false; | ||
reader.moveUp(); | ||
return new AtomicBoolean(value); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
core/src/test/java/jenkins/util/xstream/AtomicBooleanFieldsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package jenkins.util.xstream; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.thoughtworks.xstream.io.binary.BinaryStreamReader; | ||
import com.thoughtworks.xstream.io.binary.BinaryStreamWriter; | ||
import hudson.util.XStream2; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.junit.Test; | ||
|
||
public class AtomicBooleanFieldsTest { | ||
|
||
public static class Musican { | ||
public String name; | ||
public String genre; | ||
public AtomicBoolean alive; | ||
|
||
public Musican(final String name, final String genre, final AtomicBoolean alive) { | ||
this.name = name; | ||
this.genre = genre; | ||
this.alive = alive; | ||
} | ||
} | ||
|
||
@Test | ||
public void testAtomicBooleanFields() { | ||
List<Musican> jazzIcons = new ArrayList<>(); | ||
jazzIcons.add(new Musican("Miles Davis", "jazz", new AtomicBoolean(false))); | ||
jazzIcons.add(new Musican("Wynton Marsalis", "jazz", new AtomicBoolean(true))); | ||
|
||
XStream2 xstream = new XStream2(); | ||
xstream.alias("musician", Musican.class); | ||
|
||
String xmlString = xstream.toXML(jazzIcons); | ||
assertEquals( | ||
"<list>\n" | ||
+ " <musician>\n" | ||
+ " <name>Miles Davis</name>\n" | ||
+ " <genre>jazz</genre>\n" | ||
+ " <alive>\n" | ||
+ " <value>0</value>\n" | ||
+ " </alive>\n" | ||
+ " </musician>\n" | ||
+ " <musician>\n" | ||
+ " <name>Wynton Marsalis</name>\n" | ||
+ " <genre>jazz</genre>\n" | ||
+ " <alive>\n" | ||
+ " <value>1</value>\n" | ||
+ " </alive>\n" | ||
+ " </musician>\n" | ||
+ "</list>", | ||
xmlString); | ||
List<Musican> obj = (List<Musican>) xstream.fromXML(xmlString); | ||
assertNotNull(obj); | ||
assertEquals(xstream.toXML(jazzIcons), xstream.toXML(obj)); | ||
|
||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
xstream.marshal(jazzIcons, new BinaryStreamWriter(baos)); | ||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | ||
obj = (List<Musican>) xstream.unmarshal(new BinaryStreamReader(bais)); | ||
assertNotNull(obj); | ||
assertEquals(xstream.toXML(jazzIcons), xstream.toXML(obj)); | ||
} | ||
} |