Re: Set serialVersionUID

Matthew Wilson <[email protected]> Sat, 17 Oct 2009 21:11:22 +0100
Newsgroups gmane.comp.jakarta.bcel.user
Message-ID <[email protected]>
On 16/10/2009, JS <[email protected]> wrote:
> How would I set the serialVersionUID using BCEL? I'm using BCEL's
> ClassLoader, overriding modifyClass. I have it adding the Serializable
> interface successfully, but I also need to set the serialVersionUID
> for the class. How would I do so?

Hi,

You want to create a FieldGen with the UID you want and add it to the
ClassGen, e.g.:

ClassGen classGen = new ClassGen( clazz );

// create a FieldGen for 'private static final int serialVersionUID'
FieldGen fieldGen = new FieldGen( Constants.ACC_PRIVATE |
Constants.ACC_FINAL | Constants.ACC_STATIC, Type.INT,
"serialVersionUID", classGen.getConstantPool() );

// set an initial value
fieldGen.setInitValue( mySerialVersionUID );

// add it to the class
classGen.addField( fieldGen.getField() );

JavaClass modifiedClass = classGen.getJavaClass();


I hope that helps.  (I haven't actually compiled and tested that code.)

Kind regards,
Matthew