src/share/classes/com/sun/tools/javap/LocalVariableTypeTableWriter.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2009, 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  */
    26 package com.sun.tools.javap;
    28 import com.sun.tools.classfile.Attribute;
    29 import com.sun.tools.classfile.Code_attribute;
    30 import com.sun.tools.classfile.ConstantPool;
    31 import com.sun.tools.classfile.ConstantPoolException;
    32 import com.sun.tools.classfile.Descriptor;
    33 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
    34 import com.sun.tools.classfile.Instruction;
    35 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
    36 import com.sun.tools.classfile.Signature;
    37 import java.util.ArrayList;
    38 import java.util.HashMap;
    39 import java.util.List;
    40 import java.util.ListIterator;
    41 import java.util.Map;
    43 /**
    44  * Annotate instructions with details about local variables.
    45  *
    46  *  <p><b>This is NOT part of any supported API.
    47  *  If you write code that depends on this, you do so at your own risk.
    48  *  This code and its internal interfaces are subject to change or
    49  *  deletion without notice.</b>
    50  */
    51 public class LocalVariableTypeTableWriter extends  InstructionDetailWriter {
    52     public enum NoteKind {
    53         START("start") {
    54             public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) {
    55                 return (pc == entry.start_pc);
    56             }
    57         },
    58         END("end") {
    59             public boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc) {
    60                 return (pc == entry.start_pc + entry.length);
    61             }
    62         };
    63         NoteKind(String text) {
    64             this.text = text;
    65         }
    66         public abstract boolean match(LocalVariableTypeTable_attribute.Entry entry, int pc);
    67         public final String text;
    68     };
    70     static LocalVariableTypeTableWriter instance(Context context) {
    71         LocalVariableTypeTableWriter instance = context.get(LocalVariableTypeTableWriter.class);
    72         if (instance == null)
    73             instance = new LocalVariableTypeTableWriter(context);
    74         return instance;
    75     }
    77     protected LocalVariableTypeTableWriter(Context context) {
    78         super(context);
    79         context.put(LocalVariableTypeTableWriter.class, this);
    80         classWriter = ClassWriter.instance(context);
    81     }
    83     public void reset(Code_attribute attr) {
    84         codeAttr = attr;
    85         pcMap = new HashMap<Integer, List<LocalVariableTypeTable_attribute.Entry>>();
    86         LocalVariableTypeTable_attribute lvt =
    87                 (LocalVariableTypeTable_attribute) (attr.attributes.get(Attribute.LocalVariableTypeTable));
    88         if (lvt == null)
    89             return;
    91         for (int i = 0; i < lvt.local_variable_table.length; i++) {
    92             LocalVariableTypeTable_attribute.Entry entry = lvt.local_variable_table[i];
    93             put(entry.start_pc, entry);
    94             put(entry.start_pc + entry.length, entry);
    95         }
    96     }
    98     public void writeDetails(Instruction instr) {
    99         int pc = instr.getPC();
   100         writeLocalVariables(pc, NoteKind.END);
   101         writeLocalVariables(pc, NoteKind.START);
   102     }
   104     @Override
   105     public void flush() {
   106         int pc = codeAttr.code_length;
   107         writeLocalVariables(pc, NoteKind.END);
   108     }
   110     public void writeLocalVariables(int pc, NoteKind kind) {
   111         ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
   112         String indent = space(2); // get from Options?
   113         List<LocalVariableTypeTable_attribute.Entry> entries = pcMap.get(pc);
   114         if (entries != null) {
   115             for (ListIterator<LocalVariableTypeTable_attribute.Entry> iter =
   116                     entries.listIterator(kind == NoteKind.END ? entries.size() : 0);
   117                     kind == NoteKind.END ? iter.hasPrevious() : iter.hasNext() ; ) {
   118                 LocalVariableTypeTable_attribute.Entry entry =
   119                         kind == NoteKind.END ? iter.previous() : iter.next();
   120                 if (kind.match(entry, pc)) {
   121                     print(indent);
   122                     print(kind.text);
   123                     print(" generic local ");
   124                     print(entry.index);
   125                     print(" // ");
   126                     Descriptor d = new Signature(entry.signature_index);
   127                     try {
   128                         print(d.getFieldType(constant_pool).toString().replace("/", "."));
   129                     } catch (InvalidDescriptor e) {
   130                         print(report(e));
   131                     } catch (ConstantPoolException e) {
   132                         print(report(e));
   133                     }
   134                     print(" ");
   135                     try {
   136                         print(constant_pool.getUTF8Value(entry.name_index));
   137                     } catch (ConstantPoolException e) {
   138                         print(report(e));
   139                     }
   140                     println();
   141                 }
   142             }
   143         }
   144     }
   146     private void put(int pc, LocalVariableTypeTable_attribute.Entry entry) {
   147         List<LocalVariableTypeTable_attribute.Entry> list = pcMap.get(pc);
   148         if (list == null) {
   149             list = new ArrayList<LocalVariableTypeTable_attribute.Entry>();
   150             pcMap.put(pc, list);
   151         }
   152         if (!list.contains(entry))
   153             list.add(entry);
   154     }
   156     private ClassWriter classWriter;
   157     private Code_attribute codeAttr;
   158     private Map<Integer, List<LocalVariableTypeTable_attribute.Entry>> pcMap;
   159 }

mercurial