代码需要在1.4上编译,但是其中一处使用的类是已经用1.5编译好的,无法编译。这部分类是由其他模块提供的,只能在JDK1.5下运行,并且我们的代码必须兼容JRE1.4和1.5(回有配置使其在1.4的JRE上不会调用用1.5编译的类)。所以无法通过我们代码升级到1.5或者其他模块提供1.4编译的包来解决。
目前考虑使用java反射机制解决。将调用外模块那一部分代码修改为运行时决定变量类型和调用方法。现在由两个问题急需解决。
如何定义运行时类型变量以及如何调用运行时方法比如:下面的代码
com.my.QueryValue = myQueryValue;
com.my.ResultValue = myResultValue;//本模块的对象,表示本模块使用的查询条件和查询结果com.other.TypeA otherQueryValue = new com.other.TypeA();
com.other.TypeB otherResultValue = new com.other.TypeB();//外模块对象,表示调用外模块接口时的查询条件和查询结果//封装外模块查询条件
otherQueryValue.setA (myQueryValue.getA());
//调用接口
com.other.OtherInterface example= com.abc.otherFactory.getExample();
otherResultValue = exampleInterface.test(otherRequestValue);
//根据返回结果封装本模块查询结果
myResultValue.setA(otherResultValue.getA());因为com.other包是外模块用JDK1.5编译的,我在用JDK1.4编译的时候编译到com.other.TypeA otherQueryValue = new com.other.TypeA();这一句就会报错:
[javac]cannot access com.other.TypeA
[javac]bad class file: ..\lib\exintf-int.jar(com.other.TypeA.class)
[javac] class file has wrong version 49.0, should be 48.0
[javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
[javac] com.other.TypeA =
[javac] ^
[javac] 1 error
目前考虑使用java反射机制解决。将调用外模块那一部分代码修改为运行时决定变量类型和调用方法。现在由两个问题急需解决。
如何定义运行时类型变量以及如何调用运行时方法比如:下面的代码
com.my.QueryValue = myQueryValue;
com.my.ResultValue = myResultValue;//本模块的对象,表示本模块使用的查询条件和查询结果com.other.TypeA otherQueryValue = new com.other.TypeA();
com.other.TypeB otherResultValue = new com.other.TypeB();//外模块对象,表示调用外模块接口时的查询条件和查询结果//封装外模块查询条件
otherQueryValue.setA (myQueryValue.getA());
//调用接口
com.other.OtherInterface example= com.abc.otherFactory.getExample();
otherResultValue = exampleInterface.test(otherRequestValue);
//根据返回结果封装本模块查询结果
myResultValue.setA(otherResultValue.getA());因为com.other包是外模块用JDK1.5编译的,我在用JDK1.4编译的时候编译到com.other.TypeA otherQueryValue = new com.other.TypeA();这一句就会报错:
[javac]cannot access com.other.TypeA
[javac]bad class file: ..\lib\exintf-int.jar(com.other.TypeA.class)
[javac] class file has wrong version 49.0, should be 48.0
[javac] Please remove or make sure it appears in the correct subdirectory of the classpath.
[javac] com.other.TypeA =
[javac] ^
[javac] 1 error
try {
Object object = Class.forName("ClassName").getInterfaces();
Method[] ms = object.getClass().getMethods();
for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().equals("MethodName")) {
try {
ms[i].invoke(object, null);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}