com.jfoenix.adapters.ReflectionHelper Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.jfoenix.adapters;
import sun.misc.Unsafe;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
/**
* This class is for breaking the module system of Java 9.
* @author huang
*/
public class ReflectionHelper {
private static Unsafe unsafe = null;
private static long objectFieldOffset;
static {
try {
unsafe = AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
});
Field overrideField = AccessibleObject.class.getDeclaredField("override");
objectFieldOffset = unsafe.objectFieldOffset(overrideField);
} catch (Throwable ex) {
}
}
private static void setAccessible(AccessibleObject obj) {
unsafe.putBoolean(obj, objectFieldOffset, true);
}
public static T invoke(Class cls, Object obj, String methodName) {
try {
Method method = cls.getDeclaredMethod(methodName);
setAccessible(method);
return (T) method.invoke(obj);
} catch (Throwable ex) {
throw new InternalError(ex);
}
}
public static T invoke(Object obj, String methodName) {
return invoke(obj.getClass(), obj, methodName);
}
public static Method getMethod(Class cls, String methodName) {
try {
Method method = cls.getDeclaredMethod(methodName);
setAccessible(method);
return method;
} catch (Throwable ex) {
throw new InternalError(ex);
}
}
public static Field getField(Class cls, String fieldName) {
try {
Field field = cls.getDeclaredField(fieldName);
setAccessible(field);
return field;
} catch (Throwable ex) {
return null;
}
}
public static T getFieldContent(Object obj, String fieldName) {
return getFieldContent(obj.getClass(), obj, fieldName);
}
public static T getFieldContent(Class cls, Object obj, String fieldName) {
try {
Field field = cls.getDeclaredField(fieldName);
setAccessible(field);
return (T) field.get(obj);
} catch (Throwable ex) {
return null;
}
}
public static void setFieldContent(Class cls, Object obj, String fieldName, Object content) {
try {
Field field = cls.getDeclaredField(fieldName);
setAccessible(field);
field.set(obj, content);
} catch (Throwable ex) {
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy