Uploaded image for project: 'JBoss Modules'
  1. JBoss Modules
  2. MODULES-418

NoClassDefFoundError when using switch statement on enum

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 2.2.0.Final, 2.1.6.Final
    • 1.12.0.Final
    • None
    • None
    • Hide

      The code is loaded from a modudle.xml file into a single module.

      Define an enum:

      public enum BufferType {
        UNTRACKED, TRACKED_SENDER, TRACKED_RECEIVER
      }
      

      Use the enum in a switch in the same class, like in this method:

      void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) {
        switch (type) {
          case UNTRACKED:
            return;
          case TRACKED_SENDER:
            releaseSenderBuffer(buffer);
            return;
          case TRACKED_RECEIVER:
            releaseReceiveBuffer(buffer);
            return;
        }
        throw new IllegalArgumentException("Unexpected buffer type " + type);
      }
      

      The NoClassDefFoundError will be thrown on the line with "switch (type) {".

       

      This does not occur when running without JBoss Modules.

       

      The code snippets I reference come from org.apache.geode.internal.net.BufferPool from https://github.com/apache/geode

      Show
      The code is loaded from a modudle.xml file into a single module. Define an enum: public enum BufferType { UNTRACKED, TRACKED_SENDER, TRACKED_RECEIVER } Use the enum in a switch in the same class, like in this method: void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) { switch (type) { case UNTRACKED: return ; case TRACKED_SENDER: releaseSenderBuffer(buffer); return ; case TRACKED_RECEIVER: releaseReceiveBuffer(buffer); return ; } throw new IllegalArgumentException( "Unexpected buffer type " + type); } The NoClassDefFoundError will be thrown on the line with "switch (type) {".   This does not occur when running without JBoss Modules.   The code snippets I reference come from org.apache.geode.internal.net.BufferPool from https://github.com/apache/geode
    • Workaround Exists
    • Hide

      Replace with switch with an if/else so: 

      void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) {
        switch (type) {
          case UNTRACKED:
            return;
          case TRACKED_SENDER:
            releaseSenderBuffer(buffer);
            return;
          case TRACKED_RECEIVER:
            releaseReceiveBuffer(buffer);
            return;
        }
        throw new IllegalArgumentException("Unexpected buffer type " + type.toString());
      }
      

      becomes:

      void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) {
        if(type.equals(BufferType.UNTRACKED)) {
          return;
        } else if(type.equals(BufferType.TRACKED_SENDER)) {
          releaseSenderBuffer(buffer);
          return;
        } else if(type.equals(BufferType.TRACKED_RECEIVER)) {
          releaseReceiveBuffer(buffer);
          return;
        }
        throw new IllegalArgumentException("Unexpected buffer type " + type);
      }
      

      Ugly, but it works.

      Show
      Replace with switch with an if/else so:  void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) { switch (type) { case UNTRACKED: return ; case TRACKED_SENDER: releaseSenderBuffer(buffer); return ; case TRACKED_RECEIVER: releaseReceiveBuffer(buffer); return ; } throw new IllegalArgumentException( "Unexpected buffer type " + type.toString()); } becomes: void releaseBuffer(BufferPool.BufferType type, @NotNull ByteBuffer buffer) { if (type.equals(BufferType.UNTRACKED)) { return ; } else if (type.equals(BufferType.TRACKED_SENDER)) { releaseSenderBuffer(buffer); return ; } else if (type.equals(BufferType.TRACKED_RECEIVER)) { releaseReceiveBuffer(buffer); return ; } throw new IllegalArgumentException( "Unexpected buffer type " + type); } Ugly, but it works.

      java.lang.NoClassDefFoundError: org/apache/geode/internal/net/BufferPool$1java.lang.NoClassDefFoundError: org/apache/geode/internal/net/BufferPool$1 at org.apache.geode.internal.net.BufferPool.releaseBuffer(BufferPool.java:288) at org.apache.geode.internal.net.ByteBufferVendor$ByteBufferSharingInternalImpl.releaseBuffer(ByteBufferVendor.java:219) at org.apache.geode.internal.net.ByteBufferVendor.dropReference(ByteBufferVendor.java:150) at org.apache.geode.internal.net.ByteBufferVendor.destruct(ByteBufferVendor.java:115) at org.apache.geode.internal.tcp.Connection.run(Connection.java:1519) at java.lang.Thread.run(Thread.java:748)
      

      A NoClassDefFoundError for an anonymous inner class is thrown when using a switch statement on an enum. The problem goes away if the switch is replaced with an if/else. This should not happen, switch statements should not break.

              ropalka Richard Opalka
              jpatrickvm Patrick Johnson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: