IT_Programming/Java

[펌] Java Serial Version ID

JJun ™ 2009. 5. 22. 18:39

출처: http://iceken.egloos.com/3879162

 

==================================================================================================

Practical Guidelines for Java Serial Version ID and Serialization

Here are practical guidelines for using Java serialization and Serial Version IDs.

  • Add Serial Version IDs to Your Classes Immediately
  • How To Compute a Version ID

Add Serial Version IDs to Your Classes Immediately

You should add a Serial Version ID to your Serializable classes as soon as you create them.

This guarantees that if you make changes to the class and save the class at different times that

you won't end up with different serial version IDs in the saved files.

For example, if you save a object of Class C version 1 (C1) which doesn't have a version ID,

then you modify it to version 2 (C2) and save it, you'll have two saved files, one with C1 and one

with C2 objects. At this point no matter what serial version ID you put in the class, you will be unable

to deserialize one of the saved files.

The caveat to versioning your Serializable classes is that you are then restricted to making so-called "compatible" changes -- adding fields or methods for example, but not removing fields or methods.

How To Compute a Version ID

The serialver tool comes with Sun's Java Development Kit (JDK). It take a full class name on the command line and returns the serial version ID for that compiled class, or can be run with the "-show" parameter to launch a small interactive GUI.

So if your class is com.frequal.Foo, run

serialver com.frequal.Foo

and you'll get output like this:

com.frequal.Foo: static final long serialVersionUID = -6618469841127325812L;

Take the code starting with "static" and place it inside your class with other static variables.

Now the serial version ID is locked in the class.


------------------------------------------------------------------------------------

 

참고: Head First Java 개정판
참고: http://frequal.com/java/PracticalSerialVersionIdGuidelines.html

 

==================================================================================================

 

객체를 serialize 한 후 class가 변경될 한 경우, deserialize 하는 과정에서 호환성 문제가 발생할 수 있다.

 

[호환성 판단 방법]

객체가 serialize 하는 과정에서 해당 객체의 class에 대한 version id를 사용하게 되며, 이 version id를 serialVersionUID라고 한다. class가 변경될 경우, class와 변경 전의 class는 서로 다른 serialVersionUID를

가지므로 호환성 여부를 판단하는데 serialVersionUID가 사용된다. 단, 변경이 되었다고 하더라도 호환성에 문제가 없는 경우가 있을 수 있는데, serialVersionUID를 명시적으로 같이 지정함으로써 class 가 다르더라도 호환성이 문제 없음을 알릴 수 있다. serialVersionUID를 명시적으로 지정하기 위해서는 serialver 명령어를 사용해서 serialVersionUID를 생성한 후, 각 class에 아래와 같이 생성된 serialVersionUID를 선언하면 된다.

 

static final long serialVersionUID = 시리얼버전ID;

 


[호환성 문제를 발생하는 경우]
- instance variable의 삭제
- instance variable의 type 변경
- instance variable의 transient 지정
- instance variable의 static 지정
- class의 serializable 구현 취소
- class의 상속 tree 변경

 

[호환성 문제를 발생하지 않는 경우]
- instance variable의 추가
- instance variable의 transient 지정 취소
- instance variable 접근 level이 deserialize 시 값을 대입하는데 문제 없을 경우
- 상속 tree에서 class 추가
- 상속 tree에서 class 삭제