2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.util;
15 import java.lang.reflect.Field;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.lang.reflect.Modifier;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * Utility class for reflection operations such as accessing private fields or methods.
26 * @author Björn Lange - Initial contribution
29 public final class ReflectionUtil {
30 private ReflectionUtil() {
34 * Gets a private attribute.
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.
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);
52 private static Field getFieldFromClassHierarchy(Class<?> clazz, String fieldName)
53 throws NoSuchFieldException, SecurityException {
54 Class<?> iteratedClass = clazz;
57 return iteratedClass.getDeclaredField(fieldName);
58 } catch (NoSuchFieldException e) {
60 iteratedClass = iteratedClass.getSuperclass();
61 } while (iteratedClass != null);
62 throw new NoSuchFieldException();
66 * Sets a private attribute.
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.
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);
84 * Sets an attribute declared as {@code private static final}.
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.
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);
99 Field modifiersField = Field.class.getDeclaredField("modifiers");
100 modifiersField.setAccessible(true);
101 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
103 field.set(null, value);
107 * Invokes a private method on an object.
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.
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();
126 return invokePrivate(object, methodName, parameterTypes, parameters);
130 * Invokes a private method on an object.
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.
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);
149 return (T) method.invoke(object, parameters);
150 } catch (InvocationTargetException e) {
151 throw new IllegalStateException(e.getCause());
155 private static Method getMethodFromClassHierarchy(Class<?> clazz, String methodName, Class<?>[] parameterTypes)
156 throws NoSuchMethodException {
157 Class<?> iteratedClass = clazz;
160 return iteratedClass.getDeclaredMethod(methodName, parameterTypes);
161 } catch (NoSuchMethodException e) {
163 iteratedClass = iteratedClass.getSuperclass();
164 } while (iteratedClass != null);
165 throw new NoSuchMethodException();