<!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;"> </p><p>When i call the CtClass.toCalls method i always getting following error: <strong>javassist.CannotCompileException: by java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: "ReflectionTest"</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </p><p><span style="text-decoration: underline;">This is my Testcase:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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;"> </p><p><strong>2. The test class:</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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;"> </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;"> </p><p> <span style="font-family: 'courier new', courier; font-size: 8pt;">    public static void main(final String[] args) {</span></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">        /*</strong></p><p><strong style="font-size: 8pt; font-family: 'courier new', courier;">         * 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;">         * 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;">         */</strong></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        final Collection<Class<?>> classes = new ArrayList<Class<?>>();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        classes.add(ReflectionTest.class);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        doReflection(classes);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"><span style="font-size: 10pt;"> </span> </p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        final ReflectionTest test = new ReflectionTest();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        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> </p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        System.out.println("name  : " + table.name());</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        System.out.println("schema: " + table.schema()); // Expecting: "TestSchema"</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">    }</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;">    private static void doReflection(final Collection<Class<?>> classes) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        final ClassPool classPool = ClassPool.getDefault();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        for (final Class<?> clazz : classes) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">            try {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                final CtClass ctClass = classPool.get(clazz.getName());</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                final ConstPool constPool = ctClass.getClassFile().getConstPool();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                final AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                final Table table = (Table) ctClass.getAnnotation(Table.class);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                final Annotation tableAnnotation = new Annotation("javax.persistence.Table", constPool);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                tableAnnotation.addMemberValue("name", new StringMemberValue(table.name(), constPool));</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                tableAnnotation.addMemberValue("schema", new StringMemberValue("TestSchema", constPool));</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                attribute.addAnnotation(tableAnnotation);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                ctClass.getClassFile().addAttribute(attribute);</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                // This throws a java.lang.LinkageError ... attempted  duplicate class definition for name: "ReflectionTest"! </span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                ctClass.toClass();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">            } catch (Exception e) {</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">                e.printStackTrace();</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">            } </span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">        }</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">    }</span></p><p><span style="font-family: 'courier new', courier; font-size: 8pt;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><strong>Any help is appreciated!</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><strong>Tia, Carsten</strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </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>