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

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

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

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2009, 2010, 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 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 supported API.
    56  *  If 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         }
   102     }
   104     public boolean hasSource() {
   105         return (sourceLines.length > 0);
   106     }
   108     private void setLineMap(Code_attribute attr) {
   109         SortedMap<Integer, SortedSet<Integer>> map =
   110                 new TreeMap<Integer, SortedSet<Integer>>();
   111         SortedSet<Integer> allLines = new TreeSet<Integer>();
   112         for (Attribute a: attr.attributes) {
   113             if (a instanceof LineNumberTable_attribute) {
   114                 LineNumberTable_attribute t = (LineNumberTable_attribute) a;
   115                 for (LineNumberTable_attribute.Entry e: t.line_number_table) {
   116                     int start_pc = e.start_pc;
   117                     int line = e.line_number;
   118                     SortedSet<Integer> pcLines = map.get(start_pc);
   119                     if (pcLines == null) {
   120                         pcLines = new TreeSet<Integer>();
   121                         map.put(start_pc, pcLines);
   122                     }
   123                     pcLines.add(line);
   124                     allLines.add(line);
   125                 }
   126             }
   127         }
   128         lineMap = map;
   129         lineList = new ArrayList<Integer>(allLines);
   130     }
   132     private void setSource(ClassFile cf) {
   133         if (cf != classFile) {
   134             classFile = cf;
   135             sourceLines = splitLines(readSource(cf));
   136         }
   137     }
   139     private String readSource(ClassFile cf) {
   140         if (fileManager == null)
   141             return null;
   143         Location location;
   144         if (fileManager.hasLocation((StandardLocation.SOURCE_PATH)))
   145             location = StandardLocation.SOURCE_PATH;
   146         else
   147             location = StandardLocation.CLASS_PATH;
   149         // Guess the source file for a class from the package name for this
   150         // class and the base of the source file. This avoids having to read
   151         // additional classes to determine the outmost class from any
   152         // InnerClasses and EnclosingMethod attributes.
   153         try {
   154             String className = cf.getName();
   155             SourceFile_attribute sf =
   156                     (SourceFile_attribute) cf.attributes.get(Attribute.SourceFile);
   157             if (sf == null) {
   158                 report(messages.getMessage("err.no.SourceFile.attribute"));
   159                 return null;
   160             }
   161             String sourceFile = sf.getSourceFile(cf.constant_pool);
   162             String fileBase = sourceFile.endsWith(".java")
   163                 ? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile;
   164             int sep = className.lastIndexOf("/");
   165             String pkgName = (sep == -1 ? "" : className.substring(0, sep+1));
   166             String topClassName = (pkgName + fileBase).replace('/', '.');
   167             JavaFileObject fo =
   168                     fileManager.getJavaFileForInput(location,
   169                     topClassName,
   170                     JavaFileObject.Kind.SOURCE);
   171             if (fo == null) {
   172                 report(messages.getMessage("err.source.file.not.found"));
   173                 return null;
   174             }
   175             return fo.getCharContent(true).toString();
   176         } catch (ConstantPoolException e) {
   177             report(e);
   178             return null;
   179         } catch (IOException e) {
   180             report(e.getLocalizedMessage());
   181             return null;
   182         }
   183     }
   185     private static String[] splitLines(String text) {
   186         if (text == null)
   187             return new String[0];
   189         List<String> lines = new ArrayList<String>();
   190         lines.add(""); // dummy line 0
   191         try {
   192             BufferedReader r = new BufferedReader(new StringReader(text));
   193             String line;
   194             while ((line = r.readLine()) != null)
   195                 lines.add(line);
   196         } catch (IOException ignore) {
   197         }
   198         return lines.toArray(new String[lines.size()]);
   199     }
   201     private int nextLine(int line) {
   202         int i = lineList.indexOf(line);
   203         if (i == -1 || i == lineList.size() - 1)
   204             return - 1;
   205         return lineList.get(i + 1);
   206     }
   208     private JavaFileManager fileManager;
   209     private ClassFile classFile;
   210     private SortedMap<Integer, SortedSet<Integer>> lineMap;
   211     private List<Integer> lineList;
   212     private String[] sourceLines;
   213 }

mercurial