使用Java中的反射调用Getter和Setter
时间:2020-02-23 14:34:51 来源:igfitidea点击:
在本教程中,我们将看到如何使用Java中的反射调用getters和setter。
我们已经看到了如何使用Java中的反射来调用方法。
有两种方法可以使用Java中的反射来调用吸气器和Setter。
使用propertyDescriptor.
我们可以使用propertyDescriptor调用使用反射来调用getters和setter。
getter:调用propertyDescriptor Setter上的getReadMethod():propertydescriptor上的call getwriitemethod()。
让我们明白简单的例子。
我们将创建员工对象的对象,然后将调用Getter和Setter。
创建员工.java如下。
package org.igi.theitroad;
public class Employee {
String name;
int age;
public Employee()
{
}
public Employee(String name, int age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
创建InvokegettersetterMain以调用员工对象的Getter和Setter。
package org.igi.theitroad;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InvokeGetterSetterMain {
public static void main(String[] args)
{
InvokeGetterSetterMain igsm=new InvokeGetterSetterMain();
Employee emp1=new Employee();
igsm.invokeSetter(emp1, "name", "John");
igsm.invokeSetter(emp1, "age", 25);
igsm.invokeGetter(emp1, "name");
igsm.invokeGetter(emp1, "age");
}
public void invokeSetter(Object obj, String propertyName, Object variableValue)
{
PropertyDescriptor pd;
try {
pd = new PropertyDescriptor(propertyName, obj.getClass());
Method setter = pd.getWriteMethod();
try {
setter.invoke(obj,variableValue);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
public void invokeGetter(Object obj, String variableName)
{
try {
PropertyDescriptor pd = new PropertyDescriptor(variableName, obj.getClass());
Method getter = pd.getReadMethod();
Object f = getter.invoke(obj);
System.out.println(f);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IntrospectionException e) {
e.printStackTrace();
}
}
}
运行上面的程序时,我们将得到以下输出:
John 25
正如我们所看到的,我们能够使用反思调用Getters和Setter。
使用类的getdeclaredmethods
我们可以搜索任何属性的getter和setter并调用它。
这是一个同样的简单程序。
package org.igi.theitroad;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class InvokeGetterSetterWithMethodsMain {
public static void main(String[] args) {
InvokeGetterSetterWithMethodsMain igsm=new InvokeGetterSetterWithMethodsMain();
Employee emp1=new Employee();
Method methodName= igsm.getMethod("name",emp1.getClass(),"setter");
igsm.invokeSetter(emp1, "Martin",methodName);
Method methodAge= igsm.getMethod("age",emp1.getClass(),"setter");
igsm.invokeSetter(emp1, 28,methodAge);
Method methodNameGet= igsm.getMethod("name",emp1.getClass(),"getter");
igsm.invokeGetter(emp1,methodNameGet);
Method methodAgeGet= igsm.getMethod("age",emp1.getClass(),"getter");
igsm.invokeGetter(emp1,methodAgeGet);
}
public Method getMethod(String variableName,Class aClass,String getterOrSetter)
{
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method:declaredMethods) {
if(getterOrSetter.equalsIgnoreCase("getter"))
{
if(isGetter(method) && method.getName().toUpperCase().contains(variableName.toUpperCase()))
{
return method;
}
}
if(getterOrSetter.equalsIgnoreCase("setter"))
{
if(isSetter(method) && method.getName().toUpperCase().contains(variableName.toUpperCase()))
{
return method;
}
}
}
return null;
}
private static boolean isGetter(Method method){
//check for getter methods
if((method.getName().startsWith("get") || method.getName().startsWith("is"))
&& method.getParameterCount() == 0 && !method.getReturnType().equals(void.class)){
return true;
}
return false;
}
private static boolean isSetter(Method method){
//check for setter methods
if(method.getName().startsWith("set") && method.getParameterCount() == 1
&& method.getReturnType().equals(void.class)){
return true;
}
return false;
}
public void invokeSetter(Object obj,Object variableValue,Method setter)
{
try {
setter.invoke(obj,variableValue);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void invokeGetter(Object obj,Method getter)
{
try {
Object f = getter.invoke(obj);
System.out.println(f);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
运行上面的程序时,我们将得到以下输出:
Martin 28

