1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.lang;
  26 
  27 import jdk.internal.misc.JavaLangInvokeAccess;
  28 import jdk.internal.misc.SharedSecrets;
  29 
  30 import static java.lang.StackWalker.Option.*;
  31 import java.lang.StackWalker.StackFrame;
  32 import java.lang.invoke.MethodType;
  33 
  34 class StackFrameInfo implements StackFrame {
  35     private final static JavaLangInvokeAccess JLIA =
  36         SharedSecrets.getJavaLangInvokeAccess();
  37 
  38     // Footprint improvement: MemberName::clazz can replace
  39     // StackFrameInfo::declaringClass.
  40 
  41     private final boolean retainClassRef;
  42     private final Object memberName;
  43     private final short bci;
  44     private volatile StackTraceElement ste;
  45 
  46     /*
  47      * Create StackFrameInfo for StackFrameTraverser and LiveStackFrameTraverser
  48      * to use
  49      */
  50     StackFrameInfo(StackWalker walker) {
  51         this.retainClassRef = walker.retainClassRef;
  52         this.bci = -1;
  53         this.memberName = JLIA.newMemberName();
  54     }
  55 
  56     // package-private called by StackStreamFactory to skip
  57     // the capability check
  58     Class<?> declaringClass() {
  59         return JLIA.getDeclaringClass(memberName);
  60     }
  61 
  62     // ----- implementation of StackFrame methods
  63 
  64     @Override
  65     public String getClassName() {
  66         return declaringClass().getName();
  67     }
  68 
  69     @Override
  70     public Class<?> getDeclaringClass() {
  71         ensureRetainClassRefEnabled();
  72         return declaringClass();
  73     }
  74 
  75     @Override
  76     public String getMethodName() {
  77         return JLIA.getName(memberName);
  78     }
  79 
  80     @Override
  81     public MethodType getMethodType() {
  82         ensureRetainClassRefEnabled();
  83         return JLIA.getMethodType(memberName);
  84     }
  85 
  86     @Override
  87     public String getDescriptor() {
  88         return JLIA.getMethodDescriptor(memberName);
  89     }
  90 
  91     @Override
  92     public int getByteCodeIndex() {
  93         // bci not available for native methods
  94         if (isNativeMethod())
  95             return -1;
  96 
  97         return bci;
  98     }
  99 
 100     @Override
 101     public String getFileName() {
 102         return toStackTraceElement().getFileName();
 103     }
 104 
 105     @Override
 106     public int getLineNumber() {
 107         // line number not available for native methods
 108         if (isNativeMethod())
 109             return -2;
 110 
 111         return toStackTraceElement().getLineNumber();
 112     }
 113 
 114 
 115     @Override
 116     public boolean isNativeMethod() {
 117         return JLIA.isNative(memberName);
 118     }
 119 
 120     @Override
 121     public String toString() {
 122         return toStackTraceElement().toString();
 123     }
 124 
 125     @Override
 126     public StackTraceElement toStackTraceElement() {
 127         StackTraceElement s = ste;
 128         if (s == null) {
 129             synchronized (this) {
 130                 s = ste;
 131                 if (s == null) {
 132                     ste = s = StackTraceElement.of(this);
 133                 }
 134             }
 135         }
 136         return s;
 137     }
 138 
 139     private void ensureRetainClassRefEnabled() {
 140         if (!retainClassRef) {
 141             throw new UnsupportedOperationException("No access to RETAIN_CLASS_REFERENCE");
 142         }
 143     }
 144 }