8268829: Provide an optimized way to walk the stack with Class object only

8210375: StackWalker::getCallerClass throws UnsupportedOperationException

Reviewed-by: coleenp, dfuchs, bchristi
This commit is contained in:
Mandy Chung 2023-09-07 21:37:40 +00:00
parent 716201c77d
commit 111ecdbaf5
34 changed files with 982 additions and 622 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,20 +24,20 @@
*/
package java.lang;
import jdk.internal.access.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.vm.ContinuationScope;
import java.lang.StackWalker.StackFrame;
import java.lang.invoke.MethodType;
import java.lang.reflect.Modifier;
class StackFrameInfo implements StackFrame {
private static final JavaLangInvokeAccess JLIA =
SharedSecrets.getJavaLangInvokeAccess();
private final boolean retainClassRef;
private Object memberName; // MemberName initialized by VM
private int bci; // initialized by VM to >= 0
/**
* StackFrameInfo is an implementation of StackFrame that contains the
* class and method information. This is used by stack walker configured
* without StackWalker.Option#DROP_METHOD_INFO.
*/
class StackFrameInfo extends ClassFrameInfo {
private String name;
private Object type; // String or MethodType
private int bci; // set by VM to >= 0
private ContinuationScope contScope;
private volatile StackTraceElement ste;
@ -49,14 +49,13 @@ class StackFrameInfo implements StackFrame {
* @see StackStreamFactory.AbstractStackWalker#fetchStackFrames
*/
StackFrameInfo(StackWalker walker) {
this.retainClassRef = walker.retainClassRef;
this.memberName = JLIA.newMemberName();
super(walker);
}
// package-private called by StackStreamFactory to skip
// the capability check
Class<?> declaringClass() {
return JLIA.getDeclaringClass(memberName);
return JLIA.getDeclaringClass(classOrMemberName);
}
// ----- implementation of StackFrame methods
@ -66,26 +65,43 @@ class StackFrameInfo implements StackFrame {
return declaringClass().getName();
}
@Override
public Class<?> getDeclaringClass() {
ensureRetainClassRefEnabled();
return declaringClass();
}
@Override
public String getMethodName() {
return JLIA.getName(memberName);
if (name == null) {
expandStackFrameInfo();
assert name != null;
}
return name;
}
@Override
public MethodType getMethodType() {
ensureRetainClassRefEnabled();
return JLIA.getMethodType(memberName);
if (type == null) {
expandStackFrameInfo();
assert type != null;
}
if (type instanceof MethodType mt) {
return mt;
}
// type is not a MethodType yet. Convert it thread-safely.
synchronized (this) {
if (type instanceof String sig) {
type = JLIA.getMethodType(sig, declaringClass().getClassLoader());
}
}
return (MethodType)type;
}
// expand the name and type field of StackFrameInfo
private native void expandStackFrameInfo();
@Override
public String getDescriptor() {
return JLIA.getMethodDescriptor(memberName);
return getMethodType().descriptorString();
}
@Override
@ -114,7 +130,7 @@ class StackFrameInfo implements StackFrame {
@Override
public boolean isNativeMethod() {
return JLIA.isNative(memberName);
return Modifier.isNative(flags);
}
private String getContinuationScopeName() {
@ -139,10 +155,4 @@ class StackFrameInfo implements StackFrame {
}
return s;
}
private void ensureRetainClassRefEnabled() {
if (!retainClassRef) {
throw new UnsupportedOperationException("No access to RETAIN_CLASS_REFERENCE");
}
}
}