CaliCo
CaliCo

Reputation: 39

Javassist overwriting existing annotations

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

Upvotes: 0

Views: 1081

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44042

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();

Upvotes: 1

Related Questions