I'd like to bind some object instance to a class created using Javassist. This object is read from some source, the data are not known upfront.
// Create the class.
CtClass subClass = pool.makeClass( fullName );
final CtClass superClass = pool.get( Foo.class.getName() );
subClass.setSuperclass( superClass );
// Add a static field containing the definition. // Probably unachievable.
final CtClass defClass = pool.get( SomeMetaData.class.getName() );
CtField defField = new CtField( defClass, "DEF", subClass );
defField.setModifiers( Modifier.STATIC );
subClass.addField( CtField.Initializer.??? );
return subClass.toClass();
But as I checked the API, it seems that Javassist creates a real bytecode, which stores initialization in terms of "call this" or "instantiate that" or "use this constant".
Is there a way to ask Javassist to add a static field initialized to an existing instance given at runtime?