如何检查java类中是否有特定的方法? [英] How to check if a java class has a particular method in it?

查看:173
本文介绍了如何检查java类中是否有特定的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml架构(使用trang自动生成),它不断变化。这些变化不是很精细。仅从此架构中添加或删除一些元素。从这个模式,我生成java类(使用cxf),我将通过它解组xml文档。

I have an xml schema (generated automatically using trang) which keeps changing. These changes are not very elaborate. Only some elements are added or deleted from this schema. From this schema, I am generating java classes (using cxf) by which I will unmarshall the xml document.

随着架构的变化,我自动生成的java类也会发生变化。同样,与模式一样,java类中的更改也不是很大。例如,如果一个元素说 elemA 被添加到模式中;一些相关的函数说 getElemA() setElemA()被添加到自动生成的java类中。

As schema changes, my auto-generated java classes also change. Again, as with schema, changes in java classes are not very big. For instance, if an element say elemA is added to schema; some related functions say getElemA() and setElemA() are added to auto-generated java class.

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

Now how would I make sure that a particular function exists in these auto-generated classes? One solution is to hand-write the schema such that all possible elements of xml are covered. This is what I'll ultimately do. But for now, I have not fixed the format of xml file.

更新:

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

There is a possibility that a method getElemA() may be defined in auto-generated classes. I do not have control over the auto-generation of these classes. But in my main class, if have following code,

If method getElemA exists then 
     ElemA elemA = getElemA()

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

This code will always be there in my main class. If method getElemA() is generated in one of the auto-generated class then there is no problem. But if this method is not generated then compilers complain that this method does not exists in any of the class.

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

Is there any way that I can make compiler not to complain about this function at compile time?

推荐答案

@missingfaktor提到了一种方法,下面是另一种方法(如果知道名称和参数) api)。

One method is mentioned by @missingfaktor and another is below (if you know the name and parameters of the api).

假设您有一种不带args的方法:

Say you have one method which takes no args:

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:

Say you have one method which takes native 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)));
}

假设您有一个盒装方法整数 args:

Say you have one method which takes boxed 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更新

Using the above soln to invoke method won't give you compilation errors. Updated as per @Foumpie

这篇关于如何检查java类中是否有特定的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆