本文整理汇总了Java中java.lang.invoke.MethodHandles.Lookup.lookupClass方法的典型用法代码示例。如果您正苦于以下问题:Java Lookup.lookupClass方法的具体用法?Java Lookup.lookupClass怎么用?Java Lookup.lookupClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles.Lookup
的用法示例。
在下文中一共展示了Lookup.lookupClass方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lookupsEqual
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
private static boolean lookupsEqual(final Lookup l1, final Lookup l2) {
if(l1 == l2) {
return true;
}
if(l1.lookupClass() != l2.lookupClass()) {
return false;
}
return l1.lookupModes() == l2.lookupModes();
}
示例2: testReducingAccess
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
/**
* Starting with a full power Lookup, use dropLookupMode to create new Lookups
* with reduced access.
*/
public void testReducingAccess() {
Lookup lookup = MethodHandles.lookup();
final Class<?> lc = lookup.lookupClass();
assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PROTECTED|PRIVATE));
lookup = lookup.dropLookupMode(PROTECTED);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE));
lookup = lookup.dropLookupMode(PRIVATE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE));
lookup = lookup.dropLookupMode(PACKAGE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == (PUBLIC|MODULE));
lookup = lookup.dropLookupMode(MODULE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
lookup = lookup.dropLookupMode(PUBLIC);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == 0);
// repeat with lookup has no access
lookup = lookup.dropLookupMode(PUBLIC);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == 0);
}
示例3: testPublicLookup
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
/**
* Test dropLookupMode on the public Lookup.
*/
public void testPublicLookup() {
final Lookup publicLookup = MethodHandles.publicLookup();
final Class<?> lc = publicLookup.lookupClass();
assertTrue(publicLookup.lookupModes() == (PUBLIC|UNCONDITIONAL));
Lookup lookup = publicLookup.dropLookupMode(PRIVATE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
lookup = publicLookup.dropLookupMode(PROTECTED);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
lookup = publicLookup.dropLookupMode(PACKAGE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
lookup = publicLookup.dropLookupMode(MODULE);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
lookup = publicLookup.dropLookupMode(PUBLIC);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == 0);
lookup = publicLookup.dropLookupMode(UNCONDITIONAL);
assertTrue(lookup.lookupClass() == lc);
assertTrue(lookup.lookupModes() == PUBLIC);
}
示例4: testFindSpecial
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
void testFindSpecial(boolean positive, Lookup lookup, Class<?> specialCaller,
Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
countTest(positive);
String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
MethodType type = MethodType.methodType(ret, params);
Lookup specialLookup = maybeMoveIn(lookup, specialCaller);
boolean specialAccessOK = (specialLookup.lookupClass() == specialCaller &&
(specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
MethodHandle target = null;
Exception noAccess = null;
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
if (verbosity >= 5) System.out.println(" lookup => "+specialLookup);
target = specialLookup.findSpecial(defc, methodName, type, specialCaller);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertExceptionClass(
(!specialAccessOK) // this check should happen first
? IllegalAccessException.class
: (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
? NoSuchMethodException.class
: IllegalAccessException.class,
noAccess);
if (verbosity >= 5) ex.printStackTrace(System.out);
}
if (verbosity >= 3)
System.out.println("findSpecial from "+specialCaller.getName()+" to "+defc.getName()+"."+name+"/"+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(specialCaller, target.type().parameterType(0));
assertEquals(type, target.type().dropParameterTypes(0,1));
Class<?>[] paramsWithSelf = cat(array(Class[].class, (Class)specialCaller), params);
MethodType typeWithSelf = MethodType.methodType(ret, paramsWithSelf);
assertNameStringContains(target, methodName);
Object[] args = randomArgs(paramsWithSelf);
printCalled(target, name, args);
target.invokeWithArguments(args);
assertCalled(name, args);
}
示例5: testUnreflectMaybeSpecial
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
void testUnreflectMaybeSpecial(Class<?> specialCaller,
boolean positive, Lookup lookup,
Class<?> defc, Class<?> rcvc, Class<?> ret, String name, Class<?>... params) throws Throwable {
countTest(positive);
String methodName = name.substring(1 + name.indexOf('/')); // foo/bar => foo
MethodType type = MethodType.methodType(ret, params);
Lookup specialLookup = (specialCaller != null ? maybeMoveIn(lookup, specialCaller) : null);
boolean specialAccessOK = (specialCaller != null &&
specialLookup.lookupClass() == specialCaller &&
(specialLookup.lookupModes() & Lookup.PRIVATE) != 0);
Method rmethod = defc.getDeclaredMethod(methodName, params);
MethodHandle target = null;
Exception noAccess = null;
boolean isStatic = (rcvc == null);
boolean isSpecial = (specialCaller != null);
try {
if (verbosity >= 4) System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
if (isSpecial)
target = specialLookup.unreflectSpecial(rmethod, specialCaller);
else
target = maybeMoveIn(lookup, defc).unreflect(rmethod);
} catch (ReflectiveOperationException ex) {
noAccess = ex;
assertExceptionClass(
IllegalAccessException.class, // NSME is impossible, since it was already reflected
noAccess);
if (verbosity >= 5) ex.printStackTrace(System.out);
}
if (verbosity >= 3)
System.out.println("unreflect"+(isSpecial?"Special":"")+" "+defc.getName()+"."+name+"/"+type
+(!isSpecial ? "" : " specialCaller="+specialCaller)
+( isStatic ? "" : " receiver="+rcvc)
+" => "+target
+(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(isStatic, Modifier.isStatic(rmethod.getModifiers()));
Class<?>[] paramsMaybeWithSelf = params;
if (!isStatic) {
paramsMaybeWithSelf = cat(array(Class[].class, (Class)rcvc), params);
}
MethodType typeMaybeWithSelf = MethodType.methodType(ret, paramsMaybeWithSelf);
if (isStatic) {
assertEquals(typeMaybeWithSelf, target.type());
} else {
if (isSpecial)
assertEquals(specialCaller, target.type().parameterType(0));
else
assertEquals(defc, target.type().parameterType(0));
assertEquals(typeMaybeWithSelf, target.type().changeParameterType(0, rcvc));
}
Object[] argsMaybeWithSelf = randomArgs(paramsMaybeWithSelf);
printCalled(target, name, argsMaybeWithSelf);
target.invokeWithArguments(argsMaybeWithSelf);
assertCalled(name, argsMaybeWithSelf);
if (verbosity >= 1)
System.out.print(':');
}
示例6: lookupsEqual
import java.lang.invoke.MethodHandles.Lookup; //导入方法依赖的package包/类
/**
* Compares two lookup objects for value-based equality. They are considered
* equal if they have the same
* {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and
* {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}.
* @param l1 first lookup
* @param l2 second lookup
* @return true if the two lookups are equal, false otherwise.
*/
private static boolean lookupsEqual(final Lookup l1, final Lookup l2) {
return l1.lookupClass() == l2.lookupClass() && l1.lookupModes() == l2.lookupModes();
}