Acquring A Static Inner Class via Reflection

Hi,

I am trying to write a test case for a class that has a private instance member which happens to have a type that is defined as a static inner class of the enclosing class, i.e.:

class A{

private B b;

public A(char c){

b =new B(c);

}

privatestaticclass B{

privatechar c;

public B(c){

this.c = c;

}

publicchar getC(){return c;}

}

}

In order to access field `b,' I do the following:

A a =new a('z');

Field field = A.class.getDeclaredField("b");

field.setAccessible(true);

Now, my question is how to access `b' using reflect because I can't return the value of my field -- it is a private instance of `A' after all. My ultimate goal is to have access to `b's' getC() method. I can do the following:

field.get(a).getC();

But that requires a casting from Object to A.B but `B' is a private instance member again. How do I do the casting, Argh? Is there a way to achieve this or I am doomed? I appreciate your help

[1912 byte] By [aria_kokoschkaa] at [2007-11-27 11:15:47]
# 1

Well, you can't simply cast here, but have to reflect some more:

final A a = new A('z');

final Field b_Field = A.class.getDeclaredField("b");

b_Field.setAccessible(true);

final Object b_Obj = b_Field.get(a);

// just continue with reflection here...

final Method getC_Method =

b_Obj.getClass()

.getDeclaredMethod("getC", new Class[0]);

final Object getC_retObj = getC_Method.invoke(b_Obj, null);

final char getC_retVal = ((Character) getC_retObj).charValue();

System.out.println("Reflected c: '" + getC_retVal + '\''); // hopefully, prints 'z'

I'm not sure right now, but I suppose that by setting the field

accessible, you are also allowed to access the field's type (the

private Class object), then...

oeberta at 2007-7-29 14:15:20 > top of Java-index,Core,Core APIs...
# 2

Thanks mate, I guess if it wasn't for that public getter method of inner call B, it would have been impossible to tap into its value. Anyway, this works except that the Method's invoke method requires the second parameter to be explicitly casted to Object[]. That is,

final Object getC_retObj = getC_Method.invoke(b_Obj, (Object[]) null);

Cheers

aria_kokoschkaa at 2007-7-29 14:15:20 > top of Java-index,Core,Core APIs...
# 3

> Thanks mate, I guess if it wasn't for that public getter method of inner call B, it would

> have been impossible to tap into its value.

What holds you back (the private inner class is accessible)? This worked for me:

final Field c_Field = b_Obj.getClass().getDeclaredField("c");

c_Field.setAccessible(true);

final char c_Val = c_Field.getChar(b_Obj);

System.out.println("Reflected c (directly): '" + c_Val + '\'');

> Anyway, this works except that the Method's invoke method requires the second parameter to be

> explicitly casted to Object[].

Oh, I noticed a compiler warning, but didn't really pay attention. I should have. Thanks for pointing that out.

oeberta at 2007-7-29 14:15:20 > top of Java-index,Core,Core APIs...