File: ReflectionUtils.java

package info (click to toggle)
libjide-oss-java 3.7.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,444 kB
  • sloc: java: 91,177; xml: 661; makefile: 35
file content (265 lines) | stat: -rw-r--r-- 9,674 bytes parent folder | download | duplicates (8)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * @(#)ReflectionUtils.java 3/23/2011
 *
 * Copyright 2002 - 2011 JIDE Software Inc. All rights reserved.
 */

package com.jidesoft.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Utility class to use reflection to call methods.
 */
public class ReflectionUtils {
    /**
     * Helper method to call a set boolean method.
     *
     * @param thisObject the instance
     * @param methodName the method name
     * @param value      true or false
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static void callSetBoolean(Object thisObject, String methodName, boolean value) throws Exception {
        Class<?>[] argTypes = new Class<?>[]{boolean.class};
        Object[] args = new Object[]{value ? Boolean.TRUE : Boolean.FALSE};
        Method method = thisObject.getClass().getMethod(methodName, argTypes);
        method.invoke(thisObject, args);
    }

    /**
     * Helper method to call a set boolean method.
     *
     * @param thisObject the instance
     * @param methodName the method name
     * @param value      the value
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static void callSetInt(Object thisObject, String methodName, int value) throws Exception {
        Class<?>[] argTypes = new Class<?>[]{int.class};
        Object[] args = new Object[]{value};
        Method method = thisObject.getClass().getMethod(methodName, argTypes);
        method.invoke(thisObject, args);
    }

    /**
     * Helper method to call a set boolean method.
     *
     * @param thisObject the instance
     * @param methodName the method name
     * @param value      the value
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static void callSet(Object thisObject, String methodName, Object value)
            throws Exception {
        Class<?>[] argTypes = new Class<?>[]{value.getClass()};
        Object[] args = new Object[]{value};
        Method method = thisObject.getClass().getMethod(methodName, argTypes);
        method.invoke(thisObject, args);
    }

    /**
     * Helper method to call a get method with no argument.
     *
     * @param thisObject the instance
     * @param methodName the method name
     *
     * @return the value the method returns.
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static Object callGet(Object thisObject, String methodName) throws Exception {
        Method method = thisObject.getClass().getMethod(methodName, (Class[]) null);
        return method.invoke(thisObject, (Object[]) null);
    }

    /**
     * Helper method to call a no argument, no return method.
     *
     * @param thisObject the instance
     * @param methodName the method name
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static void call(Object thisObject, String methodName) throws Exception {
        Method method = thisObject.getClass().getMethod(methodName, (Class[]) null);
        method.invoke(thisObject, (Object[]) null);
    }

    /**
     * Helper method to call a constructor.
     *
     * @param clazz    the class
     * @param argTypes argument Classes for constructor lookup. Must not be null.
     * @param args     the argument array
     *
     * @return the value the method returns.
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static Object callConstructor(Class<?> clazz, Class<?>[] argTypes, Object[] args) throws Exception {
        Constructor constructor = clazz.getConstructor(argTypes);
        return constructor.newInstance(args);
    }

    /**
     * Helper method to call a multi-argument method having a return. The class types will be derived from the input
     * values. This call is usually successful with primitive types or Strings as arguments, but care should be used
     * with other kinds of values. The constructor lookup is not polymorphic.
     * <p/>
     * Calls <code>callAny(Object, methodName, argTypes, args)</code>.
     *
     * @param thisObject the instance
     * @param methodName the method name
     * @param args       the argument array, must not contain null.
     *
     * @return the value the method returns.
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static Object callAny(Object thisObject, String methodName, Object[] args) throws Exception {
        Class<?>[] argTypes = null;
        if (args != null) {
            argTypes = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                argTypes[i] = args[i].getClass();
            }
        }
        return callAny(thisObject, methodName, argTypes, args);
    }

    /**
     * Helper method to call a multi-argument method having a return.
     *
     * @param thisObject the instance
     * @param methodName the method name
     * @param argTypes   argument Classes for constructor lookup. Must not be null.
     * @param args       the argument array
     *
     * @return the value the method returns.
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static Object callAny(Object thisObject, String methodName, Class<?>[] argTypes, Object[] args) throws Exception {
        Method method = thisObject.getClass().getMethod(methodName, argTypes);
        return method.invoke(thisObject, args);
    }

    /**
     * Helper method to call a multi-argument method having a return without throwing an exception.
     *
     * @param thisObject  the instance
     * @param objectClass the class which could find the method name
     * @param methodName  the method name
     * @param argTypes    argument Classes for constructor lookup. Must not be null.
     * @param args        the argument array
     *
     * @return the value  the method returns.
     */
    public static Object callAnyWithoutException(Object thisObject, Class<?> objectClass, String methodName, Class<?>[] argTypes, Object[] args) {
        Method m;
        try {
            m = objectClass.getDeclaredMethod(methodName, argTypes);
        }
        catch (Exception e) {
            m = null;
        }
        if (m != null) {
            try {
                m.setAccessible(true);
                return m.invoke(thisObject, args);
            }
            catch (Exception ignored) {
                return null;
            }
            finally {
                m.setAccessible(false);
            }
        }
        return null;
    }

    /**
     * Helper method to call a multi-argument static class method having a return.
     *
     * @param thisClass  the class
     * @param methodName the method name
     * @param argTypes   argument Classes for constructor lookup. Must not be null.
     * @param args       the argument array
     *
     * @return the value the method returns.
     *
     * @throws Exception if the method is not found or invocation to the method fails.
     */
    public static Object callStatic(Class<?> thisClass, String methodName, Class<?>[] argTypes, Object[] args) throws Exception {
        Method method = thisClass.getMethod(methodName, argTypes);
        Object result = null;
        if (method != null) {
            try {
                method.setAccessible(true);
                result = method.invoke(null, args);
            }
            finally {
                method.setAccessible(false);
            }
        }
        return result;
    }

    /**
     * Instantiate an object based on the class name.
     *
     * @param className the class name
     * @param types     the class types for the constructor
     * @param args      the constructor values
     *
     * @return the object instance. null if any exception occurs.
     */
    public static Object createInstance(String className, Class<?>[] types, Object[] args) {
        Object instantiation = null;
        // try default class
        try {
            Class<?> cls = Class.forName(className);
            if (types != null && types.length != 0) {
                Constructor<?> constructor = cls.getConstructor(types);
                instantiation = constructor.newInstance(args);
            }
            else {
                instantiation = cls.newInstance();
            }
        }
        catch (Exception e) {
            // null
        }

        return instantiation;
    }

    /**
     * Checks if the instance o is an instance of a subclass of the designated class name.
     *
     * @param o         the instance to check
     * @param className the class name to check
     *
     * @return true if the instance o is an instance of a subclass of the class name. Otherwise false.
     *
     * @since 3.4.9
     */
    public static boolean isSubClassOf(Object o, String className) {
        if (o == null) {
            return false;
        }
        Class<?> aClass = o.getClass();
        while (aClass != null) {
            if (aClass.getName().contains(className)) {
                return true;
            }
            aClass = aClass.getSuperclass();
        }
        return false;
    }
}