Search in sources :

Example 91 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class TestDefenderMethodLookupAsm method test335DefenderSupersendsAsmAsMethodHandles.

/**
 * Construct the same tests as found in <i>org.openj9.test.jsr335.defendersupersends.asm</i>, and run these as MethodHandle tests.
 * Test details can be found in JAZZ103 39862, attachment 20515.
 */
@Test(groups = { "level.sanity" })
public void test335DefenderSupersendsAsmAsMethodHandles() throws Throwable {
    if (!ASMTESTS_CLASSES_LOADED) {
        Assert.fail("Could not load ASM test classes. See <clinit> for details.");
    }
    /* Construct test cases */
    ArrayList<LocalAsmTestCase> tests = new ArrayList<TestDefenderMethodLookupAsm.LocalAsmTestCase>(20);
    tests.add(new LocalAsmTestCase("O1", "E", 'A', null));
    tests.add(new LocalAsmTestCase("O2", "F", 'F', null));
    tests.add(new LocalAsmTestCase("O3", "G", 'G', null));
    tests.add(new LocalAsmTestCase("O4", "H", ' ', NoSuchMethodException.class));
    tests.add(new LocalAsmTestCase("S", "P", 'P', null));
    tests.add(new LocalAsmTestCase("T_K", "K", ' ', IncompatibleClassChangeError.class));
    tests.add(new LocalAsmTestCase("T_L", "L", 'L', null));
    tests.add(new LocalAsmTestCase("U", "O", ' ', ClassCastException.class));
    tests.add(new LocalAsmTestCase("V_K", "K", ' ', IncompatibleClassChangeError.class));
    tests.add(new LocalAsmTestCase("V_L", "L", 'L', null));
    tests.add(new LocalAsmTestCase("V_M", "M", 'B', null));
    tests.add(new LocalAsmTestCase("W_L", "L", 'L', null));
    tests.add(new LocalAsmTestCase("W_M", "M", 'B', null));
    tests.add(new LocalAsmTestCase("W_O", "O", 'D', null));
    tests.add(new LocalAsmTestCase("X_L", "L", 'L', null));
    tests.add(new LocalAsmTestCase("X_M", "M", 'B', null));
    tests.add(new LocalAsmTestCase("X_N", "N", 'B', null));
    tests.add(new LocalAsmTestCase("Y_M", "M", 'B', null));
    tests.add(new LocalAsmTestCase("Y_N", "N", 'B', null));
    tests.add(new LocalAsmTestCase("Z", "D", 'D', null));
    /* Run tests */
    boolean testsFailed = false;
    StringBuilder testResults = new StringBuilder();
    for (LocalAsmTestCase t : tests) {
        Class<? extends Object> defc = ASMTESTS_CLASSES.get(t.defc);
        Class<? extends Object> target = ASMTESTS_CLASSES.get(t.target);
        MethodHandle lookupMH = lookup().findStatic(target, "lookup", methodType(Lookup.class));
        Lookup l = (Lookup) lookupMH.invoke();
        try {
            MethodHandle mh = l.findSpecial(target, "m", methodType(char.class), target);
            char result = (char) mh.invoke(defc.newInstance());
            AssertJUnit.assertEquals(t.result, result);
        } catch (Throwable e) {
            /* Add failure to result if we got unexpected exception */
            if ((null == t.ex) || !(t.ex.isAssignableFrom(e.getClass()))) {
                testsFailed = true;
                testResults.append("\nTest ");
                testResults.append(t.defc);
                testResults.append("\n\tUnexpected exception - ");
                testResults.append(e.getClass());
                testResults.append(": ");
                testResults.append(e.getMessage());
            }
        }
    }
    if (testsFailed) {
        Assert.fail(testResults.toString());
    }
}
Also used : ArrayList(java.util.ArrayList) Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 92 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project openj9 by eclipse.

the class MethodHandleAPI_privateLookupIn method test_privateLookupIn_TargetClass_Itself_UnnamedModule.

/**
 * privateLookupIn test for a target class which is the caller class in an unnamed module
 */
@Test(groups = { "level.sanity" })
public static void test_privateLookupIn_TargetClass_Itself_UnnamedModule() throws Throwable {
    Lookup lookup = MethodHandles.lookup();
    Class<?> targetClass = lookup.lookupClass();
    Lookup mhprivateLookupIn = privateLookupIn(targetClass, lookup);
    Assert.assertEquals(mhprivateLookupIn.lookupClass(), targetClass);
    Assert.assertEquals(mhprivateLookupIn.lookupModes() & Lookup.PRIVATE, Lookup.PRIVATE);
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 93 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project es6draft by anba.

the class Properties method createInternalObjectLayout.

private static CompactLayout createInternalObjectLayout(Class<?> holder) {
    try {
        Lookup lookup = MethodHandles.publicLookup();
        CompatibilityExtension extension = holder.getAnnotation(CompatibilityExtension.class);
        CompatibilityOption option = extension != null ? extension.value() : null;
        Object prototypeValue = CompactLayout.EMPTY;
        ArrayList<PropertyLayout> properties = new ArrayList<>();
        boolean hasProto = false;
        for (Field field : holder.getDeclaredFields()) {
            if (!Modifier.isStatic(field.getModifiers()))
                continue;
            Value value = field.getAnnotation(Value.class);
            Prototype prototype = field.getAnnotation(Prototype.class);
            assert value == null || prototype == null;
            if (value != null) {
                properties.add(new ValueLayout(value, getRawValue(field)));
            }
            if (prototype != null) {
                assert !hasProto && (hasProto = true);
                prototypeValue = getRawValue(field);
            }
        }
        for (Method method : holder.getDeclaredMethods()) {
            if (!Modifier.isStatic(method.getModifiers()))
                continue;
            Function function = method.getAnnotation(Function.class);
            Accessor accessor = method.getAnnotation(Accessor.class);
            AliasFunction[] aliases = method.getAnnotationsByType(AliasFunction.class);
            TailCall tailCall = method.getAnnotation(TailCall.class);
            Value value = method.getAnnotation(Value.class);
            assert function == null || (accessor == null && value == null);
            assert accessor == null || (function == null && value == null);
            assert value == null || (function == null && accessor == null);
            assert aliases.length == 0 || function != null;
            assert tailCall == null || function != null;
            if (value != null) {
                MethodHandle mh = getComputedValueMethodHandle(lookup, method);
                properties.add(new ValueLayout(value, mh));
            } else if (accessor != null) {
                MethodHandle mh = getStaticMethodHandle(lookup, method);
                properties.add(new AccessorLayout(accessor, mh));
            } else if (function != null) {
                MethodHandle mh = getStaticMethodHandle(lookup, method);
                if (tailCall == null) {
                    properties.add(new FunctionLayout(function, mh));
                } else {
                    properties.add(new FunctionLayout(function, tailCall, mh));
                }
                for (AliasFunction a : aliases) {
                    properties.add(new AliasFunctionLayout(a, function));
                }
            }
        }
        return new CompactLayout(prototypeValue, properties, option);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) NativeFunction(com.github.anba.es6draft.runtime.types.builtins.NativeFunction) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction) NativeTailCallFunction(com.github.anba.es6draft.runtime.types.builtins.NativeTailCallFunction) Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 94 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project feign by OpenFeign.

the class DefaultMethodHandler method legacyReadLookup.

private Lookup legacyReadLookup() throws NoSuchFieldException, IllegalAccessException {
    Field field = Lookup.class.getDeclaredField("IMPL_LOOKUP");
    field.setAccessible(true);
    Lookup lookup = (Lookup) field.get(null);
    return lookup;
}
Also used : Field(java.lang.reflect.Field) Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 95 with Lookup

use of java.lang.invoke.MethodHandles.Lookup in project feign by OpenFeign.

the class DefaultMethodHandler method androidLookup.

public Lookup androidLookup(Class<?> declaringClass) throws InstantiationException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    Lookup lookup;
    try {
        // Android 9+ double reflection
        Class<?> classReference = Class.class;
        Class<?>[] classType = new Class[] { Class.class };
        Method reflectedGetDeclaredConstructor = classReference.getDeclaredMethod("getDeclaredConstructor", Class[].class);
        reflectedGetDeclaredConstructor.setAccessible(true);
        Constructor<?> someHiddenMethod = (Constructor<?>) reflectedGetDeclaredConstructor.invoke(Lookup.class, (Object) classType);
        lookup = (Lookup) someHiddenMethod.newInstance(declaringClass);
    } catch (IllegalAccessException ex0) {
        // Android < 9 reflection
        Constructor<Lookup> lookupConstructor = Lookup.class.getDeclaredConstructor(Class.class);
        lookupConstructor.setAccessible(true);
        lookup = lookupConstructor.newInstance(declaringClass);
    }
    return (lookup);
}
Also used : Constructor(java.lang.reflect.Constructor) Lookup(java.lang.invoke.MethodHandles.Lookup) Method(java.lang.reflect.Method)

Aggregations

Lookup (java.lang.invoke.MethodHandles.Lookup)216 Test (org.testng.annotations.Test)176 MethodHandle (java.lang.invoke.MethodHandle)96 MethodHandles.publicLookup (java.lang.invoke.MethodHandles.publicLookup)39 Method (java.lang.reflect.Method)36 SamePackageInnerClass (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass)17 MutableCallSite (java.lang.invoke.MutableCallSite)11 MethodHandles (java.lang.invoke.MethodHandles)9 SamePackageInnerClass_Nested_Level2 (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass.SamePackageInnerClass_Nested_Level2)7 PackageExamples (examples.PackageExamples)7 MethodHandles.constant (java.lang.invoke.MethodHandles.constant)4 MethodHandles.dropArguments (java.lang.invoke.MethodHandles.dropArguments)4 MethodHandles.exactInvoker (java.lang.invoke.MethodHandles.exactInvoker)4 MethodHandles.foldArguments (java.lang.invoke.MethodHandles.foldArguments)4 MethodType.methodType (java.lang.invoke.MethodType.methodType)4 Constructor (java.lang.reflect.Constructor)4 ArrayList (java.util.ArrayList)4 Objects (java.util.Objects)4 Function (java.util.function.Function)4 ToDoubleFunction (java.util.function.ToDoubleFunction)4