1 /*
   2  * Copyright (c) 2015, 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.util.Optional;
  33 import java.util.OptionalInt;
  34 
  35 class StackFrameInfo implements StackFrame {
  36     private final static JavaLangInvokeAccess JLIA =
  37         SharedSecrets.getJavaLangInvokeAccess();
  38 
  39     // Footprint improvement: MemberName::clazz can replace
  40     // StackFrameInfo::declaringClass.
  41 
  42     final StackWalker walker;
  43     final Class<?> declaringClass;
  44     final Object memberName;
  45     final short bci;
  46     private volatile StackTraceElement ste;
  47 
  48     /*
  49      * Create StackFrameInfo for StackFrameTraverser and LiveStackFrameTraverser
  50      * to use
  51      */
  52     StackFrameInfo(StackWalker walker) {
  53         this.walker = walker;
  54         this.declaringClass = null;
  55         this.bci = -1;
  56         this.memberName = JLIA.newMemberName();
  57     }
  58 
  59     @Override
  60     public String getClassName() {
  61         return declaringClass.getName();
  62     }
  63 
  64     @Override
  65     public Class<?> getDeclaringClass() {
  66         walker.ensureAccessEnabled(RETAIN_CLASS_REFERENCE);
  67         return declaringClass;
  68     }
  69 
  70     @Override
  71     public String getMethodName() {
  72         return JLIA.getName(memberName);
  73     }
  74 
  75     @Override
  76     public int getByteCodeIndex() {
  77         return bci;
  78     }
  79 
  80     @Override
  81     public final String getFileName() {
  82         if (isNativeMethod())
  83             return null;
  84 
  85         return toStackTraceElement().getFileName();
  86     }
  87 
  88     @Override
  89     public final int getLineNumber() {
  90         if (isNativeMethod())
  91             return -2;
  92 
  93         return toStackTraceElement().getLineNumber();
  94     }
  95 
  96 
  97     @Override
  98     public final boolean isNativeMethod() {
  99         return JLIA.isNative(memberName);
 100     }
 101 
 102     @Override
 103     public String toString() {
 104         return toStackTraceElement().toString();
 105     }
 106 
 107     /**
 108      * Fill in the fields of the given StackTraceElement
 109      */
 110     private native void toStackTraceElement0(StackTraceElement ste);
 111 
 112     @Override
 113     public StackTraceElement toStackTraceElement() {
 114         StackTraceElement s = ste;
 115         if (s == null) {
 116             synchronized (this) {
 117                 s = ste;
 118                 if (s == null) {
 119                     s = new StackTraceElement();
 120                     toStackTraceElement0(s);
 121                     ste = s;
 122                 }
 123             }
 124         }
 125         return s;
 126     }
 127 }