我定义了一个放射的方法。
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) {高手们帮帮忙吧。。
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) {高手们帮帮忙吧。。
解决方案 »
- 问一个线程中断的问题,结束阻塞后,线程为什么不接着执行呢
- 关于timezone的问题
- java swing 复选框
- (char)System.in.read()对于输入的汉字,为何显示为‘?’
- 能否用JAVA做一个IE内核浏览器?
- 帮忙改个错
- initialization error: com.borland.compiler.symtab.LoadError: java.io.IOException: class not found: class java.lang.Object
- 请问??Date日期类中的方法怎么用???
- 数字签名请教!!分数不够可以在加!!
- JAVA SE
- hashmap
- java 搜索
直接string就行啊 只要有该方法就会反射调用
NoSuchMethodException 明显是找不到该方法
方法名称不正确或者是你那个参数类型的原因
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的参数能让反射接受
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;
}
}
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;
}
}
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对象进行解析。
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就不行了
问题出在第二点上,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这是打印的参数类型,所以依然报找不到方法的错误
因此你通过getClass方法,获得的是实际对象的类描述对象。
你将Class作为参数传入你的动态反射方法即可,方法的执行可以用多态性,但是方法的映射不支持这种多态性。
这个可供参考
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;
}
}
方法执行这里就是多态会比较麻烦么。 比如 传进来的3个OBJECT 每个有三种类型。那就是3*3*3 27个组合 得写27个方法,要是更多那这个方法类得多大啊。
因为方法对象的搜索需要准确。
如果new Class[]{Object.class}.... 传入的参数继承于它,也不行吧。
getMethod -> getDeclaredMethod
method.setAccessible(true);
把getMethod改成getDeclaredMethod试了吗?先试试看再说
spiniper yaoweijq两位大侠~~~ 我判断了方法的参数的类型, 方法的对象搜索的时候,还是需要多态,比如实际对象还是对应不上OBJECT吧。
改了~~ 抛出的EXCEPTION跟执行中断的地方没变化
不知道怎么调用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;
}
}
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;
}
}
非常之感谢指正我修正了代码中不规范的的地方,可还是没报错。找不到方法类。
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)
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;
}
}
method = ownerClass.getMethod(methodName, argsClass);
只能获取到public 的要是获取private 的方法
method = ownerClass.getDeclaredMethod((methodName, argsClass);
要是你的方法是public ,看下你的方法名称
这个问题还真要墨迹一会,因为一个方法,参数可能是父类或接口,
实际调用的时候有可能传的是子类实例,所以通过子类实例getClass拿到的类型不一致,就导致方法找不到
等了一天,终于有大拿说到点子上了。执行成功了。感动ING~
try{
method = ownerClass.getDeclaredMethod(methodName, argsClass);
} catch (Exception e){
e.printStackTrace();
}
会抛出异常~
咋解决呢?????
能定位到方法,就是那个叫REQUEST的LIST。这里面等于是配置文件。有执行的工具类的方法,执行规则的方法,执行对象的属性,等等。只不过得写硬编码。。打完收工,给分结贴。非常感谢qybao的大力支持。我算是外行,没有做技术的朋友。只能CSDN。以后大拿们多多支持。