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;
    }
}
Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82
  • 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. – NoDataFound Dec 18 '19 at 20:58
  • @NoDataFound edited my question with code. – Anindya Chatterjee Dec 19 '19 at 18:24

1 Answers1

-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);
Bakmy
  • 81
  • 6