<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Duplicate class definition error on CtClass.toClass() call
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/aumaster">Carsten Jäger</a> in <i>Javassist</i> - <a href="https://community.jboss.org/message/809164#809164">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>I'm trying to reflect annotation changes to classes.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>When i call the CtClass.toCalls method i always getting following error: <strong>javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of&#160; sun/misc/Launcher$AppClassLoader): attempted&#160; duplicate class definition for name: "ReflectionTest"</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Googl'ing for a solution, told me to provide an<strong> own ClassLoader</strong> when calling the toClass() method. But i found no sample how to implement it...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="text-decoration: underline;">This is my Testcase:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> <strong>1. The class to modify:</strong></p><p><strong><br/></strong></p><p> <span style="font-family: 'courier new', courier; font-size: 8pt;">import javax.persistence.Table;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="font-family: 'courier new', courier; font-size: 8pt;"><strong>@Table(name="xyz")</strong></span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">public class ReflectionTest {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">} </span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>2. The test class:</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import java.util.ArrayList;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import java.util.Collection;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.ClassPool;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.CtClass;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.bytecode.AnnotationsAttribute;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.bytecode.ConstPool;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.bytecode.annotation.Annotation;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javassist.bytecode.annotation.StringMemberValue;</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">import javax.persistence.Table;</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">public class ReflectionSample {</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> <span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160; public static void main(final String[] args) {</span></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; /*</strong></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; * It's no solution to provide just the class names as a string,</strong></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; * what will only work, if no application part accessed the classes already.</strong></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; */</strong></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; final Collection&lt;Class&lt;?&gt;&gt; classes = new ArrayList&lt;Class&lt;?&gt;&gt;();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; classes.add(ReflectionTest.class);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; doReflection(classes);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"><span style="font-size: 10pt;"> </span>&#160;</p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; final ReflectionTest test = new ReflectionTest();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; final Table table = test.getClass().getAnnotation(Table.class);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"><span style="font-size: 10pt;"> </span>&#160;</p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println("name&#160; : " + table.name());</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println("schema: " + table.schema()); // Expecting: "TestSchema"</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160; }</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;"><br/></span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160; private static void doReflection(final Collection&lt;Class&lt;?&gt;&gt; classes) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; final ClassPool classPool = ClassPool.getDefault();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (final Class&lt;?&gt; clazz : classes) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; final CtClass ctClass = classPool.get(clazz.getName());</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; final ConstPool constPool = ctClass.getClassFile().getConstPool();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; final AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; final Table table = (Table) ctClass.getAnnotation(Table.class);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; final Annotation tableAnnotation = new Annotation("javax.persistence.Table", constPool);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tableAnnotation.addMemberValue("name", new StringMemberValue(table.name(), constPool));</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tableAnnotation.addMemberValue("schema", new StringMemberValue("TestSchema", constPool));</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; attribute.addAnnotation(tableAnnotation);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ctClass.getClassFile().addAttribute(attribute);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; // This throws a java.lang.LinkageError ... attempted&#160; duplicate class definition for name: "ReflectionTest"! </span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ctClass.toClass();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (Exception e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">&#160;&#160;&#160; }</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>Any help is appreciated!</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>Tia, Carsten</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>P.S.: See attached ZIP file for the complete sample.<br/></strong></p><p><strong><br/></strong></p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/809164#809164">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Javassist at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>