You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Henri Yandell (JIRA)" <ji...@apache.org> on 2007/01/25 03:59:49 UTC

[jira] Commented: (LANG-76) [lang] EnumUtils.getEnum() doesn't work well in 1.5

    [ https://issues.apache.org/jira/browse/LANG-76?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12467228 ] 

Henri Yandell commented on LANG-76:
-----------------------------------

Alternative workaround from Peter Knuts (received via email):

I have a work around that can be used to mitigate the problem that was not suggested previously.
The solution is simpler than the previously suggested workarounds.

Define a static method inside the defining enum class to load the enum, ex:

public class TestEnum extends Enum {
  private TestEnum(String name) {
    super(name);
  }
  public static final TestEnum VALUE_ONE = new TestEnum("Value one");

  public static TestEnum getEnum(String name) {
    return (TestEnum) EnumUtils.getEnum(TestEnum.class, name);
  }
}

This works fine and the class will be loaded due to the static method call
Java Language Specification:
A class or interface type T will be initialized immediately before the first occurance of any one of the following:
* T is a class and an instance of T is created
* T is a class and a static method declared by T is invoked.
...

The downside of this is that the method can not be implemented in a common super class but needs to be (re)implemented in every enum class that needs this capability (also true for other EnumUtils methods like getEnumList etc).

The upside is that the calling side will be simpler:
  Before: (TestEnum) EnumUtils.getEnum(TestEnum.class, name);
  After: TestEnum.getEnum(name); 

> [lang] EnumUtils.getEnum() doesn't work well in 1.5
> ---------------------------------------------------
>
>                 Key: LANG-76
>                 URL: https://issues.apache.org/jira/browse/LANG-76
>             Project: Commons Lang
>          Issue Type: Bug
>    Affects Versions: 2.1
>         Environment: Operating System: other
> Platform: Other
>            Reporter: Igor Laberov
>             Fix For: 3.0
>
>
> Hi,
> I encountered with problem using EnumUtils.getEnum() in 1.5. It appears that my
> Enum class should be accessed first so constructor will be called. In 1.4 it was
> enough to have myClass.class, so all static members were initialized. In 1.5 it
> doesn't work.
> I noticed that static members are not initialized anymore while acessing to
> class definition. See the code
> public class Test {
>     public static final class TT{
>         public static final TT one = new TT();
>         private TT(){
>             System.out.println("Called TT" );
>         }
>     }
>     
>     public static void main(String[] args) {
>      Class cl = TT.class;
>    // System.out.println( TT.one);
>   //  System.out.println(TT.class.isAssignableFrom(String.class));
>     }
> }
> In 1.4 constructor of TT is called, while in 1.5 is not. 
> Actually, according to the spec
> (http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#57946),
> this is right behavior of Java. 
> Unfortunately, I didn't succeded to think about good solution..
> P.s. I know that in 1.5 we have enum built-in, but it is not the same, and we
> try to move to 1.5 without too much changes

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


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