8281003: MethodHandles::lookup throws NPE if caller is null

Reviewed-by: ihse, mchung, jrose, alanb
This commit is contained in:
Tim Prinzing 2022-02-16 20:09:46 +00:00 committed by Mandy Chung
parent 847a99b53d
commit 67763df4dc
4 changed files with 160 additions and 2 deletions

View file

@ -109,14 +109,25 @@ public class MethodHandles {
* <p>
* This method is caller sensitive, which means that it may return different
* values to different callers.
* In cases where {@code MethodHandles.lookup} is called from a context where
* there is no caller frame on the stack (e.g. when called directly
* from a JNI attached thread), {@code IllegalCallerException} is thrown.
* To obtain a {@link Lookup lookup object} in such a context, use an auxiliary class that will
* implicitly be identified as the caller, or use {@link MethodHandles#publicLookup()}
* to obtain a low-privileged lookup instead.
* @return a lookup object for the caller of this method, with
* {@linkplain Lookup#ORIGINAL original} and
* {@linkplain Lookup#hasFullPrivilegeAccess() full privilege access}.
* @throws IllegalCallerException if there is no caller frame on the stack.
*/
@CallerSensitive
@ForceInline // to ensure Reflection.getCallerClass optimization
public static Lookup lookup() {
return new Lookup(Reflection.getCallerClass());
final Class<?> c = Reflection.getCallerClass();
if (c == null) {
throw new IllegalCallerException("no caller frame");
}
return new Lookup(c);
}
/**