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("}");
}
}
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);
}
};
}
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);
}
}
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);
}
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);
}
}
Aggregations