3 Replies Latest reply on Aug 30, 2005 1:36 PM by gpothier

    Strange error generating delegate method in enum

    gpothier Newbie

      I'm getting a very weird error when trying to generate a delegate method with CtNewMethod.delegator for the "equals" method of an enumeration class:
      java.lang.VerifyError: class reflex.test.misc.codemanipulation.Duration overrides final method ³È?.‘.S«?.‘

      I'm attaching a test case.

      Here is the full stack trace:
      javassist.CannotCompileException: by java.lang.VerifyError: class reflex.test.misc.codemanipulation.TestJavassistEnum$MyEnum overrides final method ³È?.‘.S«?.‘
      at javassist.ClassPool.toClass(ClassPool.java:790)
      at javassist.ClassPool.toClass(ClassPool.java:750)
      at javassist.CtClass.toClass(CtClass.java:957)
      at reflex.test.misc.codemanipulation.TestJavassistEnum.testJavassistEnum(TestJavassistEnum.java:29)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at junit.framework.TestCase.runTest(TestCase.java:154)
      at junit.framework.TestCase.runBare(TestCase.java:127)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:118)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: java.lang.VerifyError: class reflex.test.misc.codemanipulation.TestJavassistEnum$MyEnum overrides final method ³È?.‘.S«?.‘
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at javassist.ClassPool.toClass(ClassPool.java:782)
      ... 18 more

        • 1. Re: Strange error generating delegate method in enum
          gpothier Newbie

          Well I don't know how to attach a file so here is the code of the test case:

          package reflex.test.misc.codemanipulation;

          import javassist.ClassPool;
          import javassist.CtClass;
          import javassist.CtMethod;
          import javassist.CtNewMethod;
          import junit.framework.TestCase;

          public class TestJavassistEnum extends TestCase
          {
          private enum MyEnum
          {
          }

          public void testJavassistEnum ()
          {
          try
          {
          ClassPool theClassPool = ClassPool.getDefault();
          CtClass theCtClass = theClassPool.get("reflex.test.misc.codemanipulation.TestJavassistEnum$MyEnum");

          CtMethod theMethod = theCtClass.getMethod("equals", "(Ljava/lang/Object;)Z");
          CtMethod theDelegator = CtNewMethod.delegator(theMethod, theCtClass);

          theCtClass.addMethod(theDelegator);
          theCtClass.toClass();
          }
          catch (Exception e)
          {
          e.printStackTrace();
          throw new RuntimeException(e);
          }
          }
          }

          • 2. Re: Strange error generating delegate method in enum
            Shigeru Chiba Expert

            If an enum type is compiled, it is translated into a class extending
            java.lang.Enum. Since java.lang.Enum declares a final equals()
            method, you cannot add equals() to your enum type.

            • 3. Re: Strange error generating delegate method in enum
              gpothier Newbie

              Ah, ok, thanks! I wasn't aware of that. The error message is really strange anyway...
              Regards,
              Guillaume Pothier