users@jaxb.java.net

RE: Re: hasEnum & Class.forName in CodeModel

From: <HeruMartinus.Salim_at_infineon.com>
Date: Wed, 22 Nov 2006 12:59:52 +0100

> > Is there any Method to check whether a JType (object of type
> > JClass/JDefinedClass) has an enum or not, an to get the
> constants of
> > the enum?
>
> classes() iterates over inner classes.
> getClassType() is the class type, see ClassType.
>
> Enum constants are currently not available publicly, please
> log an issue on codemodel.dev.java.net project. Currently you
> can get it from the private field enumConstantsByName via
> reflection (ugly :( ).
>

Hi,

I've tried to use reflection to get this private field
enumConstantsByName.

Here is the code:

Map<String,JEnumConstant> enumField;
Field fields[] = enumFieldClass.getClass().getDeclaredFields();
for(Field field: fields)
{
        if(field.getName().equals("enumConstantsByName"))
        {
                try{
                        enumField = (Map)field.get(defClass.getClass());
                }catch(java.lang.IllegalAccessException e)
                {
                        e.printStackTrace();
                }
        break;
        }
}

But I got this run-time error message:
[xjc] java.lang.IllegalAccessException: Class
myplugin.renderer.MyFieldRendererFactory can not access a member of
class com.sun.codemodel.JDefinedClass with modifiers "private final"

Does it mean, that I can't get the enumConstant???
Or I have done it wrong?

Thanks,
Heru