lundi 21 décembre 2015

Get private field types of current class,composed class,super class using reflection api [duplicate]

This question already has an answer here:

Here is my scenario:

Class A{
private Long aId;
}

Class C{
private BigDecimal decValue;
}

Class B extends A{
private Integer intValue;
private C c;
}

I have an instance of Class B(B b = new B()). with this instance of b i am trying to retrieve type of field aId in classA and type of field decValue in Class C.

I tried: Based on this java doc

  b.getDeclaredField("intValue").getType();//this works
    b.getDeclaredField("aId").getType();//Doesnt work, private field in super class
    b.getDeclaredField("decValue").getType();//Doesnt work, private field in composed

class I also tried getField, this doesnt give access to private fields.

what i am expecting:

 b.getDeclaredField("aId").getType();//should return Long.Class.
  b.getDeclaredField("decValue").getType();//Should return BigDecimal.class

Any help is appreciated.





Aucun commentaire:

Enregistrer un commentaire