001/**
002 * Copyright (C) 2006-2022 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.runtime.reflect;
017
018import static lombok.AccessLevel.PRIVATE;
019
020import java.lang.invoke.MethodHandles;
021import java.lang.reflect.Constructor;
022import java.lang.reflect.Field;
023import java.lang.reflect.Method;
024
025import lombok.NoArgsConstructor;
026import lombok.extern.slf4j.Slf4j;
027
028@Slf4j
029@NoArgsConstructor(access = PRIVATE)
030public class Defaults {
031
032    private static final Handler HANDLER;
033
034    static {
035        try {
036            /**
037             * Disable Access Warnings:
038             */
039            Class unsafeClazz = Class.forName("sun.misc.Unsafe");
040            Field field = unsafeClazz.getDeclaredField("theUnsafe");
041            field.setAccessible(true);
042            Object unsafe = field.get(null);
043            Method putObjectVolatile =
044                    unsafeClazz.getDeclaredMethod("putObjectVolatile", Object.class, long.class, Object.class);
045            Method staticFieldOffset = unsafeClazz.getDeclaredMethod("staticFieldOffset", Field.class);
046            Class loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
047            Field loggerField = loggerClass.getDeclaredField("logger");
048            Long offset = (Long) staticFieldOffset.invoke(unsafe, loggerField);
049            putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
050        } catch (Exception e) {
051            System.err.println("Disabling unsafe warnings failed: " + e.getMessage());
052        }
053        final String version = System.getProperty("java.version", "1.8");
054        final Boolean isJava8 = version.startsWith("1.8.") || version.startsWith("8.");
055        final Constructor<MethodHandles.Lookup> constructor = findLookupConstructor(isJava8);
056        if (isJava8) { // j8
057            HANDLER = (clazz, method, proxy, args) -> constructor
058                    .newInstance(clazz, MethodHandles.Lookup.PRIVATE)
059                    .unreflectSpecial(method, clazz)
060                    .bindTo(proxy)
061                    .invokeWithArguments(args);
062        } else { // j > 8 - can need some --add-opens, we will add a module-info later to be clean when dropping j8
063            final Method privateLookup = findPrivateLookup();
064            HANDLER = (clazz, method, proxy, args) -> MethodHandles.Lookup.class
065                    .cast(privateLookup.invoke(null, clazz, constructor.newInstance(clazz)))
066                    .unreflectSpecial(method, clazz)
067                    .bindTo(proxy)
068                    .invokeWithArguments(args);
069        }
070    }
071
072    public static boolean isDefaultAndShouldHandle(final Method method) {
073        return method.isDefault();
074    }
075
076    public static Object handleDefault(final Class<?> declaringClass, final Method method, final Object proxy,
077            final Object[] args) throws Throwable {
078        return HANDLER.handle(declaringClass, method, proxy, args);
079    }
080
081    private interface Handler {
082
083        Object handle(Class<?> clazz, Method method, Object proxy, Object[] args) throws Throwable;
084    }
085
086    private static Method findPrivateLookup() {
087        try {
088            return MethodHandles.class.getMethod("privateLookupIn", Class.class, MethodHandles.Lookup.class);
089        } catch (final Exception e) {
090            throw new IllegalStateException(e);
091        }
092    }
093
094    private static Constructor<MethodHandles.Lookup> findLookupConstructor(final Boolean isJava8) {
095        try {
096            Constructor<MethodHandles.Lookup> constructor;
097            if (isJava8) {
098                constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
099            } else {
100                constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class);
101            }
102            if (!constructor.isAccessible()) {
103                // this needs the `--add-opens java.base/java.lang.invoke=ALL-UNNAMED` jvm flag when java9+.
104                constructor.setAccessible(true);
105            }
106            return constructor;
107        } catch (final Exception e) {
108            throw new IllegalStateException(e);
109        }
110    }
111}