简体   繁体   English

Java中的getDeclaredMethod失败

[英]getDeclaredMethod in Java Fails

I have a batch app which uses a file input with the method names and then the names are used to execute a method with the same name by design. 我有一个批处理应用程序,该应用程序使用带有方法名称的文件输入,然后通过设计使用名称来执行具有相同名称的方法。

eg File has getTemperature and then getTemperature is executed in the class. 例如,文件具有getTemperature,然后在该类中执行getTemperature。

I am using the following Java: 我正在使用以下Java:

java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1~0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

The code is: 代码是:

 if (args.length < 2) {
        System.out.println("SF   <retailername> retailername <manName> man");
        System.exit(1);
    }
    GetSoogrData wa = new GetSoogrData();
    try {
        BufferedReader bf = new BufferedReader(new FileReader(args[0]));

        String line = null;
        while ((line = bf.readLine()) != null) {
            Class c  = wa.getClass();


            c.getDeclaredMethod(line, null);
        }

The compiler gives warnings about a Raw Type (Class is a Raw Type) and also warnings on the getDeclaredMethod saying about Type. 编译器给出有关原始类型的警告(类是原始类型),并在getDeclaredMethod上给出有关类型的警告。 The class is just declared: 该类刚刚声明:

public class GetSoogrData {

    private HashMap<String, Integer> wordDictionary =   new HashMap<String, Integer>();
    private File inputFile;

    private File outputFile;
    private String retailerName;
    private String man;
    private String[] word;

    public void getTemperature() {
        System.out.println("device temp");
    }

I am testing with getTemperature. 我正在用getTemperature测试。

I have tried a few versions of the code and there is no error but no result either. 我尝试了几个版本的代码,没有错误,但也没有结果。 The code just seems to ignore the method completely. 该代码似乎完全忽略了该方法。 I tested without the getDeclaredMethod and it worked OK, so the method can be reached and the class instantiated. 我在没有getDeclaredMethod的情况下进行了测试,它工作正常,因此可以访问该方法并实例化该类。 Hence the error lies with the actual getDeclaredMethod. 因此,错误在于实际的getDeclaredMethod。 I saw the Java version can affect this. 我看到Java版本会影响这一点。

Has anyone any solution? 有没有人有解决方案?

You should be doing something like: 您应该执行以下操作:

Method method = c.getDeclaredMethod(line);
method.invoke(wa);//this will call method whatever is there in your line method without any parameters

First, The line: 一,行:

c.getDeclaredMethod(line, null);

Should be: 应该:

c.getDeclaredMethod(line);

getDeclaredMethod receives a varargs array of Class objects, so your non-parameterized method should receive an empty array, not null . getDeclaredMethod接收一个Class对象的varargs数组,因此您的非参数化方法应该接收一个空数组,而不是null

Second, you should invoke the method object after getting it, using Method.invoke(Object) . 其次,您应该在获取方法对象之后使用Method.invoke(Object) invoke该方法对象。

Anyway, debugging would probably be solving your problem. 无论如何,调试可能会解决您的问题。 You could just run line by line to see if the code line is "ignored", then you could probably figure out what's wrong with your code... 您可以逐行运行以查看代码行是否被“忽略”,然后您可能会发现代码出了什么问题...

我刚刚发现对我在getDeclaredMethod上的第二个查询的答案不起作用,NoSuchMethodException-解决方案包括添加String.class作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM