jjg@284: /* ohair@554: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. jjg@284: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@284: * jjg@284: * This code is free software; you can redistribute it and/or modify it jjg@284: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@284: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@284: * jjg@284: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@284: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@284: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@284: * version 2 for more details (a copy is included in the LICENSE file that jjg@284: * accompanied this code). jjg@284: * jjg@284: * You should have received a copy of the GNU General Public License version jjg@284: * 2 along with this work; if not, write to the Free Software Foundation, jjg@284: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@284: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@284: */ jjg@284: jjg@284: package com.sun.tools.javap; jjg@284: jjg@284: import com.sun.tools.classfile.Attribute; jjg@284: import com.sun.tools.classfile.Code_attribute; jjg@284: import com.sun.tools.classfile.ConstantPool; jjg@284: import com.sun.tools.classfile.ConstantPoolException; jjg@284: import com.sun.tools.classfile.Descriptor; jjg@284: import com.sun.tools.classfile.Descriptor.InvalidDescriptor; jjg@284: import com.sun.tools.classfile.Instruction; jjg@284: import com.sun.tools.classfile.LocalVariableTypeTable_attribute; jjg@284: import com.sun.tools.classfile.Signature; jjg@284: import java.util.ArrayList; jjg@284: import java.util.HashMap; jjg@284: import java.util.List; jjg@284: import java.util.ListIterator; jjg@284: import java.util.Map; jjg@284: jjg@284: /** jjg@284: * Annotate instructions with details about local variables. jjg@284: * jjg@284: *

This is NOT part of any API supported by Sun Microsystems. If jjg@284: * you write code that depends on this, you do so at your own risk. jjg@284: * This code and its internal interfaces are subject to change or jjg@284: * deletion without notice. jjg@284: */ jjg@284: public class LocalVariableTypeTableWriter extends InstructionDetailWriter { jjg@284: public enum NoteKind { jjg@284: START("start") { jjg@284: public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) { jjg@284: return (pc == entry.start_pc); jjg@284: } jjg@284: }, jjg@284: END("end") { jjg@284: public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) { jjg@284: return (pc == entry.start_pc + entry.length); jjg@284: } jjg@284: }; jjg@284: NoteKind(String text) { jjg@284: this.text = text; jjg@284: } jjg@284: public abstract boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc); jjg@284: public final String text; jjg@284: }; jjg@284: jjg@284: static LocalVariableTypeTableWriter instance(Context context) { jjg@284: LocalVariableTypeTableWriter instance = context.get(LocalVariableTypeTableWriter.class); jjg@284: if (instance == null) jjg@284: instance = new LocalVariableTypeTableWriter(context); jjg@284: return instance; jjg@284: } jjg@284: jjg@284: protected LocalVariableTypeTableWriter(Context context) { jjg@284: super(context); jjg@284: context.put(LocalVariableTypeTableWriter.class, this); jjg@284: classWriter = ClassWriter.instance(context); jjg@284: } jjg@284: jjg@284: public void reset(Code_attribute attr) { jjg@284: codeAttr = attr; jjg@284: pcMap = new HashMap>(); jjg@284: LocalVariableTypeTable_attribute lvt = jjg@284: (LocalVariableTypeTable_attribute) (attr.attributes.get(Attribute.LocalVariableTypeTable)); jjg@284: if (lvt == null) jjg@284: return; jjg@284: jjg@284: for (int i = 0; i < lvt.local_variable_table.length; i++) { jjg@284: LocalVariableTypeTable_attribute.Entry entry = lvt.local_variable_table[i]; jjg@284: put(entry.start_pc, entry); jjg@284: put(entry.start_pc + entry.length, entry); jjg@284: } jjg@284: } jjg@284: jjg@284: public void writeDetails(Instruction instr) { jjg@284: int pc = instr.getPC(); jjg@284: writeLocalVariables(pc, NoteKind.END); jjg@284: writeLocalVariables(pc, NoteKind.START); jjg@284: } jjg@284: jjg@284: @Override jjg@284: public void flush() { jjg@284: int pc = codeAttr.code_length; jjg@284: writeLocalVariables(pc, NoteKind.END); jjg@284: } jjg@284: jjg@284: public void writeLocalVariables(int pc, NoteKind kind) { jjg@284: ConstantPool constant_pool = classWriter.getClassFile().constant_pool; jjg@284: String indent = space(2); // get from Options? jjg@284: List entries = pcMap.get(pc); jjg@284: if (entries != null) { jjg@284: for (ListIterator iter = jjg@284: entries.listIterator(kind == NoteKind.END ? entries.size() : 0); jjg@284: kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) { jjg@284: LocalVariableTypeTable_attribute.Entry entry = jjg@284: kind == NoteKind.END ? iter.previous() : iter.next(); jjg@284: if (kind.match(entry, pc)) { jjg@284: print(indent); jjg@284: print(kind.text); jjg@284: print(" generic local "); jjg@284: print(entry.index); jjg@284: print(" // "); jjg@284: Descriptor d = new Signature(entry.signature_index); jjg@284: try { jjg@427: print(d.getFieldType(constant_pool).toString().replace("/", ".")); jjg@284: } catch (InvalidDescriptor e) { jjg@284: print(report(e)); jjg@284: } catch (ConstantPoolException e) { jjg@284: print(report(e)); jjg@284: } jjg@284: print(" "); jjg@284: try { jjg@284: print(constant_pool.getUTF8Value(entry.name_index)); jjg@284: } catch (ConstantPoolException e) { jjg@284: print(report(e)); jjg@284: } jjg@284: println(); jjg@284: } jjg@284: } jjg@284: } jjg@284: } jjg@284: jjg@284: private void put(int pc, LocalVariableTypeTable_attribute.Entry entry) { jjg@284: List list = pcMap.get(pc); jjg@284: if (list == null) { jjg@284: list = new ArrayList(); jjg@284: pcMap.put(pc, list); jjg@284: } jjg@284: if (!list.contains(entry)) jjg@284: list.add(entry); jjg@284: } jjg@284: jjg@284: private ClassWriter classWriter; jjg@284: private Code_attribute codeAttr; jjg@284: private Map> pcMap; jjg@284: }