2012-06-04 85 views
16

我有一个XML模式(使用trang自动生成),它不断变化。这些变化不是很详细。只有一些元素被添加或从这个模式中删除。从这个模式中,我正在生成Java类(使用cxf),通过它我将解组xml文档。如何检查一个java类是否有特定的方法?

随着模式的改变,我的自动生成的java类也随之改变。同样,与模式一样,Java类的变化也不是很大。例如,如果一个元素表示elemA被添加到模式;一些相关的功能说getElemA()setElemA()被添加到自动生成的java类。

现在我该如何确保这些自动生成的类中存在特定的函数?一种解决方案是手写模式,以覆盖所有可能的xml元素。这是我最终会做的。但是现在,我还没有修复xml文件的格式。

UPDATE:

有一个方法getElemA()可以在自动生成的类中定义的可能性。我无法控制这些类的自动生成。但在我的主类中,如果有以下代码,

If method getElemA exists then 
    ElemA elemA = getElemA() 

此代码将始终存在于我的主类中。如果方法getElemA()在其中一个自动生成的类中生成,则不存在任何问题。但是如果没有生成这个方法,编译器会抱怨这个方法在任何类中都不存在。

有没有什么办法可以让编译器在编译时不抱怨这个函数?

回答

32

一种方法是通过@missingfaktor提到的,另一种是低于(如果您知道名字和API的参数)。

假设你有一个方法,这需要无参数:

Method methodToFind = null; 
try { 
    methodToFind = YouClassName.class.getMethod("myMethodToFind", (Class<?>[]) null); 
} catch (NoSuchMethodException | SecurityException e) { 
    // Your exception handling goes here 
} 

调用它,如果存在:

if(methodToFind == null) { 
    // Method not found. 
} else { 
    // Method found. You can invoke the method like 
    methodToFind.invoke(<object_on_which_to_call_the_method>, (Object[]) null); 
} 

假设你有一个方法,这需要本土int ARGS:

Method methodToFind = null; 
methodToFind = YouClassName.class.getMethod("myMethodToFind", new Class[] { int.class }); 

如果存在,调用它:

if(methodToFind == null) { 
    // Method not found. 
} else { 
    // Method found. You can invoke the method like 
    methodToFind.invoke(<object_on_which_to_call_the_method>, invoke(this, 
     Integer.valueOf(10))); 
} 

假设你有一个方法,它采用盒装Integer ARGS:

Method methodToFind = null; 
methodToFind = YouClassName.class.getMethod("myMethodToFind", new Class[] { Integer.class }); 

调用它,如果存在:

if(methodToFind == null) { 
    // Method not found. 
} else { 
    // Method found. You can invoke the method like 
    methodToFind.invoke(<object_on_which_to_call_the_method>, invoke(this, 
     Integer.valueOf(10))); 
} 

使用上述SOLN调用方法不会给你编译错误。 根据@Foumpie更新

+0

谢谢。我想使用该方法,并且必须在该类中编写所有可能的方法,但自动生成的类可能没有该方法。如何让编译器在编译时忽略它? – Dilawar

+0

使用调用不会给你编译错误...检查更新的答案。 – havexz

+0

非常感谢。将它封入'try-catch'后,它就像一个魅力。 – Dilawar

17

使用reflection使用反射。

import java.lang.reflect.Method; 

boolean hasMethod = false; 
Method[] methods = foo.getClass().getMethods(); 
for (Method m : methods) { 
    if (m.getName().equals(someString)) { 
    hasMethod = true; 
    break; 
    } 
} 

编辑:

所以,你想如果存在要调用的方法。这是你如何做到这一点:

if (m.getName().equals(someString)) { 
    try { 
    Object result = m.invoke(instance, argumentsArray); 
    // Do whatever you want with the result. 
    } catch (Exception ex) { // For simplicity's sake, I am using Exception. 
          // You should be handling all the possible exceptions 
          // separately. 
    // Handle exception. 
    } 
} 
+0

感谢您的代码。主要的问题在于,即使我已经确定getA方法存在,我需要编写该函数。例如'如果methodA存在,那么methodA()'。由于此方法不存在,所以它给出了编译时错误。我如何让编译继续进行并将其作为运行时检查? – Dilawar

+0

@Dilawar,你的问题不清楚。请重新说明。 – missingfaktor

+1

@Dilawar,你应该仔细阅读一些反思教程,因为看起来你会需要很多。 – missingfaktor

4

如果您使用Spring Framework,最简单的方法是使用ReflectionUtils.findMethod()实用程序。

8

使用Spring:

Method method = ReflectionUtils.findMethod(TheClass, "methodName"); 
if (method != null) { 
    //do what you want 
} 
相关问题