title: Javassist: how to solve a duplicate class exception?
body: <p>Using <code>Javassist</code> I'm trying to insert a line of code into a method's body. It's a simple modification for a label's text color inside the <code>intellij-IDE</code>. I get the following error when trying to do so:</p>
  <blockquote>
  <p><em>Caused by: java.lang.LinkageError: loader com.intellij.util.lang.UrlClassLoader @d2cc05a attempted duplicate class definition for com.intellij.ui.components.labels.LinkLabel.
  (com.intellij.ui.components.labels.LinkLabel is in unnamed module of loader com.intellij.util.lang.UrlClassLoader @d2cc05a, parent loader 'platform')</em></p>
  </blockquote>
  <p>Which implies that the class I am trying to modify is already loaded by the Java class loader (at least I like to think so..).
  An example of the code is given below:</p>
  <pre><code>private static void test() {
          try {
              ClassPool cp = ClassPool.getDefault();
              CtClass cc = cp.get(&quot;com.intellij.ui.components.labels.LinkLabel&quot;);
              CtMethod cm = cc.getDeclaredMethod(&quot;getTextColor&quot;);
              cm.insertBefore(&quot;System.out.println(\&quot;helloworld\&quot;);&quot;);
  
              cc.toClass(); // &lt;--- the problem
          } catch (final Throwable e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  <p>And for reference, here's the accessed class: <a href="https://github.com/JetBrains/intellij-community/blob/d2b4f4abbc5af066875402db4250e988aadfcd76/platform/platform-api/src/com/intellij/ui/components/labels/LinkLabel.java" rel="nofollow noreferrer">intellij link</a></p>
  <hr />
  <p><a href="https://stackoverflow.com/users/1237575/rafael-winterhalter">Rafael Winterhalter</a> commented on a similar issue a couple of years ago (<a href="https://stackoverflow.com/questions/23336172/adding-an-annotation-to-a-runtime-generated-class-using-javassist/23359661#23359661">link</a>). Unfortunately my knowledge with <code>Javassist</code> is limiting me in finding a proper solution.</p>
  <p>Any ideas on how to resolve this issue?</p>
  <hr />
  <p><strong>UPDATE:</strong> A minimal, repoducible example can be found here: <a href="https://github.com/decod3d/example" rel="nofollow noreferrer">github link</a></p>