reflection - Java reflecting nested anonymous classes -
why code return "class java.lang.object" ?
object = new object() { public object b = new object(){ public int c; }; }; system.out.println(a.getclass().getfield("b").gettype());
why inner-inner type lost? how can reflect c field ?
edit:
this 1 works (as pointed out in answers):
a.getclass().getfield("b").get(a) ...
but have invoke getter, there way reflect c reflection meta data?
because b
declared object
:
public object b = ...;
there distinction between type of variable (static type) , type of object referenced variable (runtime type).
field.gettype()
returns static type of field.
if want runtime type of object referenced field, need access object , call getclass()
on (since a
declared object
, therefore b
not visible member have use reflection access it):
system.out.println( a.getclass().getfield("b").get(a).getclass());
update: can't reflect c
without accessing instance of object containing it. that's why these types called anonymous - type containing c
has no name, can't declare field b
field of type.
Comments
Post a Comment