A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡智 中级黑马   /  2013-8-20 09:21  /  1188 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 胡智 于 2013-8-20 09:27 编辑

用反射获取某个类中的某个方法可以获取,但是如何通过反射获取某个类中的带有泛型参数的方法?比如获取下面代码中的method方法?
  1. import java.lang.reflect.Method;
  2. class DemoReflectP{
  3.         public <T> void method(T[] arr){
  4.                 System.out.println(arr.length);
  5.         }
  6.         public int add(int a,int b){
  7.                 return a+b;
  8.         }
  9. }
  10. class ClassDemo {
  11.         public static void main(String[] args)throws Exception{
  12.         DemoReflectP de = new DemoReflectP();
  13.         Method me2 = DemoReflectP.class.getMethod("add",int.class,int.class);
  14.         System.out.println(me2.invoke(de, 2, 4));
  15. }
复制代码

4 个回复

倒序浏览
package com.zhiming.tree;  
  
import java.lang.reflect.Method;  
import java.lang.reflect.ParameterizedType;  
import java.lang.reflect.Type;  
import java.util.Date;  
import java.util.Vector;  
//E为任意参数类型,在类名中加<E>可起到控制类中方法参数使用,和方法返回值的类型统一  
public class ProductDao<E>  
{  
  
    public void add(E x)  
    {  
         
    }  
    public void delete(E x)  
    {  
  
    }  
  
    public E findByCondition(String name)  
    {  
        return null;  
    }  
    public static void applyVector(Vector<Date>  v1)  
    {  
         
    }  
    public static void main(String[] args) throws SecurityException, NoSuchMethodException  
    {  
        Vector<Date>  v1=new Vector<Date>();  
        //无法直接通过反射获得vector<Date>泛型的参数类型,只有通过方法去获得  
        Method method=ProductDao.class.getMethod("applyVector", Vector.class);  
        Type[] types = method.getGenericParameterTypes();  
        ParameterizedType paratype=(ParameterizedType)types[0];  
        System.out.println(paratype.getRawType());  
        System.out.println(paratype.getActualTypeArguments()[0]);  
    }  
      
}  
回复 使用道具 举报

mport java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class TempTest {
    public static void main(String[] args) throws Exception {
        Method[] methods = TempTest.class.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("method:" + method.getName());// 方法名

            // //////////////方法的参数
            System.out.println(" paramTypeType: ");
            Type[] paramTypeList = method.getGenericParameterTypes();// 方法的参数列表
            for (Type paramType : paramTypeList) {
                System.out.println("  " + paramType);// 参数类型
                if (paramType instanceof ParameterizedType)/* 如果是泛型类型 */{
                    Type[] types = ((ParameterizedType) paramType)
                            .getActualTypeArguments();// 泛型类型列表
                    System.out.println("  TypeArgument: ");
                    for (Type type : types) {
                        System.out.println("   " + type);
                    }
                }
            }

            // //////////////方法的返回值
        System.out.println(" returnType: ");
            Type returnType = method.getGenericReturnType();// 返回类型
            System.out.println("  " + returnType);
            if (returnType instanceof ParameterizedType)/* 如果是泛型类型 */{
                Type[] types = ((ParameterizedType) returnType)
                        .getActualTypeArguments();// 泛型类型列表
                System.out.println("  TypeArgument: ");
                for (Type type : types) {
                    System.out.println("   " + type);
                }
            }

        }
    }

    public static String method1(List list) {
        return null;
    }

    private static Map<String, Double> method2(Map<String, Object> map) {
        return null;
    }

}
回复 使用道具 举报
黑马-文鸿利 发表于 2013-8-20 10:47
package com.zhiming.tree;  
  
import java.lang.reflect.Method;  

class java.util.Vector
class java.util.Date
这是运行结果

回复 使用道具 举报
meng 发表于 2013-8-20 16:29
class java.util.Vector
class java.util.Date
这是运行结果

懂了,谢谢{:soso_e183:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马