Reputation: 342
I'm wondering whether it's possible to figure out if some serialVersionUID has been automatically generated (by the JVM) or whether a static one is explictly defined in the class.
Any clue if it's possible to do, and if so how?
Upvotes: 2
Views: 447
Reputation: 691645
Use reflection.Here's an example (I haven't checked the exact contract for the serialVersionUID field, though):
public class SerialTest {
private static class A implements Serializable {
private static final long serialVersionUID = 1L;
}
private static class B implements Serializable {
}
public static void main(String[] args) throws Exception {
System.out.println("A : " + containSerialVersionUID(A.class));
System.out.println("B : " + containSerialVersionUID(B.class));
}
private static boolean containSerialVersionUID(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals("serialVersionUID")
&& field.getType() == Long.TYPE
&& Modifier.isPrivate(field.getModifiers())
&& Modifier.isStatic(field.getModifiers())
&& Modifier.isFinal(field.getModifiers())) {
return true;
}
}
return false;
}
}
Upvotes: 2
Reputation: 38168
Did you try through introspection to check for the field serialUID ? I don't know if the JVM will assign it this way or just store it in a different manner when generating it.
Regards, Stéphane
Upvotes: 0