如何在不使用setter的情况下将值设置为类变量 [英] How to set values to a class variables without using setters

查看:127
本文介绍了如何在不使用setter的情况下将值设置为类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用setter的情况下将值插入到 Object 变量中。如果可能的话如何可能。



这是一个例子

  Class X {
字符串变量名;
// getters和setters
}

现在我有一个函数,它包含变量名称,要设置的和一个类X的对象 code>。



我正在尝试使用泛型方法将值设置为Object(objectOfClass),并将值传递给我( valueToBeSet 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;

方法

< 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;
}


I want to insert a value to an Object variable without using the setters. How can if be possible.

This is an example

Class X{
String variableName;
// getters and setters
}

Now i have a function which contains the variable name, the value to be set and an Object of the Class X.

I am trying to use a generic method to set the value to the Object(objectOfClass) with the value i have passed(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;
}

解决方案

This code is not tested. You can try this.

Classes to import

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;

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;
    }

这篇关于如何在不使用setter的情况下将值设置为类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆