本文整理汇总了Java中java.lang.invoke.MethodHandles.Lookup类的典型用法代码示例。如果您正苦于以下问题:Java Lookup类的具体用法?Java Lookup怎么用?Java Lookup使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Lookup类属于java.lang.invoke.MethodHandles包,在下文中一共展示了Lookup类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeLists
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
static MethodHandle[] makeLists() {
ArrayList<MethodHandle> lists = new ArrayList<>();
MethodHandles.Lookup lookup = IMPL_LOOKUP;
for (;;) {
int nargs = lists.size();
MethodType type = MethodType.genericMethodType(nargs).changeReturnType(List.class);
String name = "list";
MethodHandle list = null;
try {
list = lookup.findStatic(ValueConversions.class, name, type);
} catch (ReflectiveOperationException ex) {
// break from loop!
}
if (list == null) break;
lists.add(list);
}
assertTrue(lists.size() == 11); // current number of methods
return lists.toArray(new MethodHandle[0]);
}
示例2: testPublicLookupToUnnamedModule
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* Teleport from publicLookup to public type in unnamed module
*
* [A0] has PUBLIC access
*/
public void testPublicLookupToUnnamedModule() throws Exception {
Lookup lookup = MethodHandles.publicLookup().in(unnamedClass);
assertTrue(lookup.lookupModes() == PUBLIC); // A0
// m1
findConstructor(lookup, p1_Type1, void.class);
findConstructorExpectingIAE(lookup, p2_Type2, void.class);
// m2
findConstructor(lookup, q1_Type1, void.class);
findConstructorExpectingIAE(lookup, q2_Type2, void.class);
// java.base
findConstructor(lookup, Object.class, void.class);
findConstructorExpectingIAE(lookup, x500NameClass, void.class, String.class);
// unnamed
findConstructor(lookup, unnamedClass, void.class);
}
示例3: testPublicLookupToAccessibleTypeInNamedModule
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* Hop from publicLookup to accessible type in named module.
*
* [A0] has PUBLIC access
*/
public void testPublicLookupToAccessibleTypeInNamedModule() throws Exception {
Lookup lookup = MethodHandles.publicLookup().in(p1_Type1);
assertTrue(lookup.lookupModes() == PUBLIC); // A0
// m1
findConstructor(lookup, p1_Type1, void.class);
findConstructorExpectingIAE(lookup, p2_Type2, void.class);
// m2
findConstructor(lookup, q1_Type1, void.class);
findConstructorExpectingIAE(lookup, q2_Type2, void.class);
// java.base
findConstructor(lookup, Object.class, void.class);
findConstructorExpectingIAE(lookup, x500NameClass, void.class, String.class);
// unnamed
findConstructor(lookup, unnamedClass, void.class);
}
示例4: testGetter
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
public void testGetter(int testMode) throws Throwable {
Lookup lookup = PRIVATE; // FIXME: test more lookups than this one
for (Object[] c : HasFields.CASES) {
boolean positive = (c[1] != Error.class);
testGetter(positive, lookup, c[0], c[1], testMode);
if (positive)
testGetter(positive, lookup, c[0], c[1], testMode | TEST_NPE);
}
testGetter(true, lookup,
new Object[]{ true, System.class, "out", java.io.PrintStream.class },
System.out, testMode);
for (int isStaticN = 0; isStaticN <= 1; isStaticN++) {
testGetter(false, lookup,
new Object[]{ (isStaticN != 0), System.class, "bogus", char.class },
null, testMode);
}
}
示例5: main
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
public static void main(String[] args) throws Throwable {
Lookup LOOKUP = T3.lookup();
Class<IllegalAccessException> IAE = IllegalAccessException.class;
assertFailure(IAE, () -> LOOKUP.findVirtual(T1.class, "m1", MethodType.methodType(void.class)));
assertFailure(IAE, () -> LOOKUP.findStatic(T1.class, "m2", MethodType.methodType(void.class)));
assertSuccess(() -> LOOKUP.findVirtual(T2.class, "m1", MethodType.methodType(void.class)));
assertSuccess(() -> LOOKUP.findVirtual(T3.class, "m1", MethodType.methodType(void.class)));
assertSuccess(() -> LOOKUP.findStatic(T2.class, "m2", MethodType.methodType(void.class)));
assertSuccess(() -> LOOKUP.findStatic(T3.class, "m2", MethodType.methodType(void.class)));
assertFailure(IAE, () -> LOOKUP.unreflect(T1.class.getDeclaredMethod("m1")));
assertFailure(IAE, () -> LOOKUP.unreflect(T1.class.getDeclaredMethod("m2")));
System.out.println("TEST PASSED");
}
示例6: main
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
public static void main(String[] args) throws Throwable {
Lookup lookup = MethodHandles.lookup();
Method fooMethod = Foo.class.getDeclaredMethod("getName");
// fooMH2 displaces fooMH1 from the MemberNamesTable
MethodHandle fooMH1 = lookup.unreflect(fooMethod);
MethodHandle fooMH2 = lookup.unreflect(fooMethod);
System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
System.out.println("fooMH2.invoke = " + fooMH2.invokeExact());
// Redefining Foo.getName() causes vmtarget to be updated
// in fooMH2 but not fooMH1
redefineFoo();
// Full GC causes fooMH1.vmtarget to be deallocated
System.gc();
// Calling fooMH1.vmtarget crashes the VM
System.out.println("fooMH1.invoke = " + fooMH1.invokeExact());
}
示例7: generate
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
private static MethodHandle generate(Lookup lookup, String className, MethodType mt, Recipe recipe) throws StringConcatException {
try {
switch (STRATEGY) {
case BC_SB:
return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.DEFAULT);
case BC_SB_SIZED:
return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.SIZED);
case BC_SB_SIZED_EXACT:
return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.SIZED_EXACT);
case MH_SB_SIZED:
return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED);
case MH_SB_SIZED_EXACT:
return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED_EXACT);
case MH_INLINE_SIZED_EXACT:
return MethodHandleInlineCopyStrategy.generate(mt, recipe);
default:
throw new StringConcatException("Concatenation strategy " + STRATEGY + " is not implemented");
}
} catch (Error | StringConcatException e) {
// Pass through any error or existing StringConcatException
throw e;
} catch (Throwable t) {
throw new StringConcatException("Generator failed", t);
}
}
示例8: testPublicLookupToBaseModule
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* Hop from publicLookup to accessible type in java.base
*/
public void testPublicLookupToBaseModule() throws Exception {
Lookup lookup = MethodHandles.publicLookup().in(String.class);
assertTrue(lookup.lookupModes() == PUBLIC); // A0
// m1
findConstructorExpectingIAE(lookup, p1_Type1, void.class);
findConstructorExpectingIAE(lookup, p2_Type2, void.class);
// m2
findConstructorExpectingIAE(lookup, q1_Type1, void.class);
findConstructorExpectingIAE(lookup, q2_Type2, void.class);
// java.base
findConstructor(lookup, Object.class, void.class);
findConstructorExpectingIAE(lookup, x500NameClass, void.class, String.class);
// unnamed
findConstructorExpectingIAE(lookup, unnamedClass, void.class);
}
示例9: testTargetClassInOpenModule
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
public void testTargetClassInOpenModule() throws Throwable {
// m1/p1.Type
Class<?> clazz = Class.forName("p1.Type");
assertEquals(clazz.getModule().getName(), "m1");
// ensure that this module reads m1
Module thisModule = getClass().getModule();
Module m1 = clazz.getModule();
thisModule.addReads(clazz.getModule());
assertTrue(m1.isOpen("p1", thisModule));
Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
assertTrue(lookup.lookupClass() == clazz);
assertTrue(lookup.hasPrivateAccess());
// get obj field
MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
Object obj = mh.invokeExact();
}
示例10: testDefineClass
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* Basic test of defineClass to define a class in the same package as test.
*/
@Test
public void testDefineClass() throws Exception {
final String CLASS_NAME = THIS_PACKAGE + ".Foo";
Lookup lookup = lookup();
Class<?> clazz = lookup.defineClass(generateClass(CLASS_NAME));
// test name
assertEquals(clazz.getName(), CLASS_NAME);
// test loader/package/protection-domain
testSameAbode(clazz, lookup.lookupClass());
// test discoverable
testDiscoverable(clazz, lookup);
// attempt defineClass again
try {
lookup.defineClass(generateClass(CLASS_NAME));
assertTrue(false);
} catch (LinkageError expected) { }
}
示例11: testLookup
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* MethodHandles.lookup()
*
* [A0] has module access
* [A1] can access all public types in m1
* [A2] can access public types in packages exported by modules that m1 reads
* [A3] cannot access public types in non-exported modules of modules that m1 reads
*/
public void testLookup() throws Exception {
Lookup lookup = MethodHandles.lookup();
assertTrue((lookup.lookupModes() & MODULE) == MODULE); // [A0]
// m1
findConstructor(lookup, p1_Type1, void.class); // [A1]
findConstructor(lookup, p2_Type2, void.class); // [A1]
// m2
findConstructor(lookup, q1_Type1, void.class); // [A2]
findConstructorExpectingIAE(lookup, q2_Type2, void.class); // [A3]
// java.base
findConstructor(lookup, Object.class, void.class); // [A2]
findConstructorExpectingIAE(lookup, x500NameClass, void.class, String.class); // [A3]
// unnamed
findConstructor(lookup, unnamedClass, void.class); // [A3]
}
示例12: testFromNamedToNamedModule
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
/**
* Hop to lookup class in another named module
*
* [A0] has no access
*/
public void testFromNamedToNamedModule() throws Exception {
Lookup lookup = MethodHandles.lookup().in(q1_Type1);
assertTrue(lookup.lookupModes() == 0); // [A0]
// m1
findConstructorExpectingIAE(lookup, p1_Type1, void.class);
findConstructorExpectingIAE(lookup, p2_Type2, void.class);
// m2
findConstructorExpectingIAE(lookup, q1_Type1, void.class);
findConstructorExpectingIAE(lookup, q2_Type2, void.class);
// java.base
findConstructorExpectingIAE(lookup, Object.class, void.class);
findConstructorExpectingIAE(lookup, x500NameClass, void.class, String.class);
// unnamed
findConstructorExpectingIAE(lookup, unnamedClass, void.class);
}
示例13: invokeDefaultMethod
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
@Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object,
Object... args) throws Throwable {
// Because the service interface might not be public, we need to use a MethodHandle lookup
// that ignores the visibility of the declaringClass.
Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
constructor.setAccessible(true);
return constructor.newInstance(declaringClass, -1 /* trusted */)
.unreflectSpecial(method, declaringClass)
.bindTo(object)
.invokeWithArguments(args);
}
示例14: testRunnableProxy0
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
public void testRunnableProxy0() throws Throwable {
if (CAN_SKIP_WORKING) return;
startTest("testRunnableProxy");
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle run = lookup.findStatic(lookup.lookupClass(), "runForRunnable", MethodType.methodType(void.class));
Runnable r = MethodHandleProxies.asInterfaceInstance(Runnable.class, run);
testRunnableProxy(r);
assertCalled("runForRunnable");
}
示例15: testFindConstructor
import java.lang.invoke.MethodHandles.Lookup; //导入依赖的package包/类
void testFindConstructor(boolean positive, Lookup lookup,
Class<?> defc, Class<?>... params) throws Throwable {
countTest(positive);
MethodType type = MethodType.methodType(void.class, params);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
target = lookup.findConstructor(defc, type);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
}
if (verbosity >= 3)
System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target
+(target == null ? "" : target.type())
+(noAccess == null ? "" : " !! "+noAccess));
if (positive && noAccess != null) throw noAccess;
assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
if (!positive) return; // negative test failed as expected
assertEquals(type.changeReturnType(defc), target.type());
Object[] args = randomArgs(params);
printCalled(target, defc.getSimpleName(), args);
Object obj = target.invokeWithArguments(args);
if (!(defc == Example.class && params.length < 2))
assertCalled(defc.getSimpleName()+".<init>", args);
assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}