
de.apaxo.test.Serialize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-bean-to-code-serializer Show documentation
Show all versions of java-bean-to-code-serializer Show documentation
This code takes a java bean and tries to serialize it as java code that sets recursively all the fields. This is useful if you want to mock an object and already get the object somewhere but you want to put it in the code.
package de.apaxo.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class is a shortcut to use the {@link Object2CodeObjectOutputStream}
* class.
*
* @author Manue Blechschmidt
*
*/
public class Serialize {
private static final Logger log = Logger.getLogger(Serialize.class
.getName());
/**
* This function serializes an object directly to code. You can use a static
* import to use it.
*
* import static de.apaxo.test.Serialize.object2code;
*
* System.out.println(object2code(myBean));
*
* @param o the object to serialze
* @return a string containing the java code for creating the object
*/
public static String object2code(Object o) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (Object2CodeObjectOutputStream object2CodeObjectOutputStream = new Object2CodeObjectOutputStream(
byteArrayOutputStream)) {
object2CodeObjectOutputStream.writeObject(o);
return byteArrayOutputStream.toString();
} catch (SecurityException e) {
log.log(Level.WARNING, "Excetion was thrown", e);
throw new RuntimeException(e);
} catch (IOException e) {
log.log(Level.WARNING, "Excetion was thrown", e);
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy