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

manifold.util.ReflectionHack_1212 Maven / Gradle / Ivy

There is a newer version: 2024.1.33
Show newest version
/*
 * Copyright (c) 2019 - Manifold Systems LLC
 *
 * Licensed 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 manifold.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Set;
import jdk.internal.reflect.Reflection;
import sun.misc.Unsafe;

/**
 * Java 12 made changes in {@link Reflection} to filter fields that used to be available from calls such as
 * {@code getDeclaredFields()}. Fields like 'override' are filtered, which messes up reflective calls that force needed
 * access to otherwise inaccessible members.
 * 

* This class is a hack that shuts down the filtering added to Java 12. */ public class ReflectionHack_12 { public static void hackReflection() { try { Unsafe unsafe = NecessaryEvilUtil.getUnsafe(); // Must approximate the {@link AccessibleObject#override} field offset (because it is one of the filtered fields) long overrideOffset = AccessibleObject_layout.getOverrideOffset( unsafe ); // Change the value in Reflection.ALL_MEMBERS singleton set so that nothing is filtered Method addOpens = Module.class.getDeclaredMethod( "implAddOpens", String.class, Module.class ); unsafe.putObject( addOpens, overrideOffset, true ); addOpens.invoke( String.class.getModule(), "jdk.internal.reflect", NecessaryEvilUtil.class.getModule() ); //SharedSecrets.getJavaLangAccess().addExports( String.class.getModule(), "jdk.internal.reflect", NecessaryEvilUtil.class.getModule() ); Set allFilter = Reflection.ALL_MEMBERS; Field e0 = allFilter.getClass().getDeclaredField( "e0" ); long i_e0 = unsafe.objectFieldOffset( e0 ); unsafe.putObject( allFilter, i_e0, "" ); } catch( Exception e ) { throw new RuntimeException( e ); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy