我定义了一个放射的方法。
 public Object invokeMethod(Object owner, String methodName, Object[] args)
            throws Exception {
        Class ownerClass = owner.getClass();
        Class[] argsClass = new Class[args.length];
        for (int i = 0, j = args.length; i < j; i++) {
            argsClass[i] = args[i].getClass();
            System.out.println(argsClass[i]);
        } 
        System.out.println(methodName);
        Method method = null;
        try{
         method = ownerClass.getMethod(methodName, argsClass);
        }catch (Exception e){
         e.printStackTrace();
        }
       
        System.out.println(method.toString());
        return method.invoke(owner, args);
    }调用该反射方法
reflection.invokeMethod(methodWay, request.get(0), new Object[]{request, object, method, methodResum});下面是对应的方法public String methodCompareCommon(List<String> request, Object object, Object method, Object methodResum) { 
} List<String> request, Object object, Object method, Object methodResum 四个参数中,  Object object, Object method, 定义成Object类型是因为得到的对象参数类型不确定。下面是报错信息:
java.lang.NoSuchMethodException: com.hr2job.method.util.MethodWay.methodCompareCommon(java.util.ArrayList, com.hr2job.jobbase.domain.JobCompanyCondition_edu, com.hr2job.method.domain.Method_Education, com.hr2job.method.util.MethodResum) 后来想写成泛型的也不行。
public class MethodWay<T> {
public String methodCompareCommon(List<String> request, Class<T> object, Class<T> method, Class<T> methodResum) {高手们帮帮忙吧。。

解决方案 »

  1.   

    既然是取得方法名称,为什么要定义成object类型的呢?
    直接string就行啊 只要有该方法就会反射调用 
    NoSuchMethodException 明显是找不到该方法
    方法名称不正确或者是你那个参数类型的原因
      

  2.   

    前两个OBJCET 是POJO。具体是哪种类型是根据参数传进来不同而定义的。
     com.hr2job.jobbase.domain.JobCompanyCondition_edu, com.hr2job.method.domain.Method_Education,这两个参数是执行了
    reflection.invokeMethod(methodWay, request.get(0), new Object[]{request, object, method, methodResum});
    将类型取出来了。我是想有个通用方法,methodCompareCommon的参数,比如第二个参数,不管它接的是什么,只要是对象,反射以后就能找到这个方法。也就是说反射希望找到的是com.hr2job.method.util.MethodWay.methodCompareCommon(java.util.ArrayList, com.hr2job.jobbase.domain.JobCompanyCondition_edu, com.hr2job.method.domain.Method_Education, com.hr2job.method.util.MethodResum)
    这个方法我怎么定义methodCompareCommon的参数能让反射接受
      

  3.   

    反射类
    package com.hr2job.util;import java.lang.reflect.Array;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;public class Reflection {
    public Object invokeMethod(Object owner, String methodName, Object[] args)
                throws Exception {
            Class ownerClass = owner.getClass();
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
                System.out.println(argsClass[i]);
            } 
            
            Method method = null;
            try{
             method = ownerClass.getMethod(methodName, argsClass);
            }catch (Exception e){
             e.printStackTrace();
            }
           
            return method.invoke(owner, args);
        }
    }MethodWay:  我做了一些改动了,想用泛型,还是不成功
    package com.hr2job.method.util;import java.util.List;import com.hr2job.util.Reflection;public class MethodWay<T> {
    public String methodCompareCommon(List<String> request, Class<T> object, Class<T> method, Class<T> methodResum) {
    String getSek = "";
         String getReq = "";
         String returnValue = "";
         Reflection reflection = new Reflection();
         try{
         for (int i=1; i<=((request.size()-2) / 2); i=i+2){
         getReq = (String)reflection.invokeMethod(object, request.get(i+2).toString(), new Object[]{});
         getSek = (String)reflection.invokeMethod(method, request.get(i+1).toString(), new Object[]{});
         returnValue = reflection.invokeMethod(methodResum, request.get(1).toString(), new Object[]{getReq, getSek}).toString();
         if (Integer.parseInt(returnValue) == 1){
    break;
    }
         }
         }catch(Exception e){
         returnValue = null;
         }
    return returnValue;
    }
    }
      

  4.   

    实现类:
    import 。。public class MethodCompareImpl implements MethodCompare {
    public String compareCut(JobCompanySelection js, String cut, Object reflectionObject, Object object, Object method, Object methodResum) {
    List<String> request = new ArrayList<String>();
    Reflection reflection = new Reflection();
    MethodWay methodWay = new MethodWay();
    String returnValue = "";
    String[] requestCut = js.getJccut().split("-");
    try{
    for (int i=0; i<requestCut.length;i++){
    if (cut.indexOf(requestCut[i]) != -1){
    request = (List<String>)reflection.invokeMethod(reflectionObject, requestCut[i], new Object[]{});

    //returnValue = methodCompareCommon(request, object, method, methodResum);
    returnValue = reflection.invokeMethod(methodWay, request.get(0), new Object[]{request, object, method, methodResum}).toString();

    if (Integer.parseInt(returnValue) == 1){
    break;
    }
    }
    }
    }catch(Exception e){
         returnValue = null;
         }

    return returnValue;
    }
    }
      

  5.   

    楼主确定几点问题:
    1、方法名与类名是否正确。
    2、确定传入的对象不是java.lang.Object对象,而是实际的对象。
    3、查看指定的参数顺序是否正确。
    4、确定方法中是否有基础数据类型。其中第四点尤其重要,比如int类型对应的Class类对象与java.lang.Integer对应的Class类对象是不同的;
    int的Class对象描述符是I,但是Integer的Class对象描述符是Ljava/lang/Integer,根据jdk1.5的自动装箱拆箱机制,如果你有这样的代码Object[0]=1,那么实际操作会是Object[0]=new Integer(1);因此传入的不是int型而是Integer型。
    对此你要么修改方法参数,吧基础类型变成对象封装类型。要么修改你的反射方法,不要直接从Object引用中得到Class对象,而是从外部传入Class对象进行解析。
      

  6.   

    public class Test6 {
    public static void main(String[]args) throws Exception {
    List<String> list = new ArrayList<String>();
    list.add("yaowei");
    Object o = new InvokeMethod().invoke(list, "get", new Integer[]{0});
    System.out.println(o);
    }
    }
    class InvokeMethod {
    public Object invoke(Object o,String methodName,Object[]args) throws Exception {
    Class c = o.getClass();
    Class[] k = new Class[args.length];
    for(int i=0;i<args.length;i++) {
    k[i] = args[i].getClass();
    }
    Method m = c.getMethod(methodName, int.class);
    return m.invoke(o, args);
    }
    }
    对楼上的用程序补充下
    注意int.class
    换成k就不行了
      

  7.   


    问题出在第二点上,compareCut(js, MethodCondtion.info, mrinfo, (Object)jcskinfo, (Object)methodinfo, (Object)methodResum);
    我将传入的参数强转成Object的类型,JAVA还是会自动给我转回来。class java.util.ArrayList
    class com.hr2job.jobbase.domain.JobCompanyCondition_edu
    class com.hr2job.method.domain.Method_Education
    class com.hr2job.method.util.MethodResum这是打印的参数类型,所以依然报找不到方法的错误
      

  8.   

    对象是不会因为强转而改变的,改变的知识引用,对象本身并没有改变。
    因此你通过getClass方法,获得的是实际对象的类描述对象。
      

  9.   


    你将Class作为参数传入你的动态反射方法即可,方法的执行可以用多态性,但是方法的映射不支持这种多态性。
      

  10.   

    修改一下逻辑,
    这个可供参考
    public class Test6 {
    public static void main(String[]args) throws Exception {
    List<String> list = new ArrayList<String>();
    list.add("yaowei");
    Object o = new InvokeMethod().invoke(list, "get", new Class[]{int.class},new Object[]{0});
    System.out.println(o);
    }
    }
    class InvokeMethod {
    public Object invoke(Object o,String methodName,Class[]args,Object[]param) throws Exception {
    Class c = o.getClass();
    Class[] k = new Class[args.length];
    for(int i=0;i<args.length;i++) {
    k[i] = args[i].getClass();
    }
    Method [] m_array = c.getMethods();
    Method mm = null;
    //判断方法的名称是否符合,符合了判断方法的参数类型是否与传入的相同
    for(Method m:m_array) {
    if(m.getName().equals(methodName)) {
    Class[] g = m.getParameterTypes();
    if(g.length == args.length) {
    boolean result = true;
    for(int i=0;i<g.length;i++) {
    if(!g[i].equals(args[i])) {
    result = false;
    }
    }
    if(result) mm = m;
    }
    }
    }
    if(mm != null) {
    return mm.invoke(o, param);
    }
    return null;
    }
    }
      

  11.   


    方法执行这里就是多态会比较麻烦么。 比如 传进来的3个OBJECT 每个有三种类型。那就是3*3*3 27个组合 得写27个方法,要是更多那这个方法类得多大啊。
      

  12.   

    所以你要将Class作为参数传入反射反射方法中。
    因为方法对象的搜索需要准确。
      

  13.   


    如果new Class[]{Object.class}....  传入的参数继承于它,也不行吧。 
      

  14.   

    have a try
    getMethod -> getDeclaredMethod
    method.setAccessible(true);
      

  15.   


    把getMethod改成getDeclaredMethod试了吗?先试试看再说
      

  16.   

    重复提交了。。
    spiniper  yaoweijq两位大侠~~~  我判断了方法的参数的类型, 方法的对象搜索的时候,还是需要多态,比如实际对象还是对应不上OBJECT吧。
      

  17.   


    改了~~ 抛出的EXCEPTION跟执行中断的地方没变化
      

  18.   


    不知道怎么调用compareCut的,在你的代码的基础上帮你修改了下,你自己再试试看
    package com.hr2job.util;import java.lang.reflect.Array;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;public class Reflection {
        public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
            Class ownerClass = owner.getClass();
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
                System.out.println(argsClass[i]);
            }  
        
            Object rereult = null;
            try{
              Method method = ownerClass.getDeclaredMethod(methodName, argsClass);
              result = method.invoke(owner, args);
            } catch (Exception e){
                e.printStackTrace();
            }
        
            return result;
        }
    }
    package com.hr2job.method.util;import java.util.List;import com.hr2job.util.Reflection;public class MethodWay { 
        public String methodCompareCommon(List<String> request, Object objReq, Object objSek, Object objInvoke) {
            String getSek = "";
            String getReq = "";
            String returnValue = "";
            Reflection reflection = new Reflection();
            try {
                for (int i=1; i<=((request.size()-2) / 2); i=i+2){
                    getReq = (String)reflection.invokeMethod(objReq, request.get(i+2), new Object[]{});
                    getSek = (String)reflection.invokeMethod(objSek, request.get(i+1), new Object[]{});
                    returnValue = (String)reflection.invokeMethod(objInvoke, request.get(1), new Object[]{getReq, getSek});
                    if (returnValue != null && Integer.parseInt(returnValue) == 1){
                        break;
                    }
                }
            } catch(Exception e) {
                returnValue = null;
            }
            return returnValue;
        }
    }public class MethodCompareImpl implements MethodCompare {
        public String compareCut(JobCompanySelection js, String cut, Object reflectionObject, Object objReq, Object objSek, Object objInvoke) {
            List<String> request = new ArrayList<String>();
            Reflection reflection = new Reflection();
            MethodWay methodWay = new MethodWay();
            String returnValue = "";
            String[] requestCut = js.getJccut().split("-");
            try{
                for (int i=0; i<requestCut.length;i++){
                    if (cut.indexOf(requestCut[i]) != -1){
                        System.out.printf("%s,%s", reflectionObject.getClass(), requestCut[i]);
                        request = reflection.invokeMethod(reflectionObject, requestCut[i], new Object[]{});
                        if (request instanceof List) {
                            @SuppressWarnings("unchecked")
                            List<String> reqList = (List<String>)request;
                            if (reqList != null && reqList.size() > 0) {
                                returnValue = (String)reflection.invokeMethod(methodWay, reqList.get(0), new Object[]{reqList, objReq, objSek, objInvoke});
                            }
                        }
                        //returnValue = methodCompareCommon(request, object, method, methodResum);
                        
                        if (reruenValue != null && Integer.parseInt(returnValue) == 1){
                            break;
                        }
                    }
               }
            } catch(Exception e){
                returnValue = null;
            }        return returnValue;
        }
    }
      

  19.   


    public class MethodCompareImpl implements MethodCompare {
        public String compareCut(JobCompanySelection js, String cut, Object reflectionObject, Object objReq, Object objSek, Object objInvoke) {
            //List<String> request = new ArrayList<String>();
            Object request = null;
            Reflection reflection = new Reflection();
            MethodWay methodWay = new MethodWay();
            String returnValue = "";
            String[] requestCut = js.getJccut().split("-");
            try{
                for (int i=0; i<requestCut.length;i++){
                    if (cut.indexOf(requestCut[i]) != -1){
                        System.out.printf("%s,%s", reflectionObject.getClass(), requestCut[i]);
                        request = reflection.invokeMethod(reflectionObject, requestCut[i], new Object[]{});
                        if (request instanceof List) {
                            @SuppressWarnings("unchecked")
                            List<String> reqList = (List<String>)request;
                            if (reqList != null && reqList.size() > 0) {
                                returnValue = (String)reflection.invokeMethod(methodWay, reqList.get(0), new Object[]{reqList, objReq, objSek, objInvoke});
                            }
                        }
                        //returnValue = methodCompareCommon(request, object, method, methodResum);
                        
                        if (reruenValue != null && Integer.parseInt(returnValue) == 1){
                            break;
                        }
                    }
               }
            } catch(Exception e){
                returnValue = null;
            }        return returnValue;
        }
    }
      

  20.   


    非常之感谢指正我修正了代码中不规范的的地方,可还是没报错。找不到方法类。
    returnValue = (String)reflection.invokeMethod(methodWay, reqList.get(0), new Object[]{reqList, objReq, objSek, objInvoke});还是这个地方   这几个参数  到
     for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
                System.out.println(argsClass[i]);
            }  
    打印的结果
    java.util.ArrayList
    class com.hr2job.jobbase.domain.JobCompanyCondition_edu
    class com.hr2job.method.domain.Method_Education
    class com.hr2job.method.util.MethodResum然后报
    java.lang.NoSuchMethodException: com.hr2job.method.util.MethodWay.methodCompareCommon(java.util.ArrayList, com.hr2job.jobbase.domain.JobCompanyCondition_edu, com.hr2job.method.domain.Method_Education, com.hr2job.method.util.MethodResum)
      

  21.   

    以上代码作了整理,再看异常提示,可知以下地方有问题
    for (int i = 0, j = args.length; i < j; i++) {
        argsClass[i] = args[i].getClass(); //这里,会得到具体实例的类型,即ArrayList对象,而参数是List,所以方法就找不到了
        System.out.println(argsClass[i]);
    }  所以再作一个判断
    public class Reflection {
        public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
            Class ownerClass = owner.getClass();
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
                System.out.println(argsClass[i]);
            }  
        
            Object rereult = null;
            Method method = null;
            try{
                method = ownerClass.getDeclaredMethod(methodName, argsClass);
            } catch (Exception e){
                e.printStackTrace();
            }        try {
             if (method == null) {
                 for (Method m : ownerClass.getDeclaredMethods()) {
                     if (m.getName().equals(methodName)) {
                         boolean find = true;
                         Class[] paramType = m.getParameterTypes();
                         if (paramType.length != argsClass.length) {
                             continue;
                         }
                         for (int i=0; i<argsClass.length; i++) {
                             if (! paramType[i].isAssignableFrom(argsClass[i])) {
                                 find = false;
                                 break;
                             }
                         }
                         if (find) {
                             method = m;
                             break;
                         }
                     }
                 }
             }
                if (method != null) {
                 rereult = ownerClass.invoke(owner, args);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        
            return result;
        }
    }
      

  22.   

    就这个问题还墨迹了半天,你的方法是什么的访问权限的的啊
    method = ownerClass.getMethod(methodName, argsClass);
    只能获取到public 的要是获取private 的方法
    method = ownerClass.getDeclaredMethod((methodName, argsClass);
    要是你的方法是public ,看下你的方法名称
      

  23.   


    这个问题还真要墨迹一会,因为一个方法,参数可能是父类或接口,
    实际调用的时候有可能传的是子类实例,所以通过子类实例getClass拿到的类型不一致,就导致方法找不到
      

  24.   


    等了一天,终于有大拿说到点子上了。执行成功了。感动ING~
     try{
                method = ownerClass.getDeclaredMethod(methodName, argsClass);
            } catch (Exception e){
                e.printStackTrace();
            }
    会抛出异常~ 
    咋解决呢?????
      

  25.   

    找不到方法肯定会抛出异常的,你可以对异常忽视,即把e.printStackTrace();注释掉
      

  26.   

    借这个机会再问个问题~~   反射用多了会影响性能么???因为我的条件很多很多~如果把方法对象化,根据前台参数传的参数,调用****(Object objReq, Object objSek)里面的执行方法。 这样会写好多好多。。现在我是通过配置一个类,类里就是NEW一个LIST。通过前台给的值,去NEW这个LIST。等于写了一个配置文件。但这样我就在很多地方用了反射。效率会有影响么。还是就算麻烦点,上一种的设计会比较好。我只想到了这两种设计方法。
      

  27.   


    能定位到方法,就是那个叫REQUEST的LIST。这里面等于是配置文件。有执行的工具类的方法,执行规则的方法,执行对象的属性,等等。只不过得写硬编码。。打完收工,给分结贴。非常感谢qybao的大力支持。我算是外行,没有做技术的朋友。只能CSDN。以后大拿们多多支持。