]> git.basschouten.com Git - openhab-addons.git/blob
efc5929d9ceb84abf9e0ec6ade75c2f03dcf3f3b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.util;
14
15 import java.lang.reflect.Field;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * Utility class for reflection operations such as accessing private fields or methods.
24  *
25  * @author Björn Lange - Initial contribution
26  */
27 @NonNullByDefault
28 public final class ReflectionUtil {
29     private ReflectionUtil() {
30     }
31
32     /**
33      * Gets a private attribute.
34      *
35      * @param object The object to get the attribute from.
36      * @param fieldName The name of the field to get.
37      * @return The obtained value.
38      * @throws SecurityException if the operation is not allowed.
39      * @throws NoSuchFieldException if no field with the given name exists.
40      * @throws IllegalAccessException if the field is enforcing Java language access control and is inaccessible.
41      * @throws IllegalArgumentException if one of the passed parameters is invalid.
42      */
43     @SuppressWarnings("unchecked")
44     public static <T> T getPrivate(Object object, String fieldName)
45             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
46         Field field = getFieldFromClassHierarchy(object.getClass(), fieldName);
47         field.setAccessible(true);
48         return (T) field.get(object);
49     }
50
51     private static Field getFieldFromClassHierarchy(Class<?> clazz, String fieldName)
52             throws NoSuchFieldException, SecurityException {
53         Class<?> iteratedClass = clazz;
54         do {
55             try {
56                 return iteratedClass.getDeclaredField(fieldName);
57             } catch (NoSuchFieldException e) {
58             }
59             iteratedClass = iteratedClass.getSuperclass();
60         } while (iteratedClass != null);
61         throw new NoSuchFieldException();
62     }
63
64     /**
65      * Sets a private attribute.
66      *
67      * @param object The object to set the attribute on.
68      * @param fieldName The name of the field to set.
69      * @param value The value to set.
70      * @throws SecurityException if the operation is not allowed.
71      * @throws NoSuchFieldException if no field with the given name exists.
72      * @throws IllegalAccessException if the field is enforcing Java language access control and is inaccessible.
73      * @throws IllegalArgumentException if one of the passed parameters is invalid.
74      */
75     public static void setPrivate(Object object, String fieldName, @Nullable Object value)
76             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
77         Field field = object.getClass().getDeclaredField(fieldName);
78         field.setAccessible(true);
79         field.set(object, value);
80     }
81
82     /**
83      * Invokes a private method on an object.
84      *
85      * @param object The object to invoke the method on.
86      * @param methodName The name of the method to invoke.
87      * @param parameters The parameters of the method invocation.
88      * @return The method call's return value.
89      * @throws NoSuchMethodException if no method with the given parameters or name exists.
90      * @throws SecurityException if the operation is not allowed.
91      * @throws IllegalAccessException if the method is enforcing Java language access control and is inaccessible.
92      * @throws IllegalArgumentException if one of the passed parameters is invalid.
93      * @throws InvocationTargetException if the invoked method throws an exception.
94      */
95     public static <T> T invokePrivate(Object object, String methodName, Object... parameters)
96             throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
97         Class<?>[] parameterTypes = new Class[parameters.length];
98         for (int i = 0; i < parameters.length; i++) {
99             parameterTypes[i] = parameters[i].getClass();
100         }
101
102         return invokePrivate(object, methodName, parameterTypes, parameters);
103     }
104
105     /**
106      * Invokes a private method on an object.
107      *
108      * @param object The object to invoke the method on.
109      * @param methodName The name of the method to invoke.
110      * @param parameterTypes The types of the parameters.
111      * @param parameters The parameters of the method invocation.
112      * @return The method call's return value.
113      * @throws NoSuchMethodException if no method with the given parameters or name exists.
114      * @throws SecurityException if the operation is not allowed.
115      * @throws IllegalAccessException if the method is enforcing Java language access control and is inaccessible.
116      * @throws IllegalArgumentException if one of the passed parameters is invalid.
117      * @throws InvocationTargetException if the invoked method throws an exception.
118      */
119     @SuppressWarnings("unchecked")
120     public static <T> T invokePrivate(Object object, String methodName, Class<?>[] parameterTypes, Object... parameters)
121             throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
122         Method method = getMethodFromClassHierarchy(object.getClass(), methodName, parameterTypes);
123         method.setAccessible(true);
124         try {
125             return (T) method.invoke(object, parameters);
126         } catch (InvocationTargetException e) {
127             throw new IllegalStateException(e.getCause());
128         }
129     }
130
131     private static Method getMethodFromClassHierarchy(Class<?> clazz, String methodName, Class<?>[] parameterTypes)
132             throws NoSuchMethodException {
133         Class<?> iteratedClass = clazz;
134         do {
135             try {
136                 return iteratedClass.getDeclaredMethod(methodName, parameterTypes);
137             } catch (NoSuchMethodException e) {
138             }
139             iteratedClass = iteratedClass.getSuperclass();
140         } while (iteratedClass != null);
141         throw new NoSuchMethodException();
142     }
143 }