如何在不使用setter的情况下将值设置为类变量 [英] How to set values to a class variables without using setters
问题描述
我想在不使用setter的情况下将值插入到 Object
变量中。如果可能的话如何可能。
这是一个例子
Class X {
字符串变量名;
// getters和setters
}
现在我有一个函数,它包含变量名称
,要设置的值
和一个类X的对象 code>。
我正在尝试使用泛型方法将值设置为Object(objectOfClass),并将值传递给我( valueToBeSet $ (code> variableName
)。
对象functionName(String variableName,Object valueToBeSet,Object objectOfClass){
//我想要做与使用以下语句
// objectOfClass设置值时完全相同的内容。 setX的(valueToBeSet);
返回objectOfClass;
}
此代码未经过测试。您可以试试这个。
导入的类
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
方法 I want to insert a value to an This is an example Now i have a function which contains the I am trying to use a generic method to set the value to the Object(objectOfClass) with the value i have passed(
This code is not tested. You can try this. Classes to import Method
这篇关于如何在不使用setter的情况下将值设置为类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< pre $ public Object functionName(String variableName,Object valueToBeSet,Object objectOfClass)throws IntrospectionException,NoSuchMethodException,SecurityException,IllegalAccessException,IllegalArgumentException,InvocationTargetException {
//我想做和使用下面的语句设置值时完全相同的事情
//objectOfClass.setX(valueToBeSet);
Class clazz = objectOfClass.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class); //获取bean信息
PropertyDescriptor [] props = beanInfo.getPropertyDescriptors(); //获取有关该类的所有属性的所有信息。 (PropertyDescriptor描述符:道具)
{
String property = descriptor.getDisplayName();
if(property.equals(variableName)){
String setter = descriptor.getWriteMethod()。getName();
Class parameterType = descriptor.getPropertyType();
方法setterMethod = clazz.getDeclaredMethod(setter,parameterType); //使用方法反射
setterMethod.invoke(objectOfClass,valueToBeSet);
}
}
返回objectOfClass;
}
Object
variable without using the setters. How can if be possible.Class X{
String variableName;
// getters and setters
}
variable name
, the value to be set
and an Object of the Class X
. valueToBeSet
) in the corresponding variable(variableName
).Object functionName(String variableName, Object valueToBeSet, Object objectOfClass){
//I want to do the exact same thing as it does when setting the value using the below statement
//objectOfClass.setX(valueToBeSet);
return objectOfClass;
}
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public Object functionName(String variableName, Object valueToBeSet, Object objectOfClass) throws IntrospectionException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//I want to do the exact same thing as it does when setting the value using the below statement
//objectOfClass.setX(valueToBeSet);
Class clazz = objectOfClass.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class); // get bean info
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); // gets all info about all properties of the class.
for (PropertyDescriptor descriptor : props) {
String property = descriptor.getDisplayName();
if(property.equals(variableName)) {
String setter = descriptor.getWriteMethod().getName();
Class parameterType = descriptor.getPropertyType();
Method setterMethod = clazz.getDeclaredMethod(setter, parameterType); //Using Method Reflection
setterMethod.invoke(objectOfClass, valueToBeSet);
}
}
return objectOfClass;
}