java.lang.reflect.Method 中getWriteMethod() 的使用方法

使用getWriteMethod() 为类的属性赋值。

一、普通类如下:

public class MyBean {
	private String id = null;
	private String userName = null;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}

 

二、测试类如下:

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;
import java.util.HashMap;
import java.util.Map;

public class MethodWrite {
	
	private MyBean beanObj = null;
	
	private BeanInfo bBeanObjInfo = null;
	
	public void TestMethodInvoke(){
		Map userMap = new HashMap();
		userMap.put("id", "1001");
		userMap.put("userName", "isoftstone");
		
		try{
			//实例化一个Bean
			beanObj = new MyBean();
			//依据Bean产生一个相关的BeanInfo类
			bBeanObjInfo = Introspector.getBeanInfo(beanObj.getClass());
			
			PropertyDescriptor[] propertyDesc = bBeanObjInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDesc.length; i++) {
				if (propertyDesc[i].getName().compareToIgnoreCase("class")==0) continue;
				//System.out.print(propertyDesc[i].getName());
				String strValue = (String)userMap.get((String)propertyDesc[i].getName());
				//System.out.println(strValue);
				
				if(strValue != null){
					Object[] oParam = new Object[]{};
					Method mr = propertyDesc[i].getWriteMethod();
					if( mr != null){
						oParam = new String[]{(strValue)};
						try{
							//注意这里的参数。
							mr.invoke(beanObj, oParam);
						}catch(IllegalArgumentException iea){
							System.out.println("参数错误。");
							iea.printStackTrace();
						}
						
					}
				}
				
			}
			
			System.out.println(beanObj.getId());
			System.out.println(beanObj.getUserName());
			
		}catch(IntrospectionException e){
			System.out.println("Java Bean 内省异常。");
		}catch(IllegalAccessException ia){
			System.out.println("参数异常。");
			ia.printStackTrace();
		}catch(InvocationTargetException ie){
			System.out.println("invode异常。");
			ie.printStackTrace();
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MethodWrite().TestMethodInvoke();
	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,`java.lang.reflect.Type`和`java.lang.Class`是两个不同的类型,它们分别表示不同的概念。`java.lang.reflect.Type`表示Java的类型,可以是基本类型、类、接口、数组、泛型等;而`java.lang.Class`表示类的定义,包括类的名称、方法、属性等信息。 如果需要将`java.lang.reflect.Type`转换为`java.lang.Class`,可以通过以下方式实现: ```java import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class TypeToClassDemo { public static void main(String[] args) { // 获取一个泛型类型 Type type = new TypeReference<String>() {}.getType(); // 将泛型类型转换为Class Class<?> clazz = getClass(type); System.out.println(clazz.getName()); // 输出: java.lang.String } public static Class<?> getClass(Type type) { if (type instanceof Class) { // 如果是Class类型,则直接返回 return (Class<?>) type; } else if (type instanceof ParameterizedType) { // 如果是ParameterizedType类型,则获取原始类型 ParameterizedType parameterizedType = (ParameterizedType) type; return (Class<?>) parameterizedType.getRawType(); } else { // 如果是其他类型,则抛出异常 throw new IllegalArgumentException("Unsupported type: " + type); } } // 定义一个泛型类型引用 private static abstract class TypeReference<T> { Type getType() { Type superClass = getClass().getGenericSuperclass(); if (superClass instanceof Class) { throw new IllegalArgumentException("Missing type parameter."); } ParameterizedType parameterized = (ParameterizedType) superClass; return parameterized.getActualTypeArguments()[0]; } } } ``` 在上面的代码,我们首先定义了一个泛型类型`TypeReference<T>`,然后通过`getType()`方法获取泛型类型。接着,我们使用`getClass()`方法将泛型类型转换为`Class`类型。在`getClass()`方法,我们判断了类型是否为`Class`和`ParameterizedType`,并分别进行了处理。最后,我们就成功地将`java.lang.reflect.Type`类型转换为了`java.lang.Class`类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值