0

Already existed annotations present at the top of class are removing and whatever i added using Javassist are adding but not taking any effect.

suppose

@Entity
class Master
{

      //variables
     //getters and setters
}     

What i need is to add extra annotation @Table(name="Master",schema="Master_Database") to be added via Javassist dynamically to the above class 'Master'.

What i'm facing is @Entity is removed and @Table is successfully added. This causes loss of functionality of eclipseLink auto generation of entity classes. Plz help me

CaliCo
  • 39
  • 10

1 Answers1

1

It should be as simple as:

ctClass.getClassFile().addAttribute(attributeInfo);

That should not remove existing annotations. For helping you further we would need to know how you added the annotation.

But are you bound to Javassist? Have a look at Byte Buddy (I am the author) which makes adding annotations a rather easy excercise:

TypePool pool = TypePool.Default.ofClassPath();
TypeDescription type = pool.describe("name.of.class");
Class<?> enhanced = new ByteBuddy()
  .redefine(type, ClassFileLocator.ForClassFile.ofClassPath())
  .annotation(new Table() {
    ...
  })
  .make()
  .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
  .getLoaded();
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Hi Rafael, Thanks for your response....ByteBuddy and Javassist helps in adding annotations dynamically but efficient for single or two classes but i have more number of classes, which is not efficient. Anyways Thank you Rafael and thanks for the ByteBuddy suggestion, really great. – CaliCo Jan 18 '15 at 15:16