challenge メソッド名一覧の表示

リフレクション系のお題の続編です。

「ある与えられたオブジェクトtargetのメソッドのうち、 "test_"で始まるものをすべて呼びだす」というコードを書いてください。 引数に関しては都合のいいように仮定して構いません(全部0個、など)。

メソッドという概念がない言語の場合は、 「複数の関数への参照を持っているようなオブジェクト(たとえばパッケージとかモジュールとか)から"test_"で始まる関数をすべて呼び出す」と読み替えても構いません。

Posted feedbacks - Java

 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
30
31
32
33
34
35
36
37
import java.lang.reflect.Method;

public class TestMethods {
    
    public static void test(Object target) {
        Class targetClass = target.getClass();
        Method[] methods = targetClass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("test_")) {
                try {
                    method.invoke(target);
                }
                catch (Exception e) {}
            }
        }
    }
    
    public static void main(String[] args) {
        test(new Test());
    }
    
}

class Test {
    public void test_public() {
        System.out.println("public method");
    }
    protected void test_protected() {
        System.out.println("protected method");
    }
    void test_package_private() {
        System.out.println("package private method");
    }
    private void test_private() {
        System.out.println("private method");
    }
}

引数つきのメソッドでも呼び出せるようにしてみました。プリミティブ型なら 0(または false)、参照型なら null を渡すようにしています。
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.lang.reflect.Method;

public class Sample {
    public static void callTest(Object target) throws Exception {
        Class c = target.getClass();
        Method[] ms = c.getMethods();
        for (int i = 0; i < ms.length; i++) {
            if (ms[i].getName().startsWith("test_")) {
                Class[] argTypes = ms[i].getParameterTypes();
                Object[] args = new Object[argTypes.length];
                for (int j = 0; j < argTypes.length; j++) {
                    if (argTypes[j] == Character.TYPE) {
                        args[j] = Character.valueOf((char) 0);
                    } else if (argTypes[j] == Boolean.TYPE) {
                        args[j] = Boolean.valueOf(false);
                    } else if (argTypes[j].isPrimitive()) {
                        args[j] = Byte.valueOf((byte)0);
                    } else {
                        args[j] = null;
                    }
                }
                ms[i].invoke(target, args);
            }
        }
    }

    public void test_1(int i) {
        System.out.println("test_1 called");
    }

    private int test_2() {
        System.out.println("test_2 called");
        return 0;
    }

    public static void test_3(char c) {
        System.out.println("test_3 called");
    }

    public void test_4(byte by, boolean b, short s, String str, double d) {
        System.out.println("test_4 called");
    }

    public static void main(String[] args) throws Exception {
        callTest(new Sample());
    }
}

Index

Feed

Other

Link

Pathtraq

loading...