< prev index next >
src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Print this page
*** 111,120 ****
--- 111,133 ----
public static Lookup lookup() {
return new Lookup(Reflection.getCallerClass());
}
/**
+ * This reflected$lookup method is the alternate implementation of
+ * the lookup method when being invoked by reflection.
+ */
+ @CallerSensitive
+ private static Lookup reflected$lookup() {
+ Class<?> caller = Reflection.getCallerClass();
+ if (caller.getClassLoader() == null) {
+ throw newIllegalArgumentException("illegal lookupClass: "+caller);
+ }
+ return new Lookup(caller);
+ }
+
+ /**
* Returns a {@link Lookup lookup object} which is trusted minimally.
* The lookup has the {@code PUBLIC} and {@code UNCONDITIONAL} modes.
* It can only be used to create method handles to public members of
* public classes in packages that are exported unconditionally.
* <p>
*** 745,755 ****
* which in turn is called by a method not in this package.
*/
Lookup(Class<?> lookupClass) {
this(lookupClass, FULL_POWER_MODES);
// make sure we haven't accidentally picked up a privileged class:
! checkUnprivilegedlookupClass(lookupClass, FULL_POWER_MODES);
}
private Lookup(Class<?> lookupClass, int allowedModes) {
this.lookupClass = lookupClass;
this.allowedModes = allowedModes;
--- 758,768 ----
* which in turn is called by a method not in this package.
*/
Lookup(Class<?> lookupClass) {
this(lookupClass, FULL_POWER_MODES);
// make sure we haven't accidentally picked up a privileged class:
! checkUnprivilegedlookupClass(lookupClass);
}
private Lookup(Class<?> lookupClass, int allowedModes) {
this.lookupClass = lookupClass;
this.allowedModes = allowedModes;
*** 825,835 ****
// The requested class it not accessible from the lookup class.
// No permissions.
newModes = 0;
}
! checkUnprivilegedlookupClass(requestedLookupClass, newModes);
return new Lookup(requestedLookupClass, newModes);
}
/**
--- 838,848 ----
// The requested class it not accessible from the lookup class.
// No permissions.
newModes = 0;
}
! checkUnprivilegedlookupClass(requestedLookupClass);
return new Lookup(requestedLookupClass, newModes);
}
/**
*** 982,1010 ****
* It can only be used to create method handles to publicly accessible
* members in packages that are exported unconditionally.
*/
static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, (PUBLIC|UNCONDITIONAL));
! private static void checkUnprivilegedlookupClass(Class<?> lookupClass, int allowedModes) {
String name = lookupClass.getName();
if (name.startsWith("java.lang.invoke."))
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
-
- // For caller-sensitive MethodHandles.lookup() disallow lookup from
- // restricted packages. This a fragile and blunt approach.
- // TODO replace with a more formal and less fragile mechanism
- // that does not bluntly restrict classes under packages within
- // java.base from looking up MethodHandles or VarHandles.
- if (allowedModes == FULL_POWER_MODES && lookupClass.getClassLoader() == null) {
- if ((name.startsWith("java.") &&
- !name.equals("java.lang.Thread") &&
- !name.startsWith("java.util.concurrent.")) ||
- (name.startsWith("sun.") &&
- !name.startsWith("sun.invoke."))) {
- throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
- }
- }
}
/**
* Displays the name of the class from which lookups are to be made.
* (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
--- 995,1008 ----
* It can only be used to create method handles to publicly accessible
* members in packages that are exported unconditionally.
*/
static final Lookup PUBLIC_LOOKUP = new Lookup(Object.class, (PUBLIC|UNCONDITIONAL));
! private static void checkUnprivilegedlookupClass(Class<?> lookupClass) {
String name = lookupClass.getName();
if (name.startsWith("java.lang.invoke."))
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);
}
/**
* Displays the name of the class from which lookups are to be made.
* (The name is the one reported by {@link java.lang.Class#getName() Class.getName}.)
< prev index next >