commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 15439] New: - Enum does not support inner sub-classes
Date Tue, 17 Dec 2002 12:38:22 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15439>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15439

Enum does not support inner sub-classes

           Summary: Enum does not support inner sub-classes
           Product: Commons
           Version: 1.0.1 Final
          Platform: PC
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Lang
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: chris.webb@voxsurf.com


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);
    }

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message