Search in sources :

Example 6 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project jdk8u_jdk by JetBrains.

the class AccessControlTest_sibling method makeCases.

private void makeCases(Lookup[] originalLookups) {
    // make initial set of lookup test cases
    CASES.clear();
    LOADERS.clear();
    CASE_EDGES.clear();
    ArrayList<Class<?>> classes = new ArrayList<>();
    for (Lookup l : originalLookups) {
        CASES.add(new LookupCase(l));
        // no dups please
        classes.remove(l.lookupClass());
        classes.add(l.lookupClass());
    }
    System.out.println("loaders = " + LOADERS);
    int rounds = 0;
    for (int lastCount = -1; lastCount != CASES.size(); ) {
        // if CASES grow in the loop we go round again
        lastCount = CASES.size();
        for (LookupCase lc1 : CASES.toArray(new LookupCase[0])) {
            for (Class<?> c2 : classes) {
                LookupCase lc2 = new LookupCase(lc1.lookup().in(c2));
                addLookupEdge(lc1, c2, lc2);
                CASES.add(lc2);
            }
        }
        rounds++;
    }
    System.out.println("filled in " + CASES.size() + " cases from " + originalLookups.length + " original cases in " + rounds + " rounds");
    if (false) {
        System.out.println("CASES: {");
        for (LookupCase lc : CASES) {
            System.out.println(lc);
            Set<LookupCase> edges = CASE_EDGES.get(lc);
            if (edges != null)
                for (LookupCase prev : edges) {
                    System.out.println("\t" + prev);
                }
        }
        System.out.println("}");
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup)

Example 7 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project exotic by forax.

the class StableField method doubleGetter.

/**
 * Create a getter on a field of type {@code double} of a class with a stable semantics.
 *
 * <p>If the field is not initialized or initialized with its default value, the default value
 * will be returned when calling the getter. If the field is initialized with another value than
 * the default value, the getter will return the first value of the field observed by the getter,
 * any subsequent calls to the getter will return this same value.
 *
 * <p>If the getter has observed a value different from the default value, any subsequent calls to
 * the getter need to pass the same object as argument of the getter.
 *
 * <p>This call is equivalent to a call to {@link #getter(Lookup, Class, String, Class)} with
 * {@code double.class} as last argument that returns a getter that doesn't box the return value.
 *
 * @param <T> the type of the object containing the field.
 * @param lookup a lookup object that can access to the field.
 * @param declaringClass the class that declares the field.
 * @param name the name of the field.
 * @return a function that takes an object of the {@code declaring class} and returns the value of
 *     the field.
 * @throws NullPointerException if either the lookup, the declaring class or the name is null.
 * @throws NoSuchFieldError if the field doesn't exist.
 * @throws IllegalAccessError if the field is not accessible from the lookup.
 * @throws IllegalStateException if the argument of the getter is not constant.
 */
public static <T> ToDoubleFunction<T> doubleGetter(Lookup lookup, Class<T> declaringClass, String name) {
    Objects.requireNonNull(lookup);
    Objects.requireNonNull(declaringClass);
    Objects.requireNonNull(name);
    MethodHandle getter = createGetter(lookup, declaringClass, name, double.class);
    MethodHandle mh = new StableFieldCS(getter, double.class).dynamicInvoker();
    return object -> {
        try {
            return (double) mh.invokeExact(object);
        } catch (Throwable t) {
            throw Thrower.rethrow(t);
        }
    };
}
Also used : MethodType.methodType(java.lang.invoke.MethodType.methodType) MethodHandles.constant(java.lang.invoke.MethodHandles.constant) MethodHandles.dropArguments(java.lang.invoke.MethodHandles.dropArguments) MethodHandle(java.lang.invoke.MethodHandle) MethodHandles(java.lang.invoke.MethodHandles) ToIntFunction(java.util.function.ToIntFunction) Function(java.util.function.Function) Objects(java.util.Objects) Lookup(java.lang.invoke.MethodHandles.Lookup) MethodHandles.exactInvoker(java.lang.invoke.MethodHandles.exactInvoker) ToDoubleFunction(java.util.function.ToDoubleFunction) MethodHandles.foldArguments(java.lang.invoke.MethodHandles.foldArguments) MutableCallSite(java.lang.invoke.MutableCallSite) ToLongFunction(java.util.function.ToLongFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with MethodHandles.lookup

use of java.lang.invoke.MethodHandles.lookup in project ceylon by eclipse.

the class Array method $set$.

@Ignore
@Override
public void $set$(ReachableReference reference, java.lang.Object instance) {
    if (reference instanceof Member && getArraySize().equals(((Member) reference).getAttribute())) {
        int size = toSize(((ceylon.language.Integer) instance).value);
        try {
            Lookup lookup = MethodHandles.lookup();
            Util.setter(lookup, "size").invokeExact(this, size);
            java.lang.Object array = createArrayWithElement($reifiedElement, size, null);
            Util.setter(lookup, "array").invokeExact(this, array);
            if (elementType.fieldName != null) {
                MethodHandle fieldSetter = Util.setter(lookup, elementType.fieldName);
                switch(elementType) {
                    case CeylonInteger:
                        fieldSetter.invokeExact(this, (long[]) array);
                        break;
                    case CeylonFloat:
                        fieldSetter.invokeExact(this, (double[]) array);
                        break;
                    case CeylonCharacter:
                        fieldSetter.invokeExact(this, (int[]) array);
                        break;
                    case CeylonByte:
                        fieldSetter.invokeExact(this, (byte[]) array);
                        break;
                    case CeylonBoolean:
                        fieldSetter.invokeExact(this, (boolean[]) array);
                        break;
                    case CeylonString:
                        fieldSetter.invokeExact(this, (java.lang.String[]) array);
                        break;
                    case Other:
                        fieldSetter.invokeExact(this, (java.lang.Object[]) array);
                        break;
                    default:
                        fieldSetter.invoke(this, array);
                        break;
                }
            }
        } catch (java.lang.Throwable t) {
            rethrow_.rethrow(t);
        }
    } else {
        ceylon.language.serialization.Element index = (ceylon.language.serialization.Element) reference;
        set(index.getIndex(), (Element) instance);
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Member(ceylon.language.serialization.Member) MethodHandle(java.lang.invoke.MethodHandle) Ignore(org.eclipse.ceylon.compiler.java.metadata.Ignore)

Example 9 with MethodHandles.lookup

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

the class Test_MethodHandleInfo method test_RevealDirect_Security.

/**
 * Check that a SecurityException is thrown when ...
 *
 * @throws Throwable
 */
@Test(groups = { "level.sanity" })
public void test_RevealDirect_Security() throws Throwable {
    final String lookup = "org.openj9.test.java.lang.invoke.Helper_MethodHandleInfo";
    final String methodHandle = "org.openj9.test.java.lang.invoke.helpers.Helper_MethodHandleInfoOtherPackagePublic";
    // Get lookup object from class loader L1
    ClassLoader l1 = new ClassLoader(Test_MethodHandleInfo.class.getClassLoader()) {

        @Override
        public Class<?> loadClass(String className) throws ClassNotFoundException {
            if (className.equals(lookup)) {
                return getClass(className);
            }
            if (className.equals(methodHandle)) {
                return getClass(className);
            }
            return super.loadClass(className);
        }

        private Class<?> getClass(String className) throws ClassNotFoundException {
            String classFile = className.replace('.', '/') + ".class";
            try {
                InputStream classStream = getClass().getClassLoader().getResourceAsStream(classFile);
                if (classStream == null) {
                    throw new ClassNotFoundException("Error loading class : " + classFile);
                }
                int size = classStream.available();
                byte[] classRep = new byte[size];
                DataInputStream in = new DataInputStream(classStream);
                in.readFully(classRep);
                in.close();
                Class<?> clazz = defineClass(className, classRep, 0, classRep.length);
                return clazz;
            } catch (IOException e) {
                throw new ClassNotFoundException(e.getMessage());
            }
        }
    };
    Class classL1 = l1.loadClass(lookup);
    Method mL1 = classL1.getDeclaredMethod("lookup");
    Lookup lookup1 = (Lookup) mL1.invoke(null);
    // Get MethodHandle from class loader L2
    ClassLoader l2 = new ClassLoader(Test_MethodHandleInfo.class.getClassLoader()) {

        @Override
        public Class<?> loadClass(String className) throws ClassNotFoundException {
            if (className.equals(methodHandle)) {
                return getClass(className);
            }
            return super.loadClass(className);
        }

        private Class<?> getClass(String className) throws ClassNotFoundException {
            String classFile = className.replace('.', '/') + ".class";
            try {
                InputStream classStream = getClass().getClassLoader().getResourceAsStream(classFile);
                if (classStream == null) {
                    throw new ClassNotFoundException("Error loading class : " + classFile);
                }
                int size = classStream.available();
                byte[] classRep = new byte[size];
                DataInputStream in = new DataInputStream(classStream);
                in.readFully(classRep);
                in.close();
                Class<?> clazz = defineClass(className, classRep, 0, classRep.length);
                return clazz;
            } catch (IOException e) {
                throw new ClassNotFoundException(e.getMessage());
            }
        }
    };
    Class classL2 = l2.loadClass(methodHandle);
    Method mL2 = classL2.getDeclaredMethod("publicClassMethodHandle");
    MethodHandle mh = (MethodHandle) mL2.invoke(null);
    Method m2L2 = classL2.getDeclaredMethod("lookup");
    Lookup lookup2 = (Lookup) m2L2.invoke(null);
    helper_turnOwnSecurityManagerOn();
    boolean ACEThrown = false;
    try {
        MethodHandleInfo mhi1 = lookup1.revealDirect(mh);
    } catch (AccessControlException e) {
        ACEThrown = true;
    } catch (IllegalArgumentException e) {
        Assert.fail("IllegalArgumentException thrown, expected AccessControlException.");
    } finally {
        helper_resetSecurityManager();
    }
    AssertJUnit.assertTrue(ACEThrown);
}
Also used : DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Example 10 with MethodHandles.lookup

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

the class Test_MethodHandleInfo method test_RevealDirect_SamePackage.

/**
 * Check access to MethodHandleInfo of methods from the same package using local lookup object.
 * Private and protected should fail, default and public should pass.
 *
 * @throws Throwable
 */
@Test(groups = { "level.sanity" })
public void test_RevealDirect_SamePackage() throws Throwable {
    Lookup findLookup = Helper_MethodHandleInfoSubClass.lookup();
    Lookup lookup = lookup();
    /* Private */
    {
        boolean IAEThrown = false;
        MethodHandle mh = findLookup.findVirtual(Helper_MethodHandleInfoSubClass.class, "privateMethod", methodType(String.class));
        try {
            lookup.revealDirect(mh);
        } catch (IllegalArgumentException e) {
            IAEThrown = true;
        }
        AssertJUnit.assertTrue(IAEThrown);
    }
    /* Protected */
    {
        MethodHandle mh = findLookup.findVirtual(Helper_MethodHandleInfoSubClass.class, "protectedMethod", methodType(String.class));
        lookup.revealDirect(mh);
    }
    /* Default */
    {
        MethodHandle mh = findLookup.findVirtual(Helper_MethodHandleInfoSubClass.class, "packageMethod", methodType(String.class));
        lookup.revealDirect(mh);
    }
    /* Public */
    {
        MethodHandle mh = findLookup.findVirtual(Helper_MethodHandleInfoSubClass.class, "publicMethod", methodType(String.class));
        lookup.revealDirect(mh);
    }
}
Also used : Lookup(java.lang.invoke.MethodHandles.Lookup) Test(org.testng.annotations.Test)

Aggregations

Lookup (java.lang.invoke.MethodHandles.Lookup)218 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)37 SamePackageInnerClass (com.ibm.j9.jsr292.SamePackageExample.SamePackageInnerClass)17 MutableCallSite (java.lang.invoke.MutableCallSite)11 MethodHandles (java.lang.invoke.MethodHandles)10 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