< prev index next >

src/java.base/share/classes/java/lang/StackFrameInfo.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2016, 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 StackWalker walker;
  42     private final Class<?> declaringClass;
  43     private final Object memberName;
  44     private final short bci;
  45     private volatile StackTraceElement ste;
  46 
  47     /*
  48      * Create StackFrameInfo for StackFrameTraverser and LiveStackFrameTraverser
  49      * to use
  50      */
  51     StackFrameInfo(StackWalker walker) {
  52         this.walker = walker;
  53         this.declaringClass = null;
  54         this.bci = -1;
  55         this.memberName = JLIA.newMemberName();
  56     }
  57 
  58     // package-private called by StackStreamFactory to skip
  59     // the capability check
  60     Class<?> declaringClass() {
  61         return declaringClass;
  62     }
  63 
  64     // ----- implementation of StackFrame methods
  65 
  66     @Override
  67     public String getClassName() {
  68         return declaringClass.getName();
  69     }
  70 
  71     @Override
  72     public Class<?> getDeclaringClass() {
  73         walker.ensureAccessEnabled(RETAIN_CLASS_REFERENCE);
  74         return declaringClass;
  75     }
  76 
  77     @Override
  78     public String getMethodName() {
  79         return JLIA.getName(memberName);
  80     }
  81 
  82     @Override
  83     public MethodType getMethodType() {
  84         walker.ensureAccessEnabled(RETAIN_CLASS_REFERENCE);
  85         return JLIA.getMethodType(memberName);
  86     }
  87 
  88     @Override
  89     public String getDescriptor() {
  90         return JLIA.getMethodDescriptor(memberName);
  91     }
  92 
  93     @Override
  94     public int getByteCodeIndex() {
  95         // bci not available for native methods
  96         if (isNativeMethod())
  97             return -1;
  98 
  99         return bci;
 100     }
 101 
 102     @Override
 103     public String getFileName() {
 104         return toStackTraceElement().getFileName();


 119         return JLIA.isNative(memberName);
 120     }
 121 
 122     @Override
 123     public String toString() {
 124         return toStackTraceElement().toString();
 125     }
 126 
 127     @Override
 128     public StackTraceElement toStackTraceElement() {
 129         StackTraceElement s = ste;
 130         if (s == null) {
 131             synchronized (this) {
 132                 s = ste;
 133                 if (s == null) {
 134                     ste = s = StackTraceElement.of(this);
 135                 }
 136             }
 137         }
 138         return s;






 139     }
 140 }
   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();


 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 }
< prev index next >