0

There is some legacy Java pojos which was used for binary serialization. Among the fields of one pojo, I have one enum field. Now in the new Java pojo, the enum field is replaced by a string field.

// old pojo with enum class Test { private DataType dataType; private String someOtherField; } enum DataType { Int, Float, String } // new pojo without enum field class NewTest { private String dataType; private String someOtherField; } 

While reading(de-serializing) the old data, I have used the techniques mentioned here - https://stackoverflow.com/a/14608062/314310 to read old data into the new refactored pojo, which performs successfully for the non enum fields. But reading enum data to string field is almost seems impossible. I am getting exception as

java.lang.ClassCastException: cannot assign instance of demo.DataType to field demo.NewTest.dataType of type java.lang.String in instance of demo.NewTest

Is there anyway I can achieve this?

EDIT:

Here is the code for my custom ObjectInputStream

class MyObjectInputStream extends ObjectInputStream { private static final Map<String, Class<?>> migrationMap = new HashMap<>(); static { migrationMap.put("demo.Test", NewTest.class); migrationMap.put("demo.DataType", String.class); } public MyObjectInputStream(InputStream stream) throws IOException { super(stream); } @Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass resultClassDescriptor = super.readClassDescriptor(); for (final String oldName : migrationMap.keySet()) { if (resultClassDescriptor != null && resultClassDescriptor.getName().equals(oldName)) { Class<?> replacement = migrationMap.get(oldName); try { resultClassDescriptor = ObjectStreamClass.lookup(replacement); } catch (Exception e) { log.error("Error while replacing class name." + e.getMessage(), e); } } } return resultClassDescriptor; } } 
2
  • Could you print your converter class ? Because the code you reference (from the answer) does not do any conversion from one type (Enum) to another (String). And I suppose your problem is here: f.set(resultClassDescriptor, replacement); where replacement is DataType where Java expect a String. Commented Dec 18, 2019 at 20:58
  • @NoDataFound edited my question with code. Commented Dec 19, 2019 at 18:24

1 Answer 1

-1

Try changing your enum into this:

enum DataType { Int, Float, String; public static DataType getFromString(String stringDataType) { for(DataType dataType in DataType.values()) { if (dataType.toString().equals(stringDataType)) { return dataType; } } throw new IllegalArgumentException("Invalid input"); } } 

So when you want to assign the Enum to String you call:

newTest.dataType = test.dataType.toString(); 

And when you want to assign the String to Enum, you call:

test.dataType = DataType.getFromString(newTest.dataType); 
Sign up to request clarification or add additional context in comments.

2 Comments

There is always DataType.valueOf(String) for that, especially if toString() is not overridden.
I think you are right. I did not checked this using a compiler. xD

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.