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

Tue, 19 May 2009 11:50:54 -0700

author
jjg
date
Tue, 19 May 2009 11:50:54 -0700
changeset 283
cd0630109de5
child 300
ed989c347b3c
permissions
-rw-r--r--

6824493: experimental support for additional info for instructions
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright 2009 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javap;
    28 import java.io.BufferedReader;
    29 import java.io.IOException;
    30 import java.io.StringReader;
    31 import java.util.ArrayList;
    32 import java.util.List;
    33 import java.util.Set;
    34 import java.util.SortedMap;
    35 import java.util.SortedSet;
    36 import java.util.TreeMap;
    37 import java.util.TreeSet;
    38 import javax.tools.JavaFileManager;
    39 import javax.tools.JavaFileManager.Location;
    40 import javax.tools.JavaFileObject;
    41 import javax.tools.StandardLocation;
    43 import com.sun.tools.classfile.Attribute;
    44 import com.sun.tools.classfile.ClassFile;
    45 import com.sun.tools.classfile.Code_attribute;
    46 import com.sun.tools.classfile.ConstantPoolException;
    47 import com.sun.tools.classfile.Instruction;
    48 import com.sun.tools.classfile.LineNumberTable_attribute;
    49 import com.sun.tools.classfile.SourceFile_attribute;
    52 /**
    53  * Annotate instructions with source code.
    54  *
    55  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    56  *  you write code that depends on this, you do so at your own risk.
    57  *  This code and its internal interfaces are subject to change or
    58  *  deletion without notice.</b>
    59  */
    60 public class SourceWriter extends InstructionDetailWriter {
    61     static SourceWriter instance(Context context) {
    62         SourceWriter instance = context.get(SourceWriter.class);
    63         if (instance == null)
    64             instance = new SourceWriter(context);
    65         return instance;
    66     }
    68     protected SourceWriter(Context context) {
    69         super(context);
    70         context.put(SourceWriter.class, this);
    71     }
    73     void setFileManager(JavaFileManager fileManager) {
    74         this.fileManager = fileManager;
    75     }
    77     public void reset(ClassFile cf, Code_attribute attr) {
    78         setSource(cf);
    79         setLineMap(attr);
    80     }
    82     public void writeDetails(Instruction instr) {
    83         String indent = space(40); // could get from Options?
    84         Set<Integer> lines = lineMap.get(instr.getPC());
    85         if (lines != null) {
    86             for (int line: lines) {
    87                 print(indent);
    88                 print(String.format(" %4d ", line));
    89                 if (line < sourceLines.length)
    90                     print(sourceLines[line]);
    91                 println();
    92                 int nextLine = nextLine(line);
    93                 for (int i = line + 1; i < nextLine; i++) {
    94                     print(indent);
    95                     print(String.format("(%4d)", i));
    96                     if (i < sourceLines.length)
    97                         print(sourceLines[i]);
    98                     println();
    99                 }
   100             }
   101         }
   103     }
   105     private void setLineMap(Code_attribute attr) {
   106         SortedMap<Integer, SortedSet<Integer>> map =
   107                 new TreeMap<Integer, SortedSet<Integer>>();
   108         SortedSet<Integer> allLines = new TreeSet<Integer>();
   109         for (Attribute a: attr.attributes) {
   110             if (a instanceof LineNumberTable_attribute) {
   111                 LineNumberTable_attribute t = (LineNumberTable_attribute) a;
   112                 for (LineNumberTable_attribute.Entry e: t.line_number_table) {
   113                     int start_pc = e.start_pc;
   114                     int line = e.line_number;
   115                     SortedSet<Integer> pcLines = map.get(start_pc);
   116                     if (pcLines == null) {
   117                         pcLines = new TreeSet<Integer>();
   118                         map.put(start_pc, pcLines);
   119                     }
   120                     pcLines.add(line);
   121                     allLines.add(line);
   122                 }
   123             }
   124         }
   125         lineMap = map;
   126         lineList = new ArrayList<Integer>(allLines);
   127     }
   129     private void setSource(ClassFile cf) {
   130         if (cf != classFile) {
   131             classFile = cf;
   132             sourceLines = splitLines(readSource(cf));
   133         }
   134     }
   136     private String readSource(ClassFile cf) {
   137         Location location;
   138         if (fileManager.hasLocation((StandardLocation.SOURCE_PATH)))
   139             location = StandardLocation.SOURCE_PATH;
   140         else
   141             location = StandardLocation.CLASS_PATH;
   143         // Guess the source file for a class from the package name for this
   144         // class and the base of the source file. This avoids having to read
   145         // additional classes to determine the outmost class from any
   146         // InnerClasses and EnclosingMethod attributes.
   147         try {
   148             String className = cf.getName();
   149             SourceFile_attribute sf =
   150                     (SourceFile_attribute) cf.attributes.get(Attribute.SourceFile);
   151             if (sf == null) {
   152                 report(messages.getMessage("err.no.SourceFile.attribute"));
   153                 return null;
   154             }
   155             String sourceFile = sf.getSourceFile(cf.constant_pool);
   156             String fileBase = sourceFile.endsWith(".java")
   157                 ? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile;
   158             int sep = className.lastIndexOf("/");
   159             String pkgName = (sep == -1 ? "" : className.substring(0, sep+1));
   160             String topClassName = (pkgName + fileBase).replace('/', '.');
   161             JavaFileObject fo =
   162                     fileManager.getJavaFileForInput(location,
   163                     topClassName,
   164                     JavaFileObject.Kind.SOURCE);
   165             if (fo == null) {
   166                 report(messages.getMessage("err.source.file.not.found"));
   167                 return null;
   168             }
   169             return fo.getCharContent(true).toString();
   170         } catch (ConstantPoolException e) {
   171             report(e);
   172             return null;
   173         } catch (IOException e) {
   174             report(e.getLocalizedMessage());
   175             return null;
   176         }
   177     }
   179     private static String[] splitLines(String text) {
   180         if (text == null)
   181             return new String[0];
   183         List<String> lines = new ArrayList<String>();
   184         lines.add(""); // dummy line 0
   185         try {
   186             BufferedReader r = new BufferedReader(new StringReader(text));
   187             String line;
   188             while ((line = r.readLine()) != null)
   189                 lines.add(line);
   190         } catch (IOException ignore) {
   191         }
   192         return lines.toArray(new String[lines.size()]);
   193     }
   195     private int nextLine(int line) {
   196         int i = lineList.indexOf(line);
   197         if (i == -1 || i == lineList.size() - 1)
   198             return - 1;
   199         return lineList.get(i + 1);
   200     }
   202     private JavaFileManager fileManager;
   203     private ClassFile classFile;
   204     private SortedMap<Integer, SortedSet<Integer>> lineMap;
   205     private List<Integer> lineList;
   206     private String[] sourceLines;
   207 }

mercurial