]> git.basschouten.com Git - openhab-addons.git/blob
ffc6dba2d13549fb17b76a98e636da2ad8eb9e75
[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 import java.lang.reflect.Modifier;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * Utility class for reflection operations such as accessing private fields or methods.
25  *
26  * @author Björn Lange - Initial contribution
27  */
28 @NonNullByDefault
29 public final class ReflectionUtil {
30     private ReflectionUtil() {
31     }
32
33     /**
34      * Gets a private attribute.
35      *
36      * @param object The object to get the attribute from.
37      * @param fieldName The name of the field to get.
38      * @return The obtained value.
39      * @throws SecurityException if the operation is not allowed.
40      * @throws NoSuchFieldException if no field with the given name exists.
41      * @throws IllegalAccessException if the field is enforcing Java language access control and is inaccessible.
42      * @throws IllegalArgumentException if one of the passed parameters is invalid.
43      */
44     @SuppressWarnings("unchecked")
45     public static <T> T getPrivate(Object object, String fieldName)
46             throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
47         Field field = getFieldFromClassHierarchy(object.getClass(), fieldName);
48         field.setAccessible(true);
49         return (T) field.get(object);
50     }
51
52     private static Field getFieldFromClassHierarchy(Class<?> clazz, String fieldName)
53             throws NoSuchFieldException, SecurityException {
54         Class<?> iteratedClass = clazz;
55         do {
56             try {
57                 return iteratedClass.getDeclaredField(fieldName);
58             } catch (NoSuchFieldException e) {
59             }
60             iteratedClass = iteratedClass.getSuperclass();
61         } while (iteratedClass != null);
62         throw new NoSuchFieldException();
63     }
64
65     /**
66      * Sets a private attribute.
67      *
68      * @param object The object to set the attribute on.
69      * @param fieldName The name of the field to set.
70      * @param value The value to set.
71      * @throws SecurityException if the operation is not allowed.
72      * @throws NoSuchFieldException if no field with the given name exists.
73      * @throws IllegalAccessException if the field is enforcing Java language access control and is inaccessible.
74      * @throws IllegalArgumentException if one of the passed parameters is invalid.
75      */
76     public static void setPrivate(Object object, String fieldName, @Nullable Object value)
77             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
78         Field field = object.getClass().getDeclaredField(fieldName);
79         field.setAccessible(true);
80         field.set(object, value);
81     }
82
83     /**
84      * Sets an attribute declared as {@code private static final}.
85      *
86      * @param clazz The class owning the static attribute.
87      * @param fieldName The name of the attribute.
88      * @param value The new value.
89      * @throws NoSuchFieldException if no field with the given name exists.
90      * @throws SecurityException if the operation is not allowed.
91      * @throws IllegalArgumentException if one of the passed parameters is invalid.
92      * @throws IllegalAccessException if the field is enforcing Java language access control and is inaccessible.
93      */
94     public static void setPrivateStaticFinal(Class<?> clazz, String fieldName, @Nullable Object value)
95             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
96         Field field = clazz.getDeclaredField(fieldName);
97         field.setAccessible(true);
98
99         Field modifiersField = Field.class.getDeclaredField("modifiers");
100         modifiersField.setAccessible(true);
101         modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
102
103         field.set(null, value);
104     }
105
106     /**
107      * Invokes a private method on an object.
108      *
109      * @param object The object to invoke the method on.
110      * @param methodName The name of the method to invoke.
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     public static <T> T invokePrivate(Object object, String methodName, Object... parameters)
120             throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
121         Class<?>[] parameterTypes = new Class[parameters.length];
122         for (int i = 0; i < parameters.length; i++) {
123             parameterTypes[i] = parameters[i].getClass();
124         }
125
126         return invokePrivate(object, methodName, parameterTypes, parameters);
127     }
128
129     /**
130      * Invokes a private method on an object.
131      *
132      * @param object The object to invoke the method on.
133      * @param methodName The name of the method to invoke.
134      * @param parameterTypes The types of the parameters.
135      * @param parameters The parameters of the method invocation.
136      * @return The method call's return value.
137      * @throws NoSuchMethodException if no method with the given parameters or name exists.
138      * @throws SecurityException if the operation is not allowed.
139      * @throws IllegalAccessException if the method is enforcing Java language access control and is inaccessible.
140      * @throws IllegalArgumentException if one of the passed parameters is invalid.
141      * @throws InvocationTargetException if the invoked method throws an exception.
142      */
143     @SuppressWarnings("unchecked")
144     public static <T> T invokePrivate(Object object, String methodName, Class<?>[] parameterTypes, Object... parameters)
145             throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException {
146         Method method = getMethodFromClassHierarchy(object.getClass(), methodName, parameterTypes);
147         method.setAccessible(true);
148         try {
149             return (T) method.invoke(object, parameters);
150         } catch (InvocationTargetException e) {
151             throw new IllegalStateException(e.getCause());
152         }
153     }
154
155     private static Method getMethodFromClassHierarchy(Class<?> clazz, String methodName, Class<?>[] parameterTypes)
156             throws NoSuchMethodException {
157         Class<?> iteratedClass = clazz;
158         do {
159             try {
160                 return iteratedClass.getDeclaredMethod(methodName, parameterTypes);
161             } catch (NoSuchMethodException e) {
162             }
163             iteratedClass = iteratedClass.getSuperclass();
164         } while (iteratedClass != null);
165         throw new NoSuchMethodException();
166     }
167 }