开发者

Java reflection "blindly" getting values

开发者 https://www.devze.com 2023-03-23 07:18 出处:网络
I am trying to get values from classes by reflection. The thing is开发者_运维百科 that I don\'t always know what Type to cast the value.

I am trying to get values from classes by reflection.

The thing is开发者_运维百科 that I don't always know what Type to cast the value.

Is there a way of knowing?


field.getType()


Cast it to java.lang.Object IMO


You could get the type of the value that is returned, which might be usefull if you might have different types of values assigned to let's say a field that has an interface/superclass type.

Additionally, you might want to look into java.beans.Introspector which would return field descriptors along with the getters and setters. Note that this only works if the introspected class complies with the Java Beans conventions.

0

精彩评论

暂无评论...
验证码 换一张
取 消