简体   繁体   中英

Using java reflection to set field through its setter method

Assume my class I have a field name called

private long _quantity;

I have created a getter and a setter for it (eg getQty and setQty ).

Is there a way in Java reflection when I use field.set(...) to force it to go through its setter?

I just read up on a tag @Access...does any one have experience with using @Access? can it be used to force a field access into a method?

No, if you want to call your setter, you'll need to call that method and not set the field directly.

If you have a handle on the field itself, set will not go through your method.

否。整个思考的重点是您实际上正在卸下安全带。

Assuming you the field name corresponds to a getter/setter methods, perhaps you could use the getDeclaredMethods, find out the correct method and use it? See http://java.sun.com/developer/technicalArticles/ALT/Reflection/

No, you can't do that with reflection. The only technology on the Java VM that I am aware of that does something similar is AspectJ, it lets you register aspects that intercept field access . But even that won't work with reflection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM