File: Reflector.java

package info (click to toggle)
jruby 9.3.9.0%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,856 kB
  • sloc: ruby: 517,823; java: 260,094; xml: 31,930; ansic: 5,777; yacc: 4,973; sh: 1,163; makefile: 105; jsp: 48; tcl: 40; exp: 11
file content (102 lines) | stat: -rw-r--r-- 4,145 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package java_integration.fixtures;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;

public abstract class Reflector {

    public static Object invoke(final Object obj, final Method method) throws Exception {
        if ( method.isVarArgs() ) {
            final Class type = method.getParameterTypes()[0].getComponentType();
            Object array = Array.newInstance(type, 0);
            return method.invoke(obj, array);
        }
        return method.invoke(obj);
    }

    public static Object invoke(final Object obj, final Method method, Object arg) throws Exception {
        if ( method.isVarArgs() && ! arg.getClass().isArray() ) {
            final Class type = method.getParameterTypes()[0].getComponentType();
            Object[] array = (Object[]) Array.newInstance(type, 1);
            array[0] = arg;
            return method.invoke(obj, (Object) array);
        }
        return method.invoke(obj, arg);
    }

    public static Object invoke(final Object obj, final Class<?> klass, final String method) throws Exception {
        Method instanceMethod = klass.getMethod(method, (Class[]) null);
        return instanceMethod.invoke(obj, (Object[]) null);
    }

    public static Object invoke(final Object obj, final String method) throws Exception {
        return invoke(obj, obj.getClass(), method);
    }

    public static Object invokeMatch(final Object obj, final String method, final Object... args) throws Exception {
        final Method[] methods = obj.getClass().getMethods();
        ArrayList<Method> matchedMethods = new ArrayList<Method>();
        for ( Method m : methods ) {
            if ( method.equals(m.getName()) ) {
                if ( m.isVarArgs() ) matchedMethods.add(m);
                else {
                    if (m.getParameterTypes().length == args.length) {
                        matchedMethods.add(m);
                    }
                }
            }
        }
        if ( matchedMethods.isEmpty() ) {
            throw new IllegalArgumentException("no methods of name " + method + " matched in " + obj.getClass());
        }
        if ( matchedMethods.size() > 1 ) {
            throw new IllegalArgumentException("multiple methods matched for name " + method + " and arguments " + Arrays.toString(args));
        }
        return matchedMethods.get(0).invoke(obj, args);
    }

    public static Method resolveMethod(final Object obj, final String method, final Class... types) throws Exception {
        final Method[] methods = obj.getClass().getMethods();
        ArrayList<Method> matchedMethods = new ArrayList<Method>();
        for ( Method m : methods ) {
            if ( method.equals(m.getName()) ) {
                if ( Arrays.equals(m.getParameterTypes(), types) ) {
                    matchedMethods.add(m);
                }
            }
        }
        if ( matchedMethods.isEmpty() ) {
            throw new IllegalArgumentException("no methods of name " + method + " matched in " + obj.getClass());
        }
        if ( matchedMethods.size() > 1 ) {
            throw new IllegalArgumentException("multiple methods matched for name " + method + " and param types " + Arrays.toString(types));
        }
        return matchedMethods.get(0);
    }

    public static Collection<Method> findMethods(final Object obj, final String method) throws Exception {
        final Method[] methods = obj.getClass().getMethods();
        ArrayList<Method> matchedMethods = new ArrayList<Method>();
        for ( Method m : methods ) {
            if ( method.equals(m.getName()) ) {
                matchedMethods.add(m);
            }
        }
        return matchedMethods;
    }

    public static <A extends Annotation> A getDeclaredAnnotation(Class<?> clazz,
        Class<A> annotationType) {
        for (Annotation a : clazz.getDeclaredAnnotations()) {
            if (annotationType.isAssignableFrom(a.annotationType())) {
                return (A) a;
            }
        }
        return null;
    }
}