< prev index next >

src/jdk.jdeps/share/classes/com/sun/tools/javap/StackMapWriter.java

Print this page

  1 /*
  2  * Copyright (c) 2009, 2024, 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 
 26 package com.sun.tools.javap;
 27 

 28 import java.util.HashMap;
 29 import java.util.List;
 30 import java.util.Map;
 31 import java.lang.classfile.Attributes;
 32 import java.lang.classfile.ClassFile;
 33 
 34 import java.lang.classfile.Instruction;
 35 import java.lang.classfile.attribute.CodeAttribute;
 36 import java.lang.classfile.attribute.StackMapFrameInfo;
 37 import java.lang.classfile.attribute.StackMapTableAttribute;
 38 
 39 /**
 40  * Annotate instructions with stack map.
 41  *
 42  *  <p><b>This is NOT part of any supported API.
 43  *  If you write code that depends on this, you do so at your own risk.
 44  *  This code and its internal interfaces are subject to change or
 45  *  deletion without notice.</b>
 46  */
 47 public class StackMapWriter extends InstructionDetailWriter {

 81             map.put(code.labelToBci(fr.target()), fr);
 82     }
 83 
 84     public void writeInitialDetails() {
 85         writeDetails(-1);
 86     }
 87 
 88     @Override
 89     public void writeDetails(int pc, Instruction instr) {
 90         writeDetails(pc);
 91     }
 92 
 93     private void writeDetails(int pc) {
 94         if (map == null)
 95             return;
 96 
 97         var m = map.get(pc);
 98         if (m != null) {
 99             print("StackMap locals: ", m.locals(), true);
100             print("StackMap stack: ", m.stack(), false);



101         }
102 
103     }
104 
















105     void print(String label, List<StackMapFrameInfo.VerificationTypeInfo> entries,
106             boolean firstThis) {
107         print(label);
108         for (var e : entries) {
109             print(" ");
110             print(e, firstThis);
111             firstThis = false;
112         }
113         println();
114     }
115 
116     void print(StackMapFrameInfo.VerificationTypeInfo entry, boolean firstThis) {
117         if (entry == null) {
118             print("ERROR");
119             return;
120         }
121 
122         switch (entry) {
123             case StackMapFrameInfo.SimpleVerificationTypeInfo s -> {
124                 switch (s) {

  1 /*
  2  * Copyright (c) 2009, 2025, 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 
 26 package com.sun.tools.javap;
 27 
 28 import java.lang.classfile.constantpool.NameAndTypeEntry;
 29 import java.util.HashMap;
 30 import java.util.List;
 31 import java.util.Map;
 32 import java.lang.classfile.Attributes;
 33 import java.lang.classfile.ClassFile;
 34 
 35 import java.lang.classfile.Instruction;
 36 import java.lang.classfile.attribute.CodeAttribute;
 37 import java.lang.classfile.attribute.StackMapFrameInfo;
 38 import java.lang.classfile.attribute.StackMapTableAttribute;
 39 
 40 /**
 41  * Annotate instructions with stack map.
 42  *
 43  *  <p><b>This is NOT part of any supported API.
 44  *  If you write code that depends on this, you do so at your own risk.
 45  *  This code and its internal interfaces are subject to change or
 46  *  deletion without notice.</b>
 47  */
 48 public class StackMapWriter extends InstructionDetailWriter {

 82             map.put(code.labelToBci(fr.target()), fr);
 83     }
 84 
 85     public void writeInitialDetails() {
 86         writeDetails(-1);
 87     }
 88 
 89     @Override
 90     public void writeDetails(int pc, Instruction instr) {
 91         writeDetails(pc);
 92     }
 93 
 94     private void writeDetails(int pc) {
 95         if (map == null)
 96             return;
 97 
 98         var m = map.get(pc);
 99         if (m != null) {
100             print("StackMap locals: ", m.locals(), true);
101             print("StackMap stack: ", m.stack(), false);
102             if (!m.unsetFields().isEmpty()) {
103                 printFields("StackMap unset fields: ", m.unsetFields());
104             }
105         }
106 
107     }
108 
109     void printFields(String label, List<NameAndTypeEntry> entries) {
110         print(label);
111         boolean first = true;
112         for (var e : entries) {
113             if (!first) {
114                 print(", ");
115             } else {
116                 first = false;
117             }
118             print(e::name);
119             print(":");
120             print(e::type);
121         }
122         println();
123     }
124 
125     void print(String label, List<StackMapFrameInfo.VerificationTypeInfo> entries,
126             boolean firstThis) {
127         print(label);
128         for (var e : entries) {
129             print(" ");
130             print(e, firstThis);
131             firstThis = false;
132         }
133         println();
134     }
135 
136     void print(StackMapFrameInfo.VerificationTypeInfo entry, boolean firstThis) {
137         if (entry == null) {
138             print("ERROR");
139             return;
140         }
141 
142         switch (entry) {
143             case StackMapFrameInfo.SimpleVerificationTypeInfo s -> {
144                 switch (s) {
< prev index next >