All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gilecode.reflection.impl.UnsafeReflectionAccessor Maven / Gradle / Ivy

There is a newer version: 0.62.3
Show newest version
package com.gilecode.reflection.impl;

import com.gilecode.reflection.ReflectionAccessor;
import sun.misc.Unsafe;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;

/**
 * An implementation of {@link ReflectionAccessor} based on {@link Unsafe}.
 * 

* NOTE: This implementation is designed for Java 9. Although it should work with earlier Java releases, it is better to * use {@link PreJava9ReflectionAccessor} for them. * * @author Andrey Mogilev */ public class UnsafeReflectionAccessor implements ReflectionAccessor { private static final Unsafe theUnsafe = getUnsafeInstance(); private static final Field overrideField = getOverrideField(); /** * {@inheritDoc} */ public void makeAccessible(AccessibleObject ao) { if (theUnsafe != null && overrideField != null) { long overrideOffset = theUnsafe.objectFieldOffset(overrideField); theUnsafe.putBoolean(ao, overrideOffset, true); } } private static Unsafe getUnsafeInstance() { try { Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); unsafeField.setAccessible(true); return (Unsafe) unsafeField.get(null); } catch (IllegalAccessException|NoSuchFieldException e) { e.printStackTrace(); return null; } } private static Field getOverrideField() { try { return AccessibleObject.class.getDeclaredField("override"); } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy