|
From: Martin V. (JIRA) <ji...@an...> - 2005-07-11 14:34:03
|
[ http://jira.andromda.org/browse/JAVA-17?page=comments#action_11775 ] Martin Vysny commented on JAVA-17: ---------------------------------- In order for using the == operator safely, you must take care of four things: 1. create an instance of enum constant only once in code, 2. prevent instantiation by hiding the constructor, **** and take care of facilities that allows instantiating without calling a constructor **** 3. ensure that clone does not return clone, it must returns 'this' instead, 4. ensure that deserialization returns appropriate constant instead of new instance of enum constant. This is done nicely in Enum class in Java 1.5. For depth explanation see Java Effective Item 31: Enums (not sure about that number). > Enumeration method from* should return static final object instance > ------------------------------------------------------------------- > > Key: JAVA-17 > URL: http://jira.andromda.org/browse/JAVA-17 > Project: Java Cartridge > Type: Bug > Reporter: Jens Vagts > Assignee: Matthias Bohlen > > The Enumeration method from* returns a new object instance which is not compareable against the static final instances via "==" operator. The following code replaces the current (cvs) code and returns a already created instance: > public static $enumeration.name $enumeration.fromOperationSignature > { > #set ($value = "value") > #if($enumeration.literalType.primitive) > #set ($value = "new ${enumeration.literalType.wrapperName}(value)") > #end > ${enumeration.name} typeValue = (${enumeration.name}) values.get($value); > if (typeValue == null) > { > throw new IllegalArgumentException( > "invalid value '" + value + "', possible values are: " + literals); > } > > return typeValue; > } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://jira.andromda.org/secure/Administrators.jspa - If you want more information on JIRA, or have a bug to report see: http://www.atlassian.com/software/jira |