スキップしてメイン コンテンツに移動

投稿

ラベル(setter)が付いた投稿を表示しています

Java Reflection: Getter and Setter Method

Reflective Getter and Setter Method In this post, I will show you Java code for getting getter and setter method from given object. The code is below: public final static Method getSetterMethod(Object o, String propertyName, Class<?> paramterType) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("set"+toUpperFirstChar(propertyName), paramterType); } public final static Method getGetterMethod(Object o, String propertyName) throws SecurityException, NoSuchMethodException{ return o.getClass().getMethod("get"+toUpperFirstChar(propertyName)); } public final static String toUpperFirstChar(String str){ if(str.isEmpty()) return ""; return str.substring(0, 1).toUpperCase()+str.substring(1, str.length()); } Where to Use Reflective Getter and Setter? You may have a question - "Where should we use reflective getter and setter method?" If you already know where to use them, you can skip the fo...