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