You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by JS <js...@gmail.com> on 2009/10/16 22:09:34 UTC

Set serialVersionUID

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?

Thanks!

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Set serialVersionUID

Posted by Matthew Wilson <mj...@googlemail.com>.
On 16/10/2009, JS <js...@gmail.com> 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

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org