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