Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-146

Enum does not support inner sub-classes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0
    • 2.0
    • None
    • None
    • Operating System: other
      Platform: PC

    • 15439

    Description

      The org.apache.commons.lang.enum.Enum class does not support the following
      construct of static inner anonymous sub-classed constants:

      public abstract class MyEnum extends Enum {
      public static final MyEnum ENUM1 = new MyEnum("enum1") {
      public String getValue()

      { return "X"; }

      };

      public static final MyEnum ENUM2 = new MyEnum("enum2") {
      public String getValue()

      { return "Y"; }

      };

      /**

      • Constructor.
        */
        protected MyEnum(String name) { super(name); }

      /**

      • Returns a value.
      • Creation date: (16/12/2002 13:25:35)
        */
        public abstract String getValue();
        }

      ENUM1 and ENUM2 are assigned anonymous inner class names 'MyEnum$0' and
      'MyEnum$1' respectively instead of the super class 'MyEnum' when added to the
      list of enumerations kept in the Enum super class via the Enum constructor.

      A simple work around is to strip the inner class descriminator off the class
      name in the Enum constructor, i.e.

      protected Enum(String name) {
      super();
      if (name == null || name.length() == 0)

      { throw new IllegalArgumentException("The Enum name must not be empty"); }

      iName = name;

      // Retrieve class name.
      String className = getClass().getName();

      // Search for inner class.
      int index = className.lastIndexOf('$');
      if (index > -1)

      { // Strip off inner class reference. className = className.substring(0, index); }

      Entry entry = (Entry) cEnumClasses.get(className);

      if (entry == null)

      { entry = new Entry(); cEnumClasses.put(className, entry); }

      if (entry.map.containsKey(name))

      { throw new IllegalArgumentException("The Enum name must be unique, '" + name + "' has already been added"); }

      entry.map.put(name, this);
      entry.list.add(this);
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            chris.webb@voxsurf.com Chris Webb
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: