Java reflection:如何只获取没有参数的方法

Java reflection: How to get methods with no parameters only

我正在做一个关于Java反射的学校作业。详情如下:

Write a console program that asks the user for a class name, loads
that class and creates an instance of it. We assume that the class has
a constructor without any parameters. Then, the program prints out the
names and values of the public variables of the created object, and
also a list of the public methods that do not specify a parameter. The
program should let the user choose a method and execute that method on
the created object. Afterwards, the program should again show the
public variables with their values and allow the user to choose a
method, and so on. Use the following class to test your
implementation:

1
2
3
4
5
6
public class Counter {
    public int c;
    public void increment() { c++; }
    public void decrement() { c--; }
    public void reset() { c = 0; }
}

我要解决的问题是:"没有指定参数的公共方法列表"。有没有办法只列出没有参数的方法?我已经使用了getmethods,但是我最终得到了很多来自对象和带有参数的类超类的方法。

例如,我编写的以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.lang.reflect.*;
import java.io.*;

public class Q1 {
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("What class would you like to run?");
            String className = reader.readLine();

            Class c = Class.forName(className);
            Object o = c.newInstance();

            for (Field f : c.getFields())
                System.out.println(f);
            for (Method m : c.getMethods())
                System.out.println(m);

        } catch(IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

输出以下内容:

What class would you like to run? Counter
public int Counter.c
public void Counter.reset()
public void Counter.increment()
public void Counter.decrement()
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

有没有办法只打印那些没有参数的?我对作业细节的解释是否正确?或者,"不指定参数的公共方法"这个短语可能意味着其他的东西,而我的想法完全是错误的?


你看过方法类的API了吗?有一个名为getParameterTypes()的方法可以回答您要查找的内容,并且API明确说明如果没有参数,将返回什么。只需在返回的方法的for循环中调用它,您就应该像Flint一样。


只需使用方法类的getParameterTypes函数。如果返回值为0,则该函数没有参数。JAVA DOC的关键部分:

getParameterTypes

public Class[] getParameterTypes()

1
Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method represented by

this Method object. Returns an array of length 0 if the underlying
method takes no parameters.

1
2
Returns:
    the parameter types for the method this object represents