反射==>获得泛型的实际类型参数

本文介绍了一种使用Java反射机制来获取方法签名中泛型参数实际类型的方法。通过实例演示了如何从带有泛型参数的方法中提取具体类型信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.interview.getGenericArgument;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
 * 通过反射技术得到泛型的实际类型参数
 * @author dhh
 *
 */
public class GetGernericArgument {
	public static void main(String[] args) {
		List<Date> lists = new ArrayList<Date>();
		List<String> lists2 = new ArrayList<String>();
		/**
		 * 如果想通过lists,lists2来得到lists的实际类型参数(也就是Date)
		 * 这是不能实现的,因为泛型就是编译器用来防止开发者不按实际类型胡乱
		 * 添加。它编译完之后成class之后,将没有泛型这个概念,
		 * 因为lists.getClass()==list2.getClass()
		 * 但是如果要得到方法里面的实际类型参数,就可以实现
		*/
		//得到getActualArgument方法的实际类型参数
		try {
			Method method =
				GetGernericArgument.class.getMethod("getActualArgument", ArrayList.class,HashMap.class);
			//得到所有泛型参数类型,不能用method.getParameterTypes(),因为class中不包含泛型类型
			Type[] types = method.getGenericParameterTypes();
			System.out.println("**---------第一个参数-------**");
			//Type包含很多种,我们要得到参数化的Type.ParameterizedType 表示参数化类型,如 Collection<String>
			ParameterizedType paramType = (ParameterizedType)types[0];
			//返回 Type 对象,表示声明此类型的类或接口(原始类型)
			System.out.println(paramType.getRawType());
			//得到第一个参数的类型,如果是HashMap<K,V>将会有两个参数
			//返回表示此类型实际类型参数的 Type 对象的数组。
			System.out.println(paramType.getActualTypeArguments()[0]);  
			
			System.out.println("**---------第二个参数-------**");
			//Type包含很多种,我们要得到参数化的Type.ParameterizedType 表示参数化类型,如 Collection<String>
			ParameterizedType paramType2 = (ParameterizedType)types[1];
			System.out.println(paramType2.getRawType());
			System.out.println(paramType2.getActualTypeArguments()[0]);
			System.out.println(paramType2.getActualTypeArguments()[1]);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过方法得到参数的实际类型参数
	 * @param lists
	 * @param maps
	 */
	public void getActualArgument(ArrayList<Date> lists,HashMap<Integer, String> maps){}
}

public int insertDeptList(List<Map<String,String>> deptList);这是接口, <insert id="insertDeptList" parameterType="java.util.List"> INSERT INTO sys_dept ( <if test="deptList[0].parent_id != null">parent_id,</if> <if test="deptList[0].ancestors != null">ancestors,</if> <if test="deptList[0].dept_name != null">dept_name,</if> <if test="deptList[0].order_num != null">order_num,</if> <if test="deptList[0].leader != null">leader,</if> <if test="deptList[0].phone != null">phone,</if> <if test="deptList[0].email != null">email,</if> <if test="deptList[0].status != null">status,</if> <if test="deptList[0].del_flag != null">del_flag,</if> <if test="deptList[0].create_by != null">create_by,</if> create_time, <if test="deptList[0].update_by != null">update_by,</if> <if test="deptList[0].update_time != null">update_time,</if> <if test="deptList[0].org_index_code != null">org_index_code,</if> <if test="deptList[0].org_no != null">org_no,</if> <if test="deptList[0].org_path != null">org_path,</if> <if test="deptList[0].parent_org_index_code != null">parent_org_index_code,</if> <if test="deptList[0].parent_org_name != null">parent_org_name,</if> ) VALUES <foreach collection="deptList" item="item" separator=","> ( <if test="item.parent_id != null">#{item.parentId},</if> <if test="item.ancestors != null">#{item.ancestors},</if> <if test="item.dept_name != null">#{item.deptName},</if> <if test="item.order_num != null">#{item.orderNum},</if> <if test="item.leader != null">#{item.leader},</if> <if test="item.phone != null">#{item.phone},</if> <if test="item.email != null">#{item.email},</if> <if test="item.status != null">#{item.status},</if> <if test="item.del_flag != null">#{item.delFlag},</if> <if test="item.create_by != null">#{item.createBy},</if> sysdate(), <if test="item.update_by != null">#{item.updateBy},</if> <if test="item.update_time != null">#{item.updateTime},</if> <if test="item.org_index_code != null">#{item.orgIndexCode},</if> <if test="item.org_no != null">#{item.orgNo},</if> <if test="item.org_path != null">#{item.orgPath},</if> <if test="item.parent_org_index_code != null">#{item.parentOrgIndexCode},</if> <if test="item.parent_org_name != null">#{item.parentOrgName},</if> ) </foreach> </insert>这是代码,nested exception is org.apache.ibatis.binding.BindingException: Parameter 'deptList' not found. Available parameters are [arg0, collection, list]这是报错
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值