0

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.

rohith
  • 733
  • 4
  • 10
  • 24
  • There is no such concept of _composed class_ in the language or in the reflection API. You'll need to get the `C` field and reflect on its type. – Sotirios Delimanolis Dec 21 '15 at 19:58
  • First line from the table from the doc you've linked: "getDeclaredField() - inherited members? no", so why do you expect to get anything for `aId`? – user3707125 Dec 21 '15 at 20:00
  • @user3707125 i just showed what i tried and is not working. i am not expecting getDeclaredField to work on inherited fields. My be my expectation section should be changed.I will edit it. sorry. – rohith Dec 21 '15 at 20:07
  • I don't know what you're expecting, but the best you can hope for is `b.getDeclaredField("c").getType().getDeclaredField("decValue").getType()` to return `BigDecimal.class`. – Louis Wasserman Dec 21 '15 at 20:19

0 Answers0