title: Adding an annotation to a runtime generated class using Javassist
body: <p>I'm using Javassist(Java 1.7) to add an annotation to the class ClassA, but i get the exception. What am i doing wrong? The code I tried looks like this:</p>
  
  <p>ClassA.java</p>
  
  <pre><code>public class ClassA
  {
  
  }
  </code></pre>
  
  <p>add method</p>
  
  <pre><code>public static &lt;T&gt; Class&lt;T&gt; addXmlRootAnnotationDynamicly(Class&lt;T&gt; declaredTyp) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException
  {
      //pool creation 
      ClassPool pool = ClassPool.getDefault();
      //extracting the class
      CtClass cc = pool.getCtClass(declaredTyp.getCanonicalName());
  
      // create the annotation
      ClassFile ccFile = cc.getClassFile();
      ConstPool constpool = ccFile.getConstPool();
      AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
      Annotation annot = new Annotation("javax.xml.bind.annotation.XmlRootElement", constpool);
      attr.addAnnotation(annot);
  
      // add the annotation to the class
      cc.getClassFile().addAttribute(attr);
  
      // transform the ctClass to java class
      Class&lt;T&gt; dynamiqueBeanClass = cc.toClass();
  
      //instanciating the updated class 
      //      T sayHelloBean = dynamiqueBeanClass.newInstance();
  
      return dynamiqueBeanClass;
  
  }
  </code></pre>
  
  <p>call </p>
  
  <pre><code>Class&lt;ClassA&gt; addXmlRootAnnotationDynamicly = addXmlRootAnnotationDynamicly(ClassA.class);
  </code></pre>
  
  <p>Exception</p>
  
  <blockquote>
    <p>javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "de/it_p/pvlight/share/util/ClassA"
        at javassist.ClassPool.toClass(ClassPool.java:1099)
        at javassist.ClassPool.toClass(ClassPool.java:1042)
        at javassist.ClassPool.toClass(ClassPool.java:1000)
        at javassist.CtClass.toClass(CtClass.java:1224)
        at de.it_p.pvlight.share.util.JAXBUtil.addXmlRootAnnotationDynamicly(JAXBUtil.java:107)
        at de.it_p.pvlight.share.util.JAXBUtilTest.addXmlRootAnnotationDynamicly(JAXBUtilTest.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "de/it_p/pvlight/share/util/ClassA"
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at javassist.ClassPool.toClass2(ClassPool.java:1112)
        at javassist.ClassPool.toClass(ClassPool.java:1093)
        ... 15 more</p>
  </blockquote>