users@jaxb.java.net

Re: hasEnum & Class.forName in CodeModel

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Wed, 22 Nov 2006 13:05:33 +0100

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?


Try googling for "accessing private fields in Java via reflection". See
Accessible.setAccessible(...).

Bye.
/lexi