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

jjg@283 1 /*
ohair@798 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
jjg@283 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@283 4 *
jjg@283 5 * This code is free software; you can redistribute it and/or modify it
jjg@283 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@283 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@283 10 *
jjg@283 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@283 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@283 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@283 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@283 15 * accompanied this code).
jjg@283 16 *
jjg@283 17 * You should have received a copy of the GNU General Public License version
jjg@283 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@283 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@283 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@283 24 */
jjg@283 25
jjg@283 26 package com.sun.tools.javap;
jjg@283 27
jjg@283 28 import java.io.BufferedReader;
jjg@283 29 import java.io.IOException;
jjg@283 30 import java.io.StringReader;
jjg@283 31 import java.util.ArrayList;
jjg@283 32 import java.util.List;
jjg@283 33 import java.util.Set;
jjg@283 34 import java.util.SortedMap;
jjg@283 35 import java.util.SortedSet;
jjg@283 36 import java.util.TreeMap;
jjg@283 37 import java.util.TreeSet;
jjg@283 38 import javax.tools.JavaFileManager;
jjg@283 39 import javax.tools.JavaFileManager.Location;
jjg@283 40 import javax.tools.JavaFileObject;
jjg@283 41 import javax.tools.StandardLocation;
jjg@283 42
jjg@283 43 import com.sun.tools.classfile.Attribute;
jjg@283 44 import com.sun.tools.classfile.ClassFile;
jjg@283 45 import com.sun.tools.classfile.Code_attribute;
jjg@283 46 import com.sun.tools.classfile.ConstantPoolException;
jjg@283 47 import com.sun.tools.classfile.Instruction;
jjg@283 48 import com.sun.tools.classfile.LineNumberTable_attribute;
jjg@283 49 import com.sun.tools.classfile.SourceFile_attribute;
jjg@283 50
jjg@283 51
jjg@283 52 /**
jjg@283 53 * Annotate instructions with source code.
jjg@283 54 *
jjg@581 55 * <p><b>This is NOT part of any supported API.
jjg@581 56 * If you write code that depends on this, you do so at your own risk.
jjg@283 57 * This code and its internal interfaces are subject to change or
jjg@283 58 * deletion without notice.</b>
jjg@283 59 */
jjg@283 60 public class SourceWriter extends InstructionDetailWriter {
jjg@283 61 static SourceWriter instance(Context context) {
jjg@283 62 SourceWriter instance = context.get(SourceWriter.class);
jjg@283 63 if (instance == null)
jjg@283 64 instance = new SourceWriter(context);
jjg@283 65 return instance;
jjg@283 66 }
jjg@283 67
jjg@283 68 protected SourceWriter(Context context) {
jjg@283 69 super(context);
jjg@283 70 context.put(SourceWriter.class, this);
jjg@283 71 }
jjg@283 72
jjg@283 73 void setFileManager(JavaFileManager fileManager) {
jjg@283 74 this.fileManager = fileManager;
jjg@283 75 }
jjg@283 76
jjg@283 77 public void reset(ClassFile cf, Code_attribute attr) {
jjg@283 78 setSource(cf);
jjg@283 79 setLineMap(attr);
jjg@283 80 }
jjg@283 81
jjg@283 82 public void writeDetails(Instruction instr) {
jjg@283 83 String indent = space(40); // could get from Options?
jjg@283 84 Set<Integer> lines = lineMap.get(instr.getPC());
jjg@283 85 if (lines != null) {
jjg@283 86 for (int line: lines) {
jjg@283 87 print(indent);
jjg@283 88 print(String.format(" %4d ", line));
jjg@283 89 if (line < sourceLines.length)
jjg@283 90 print(sourceLines[line]);
jjg@283 91 println();
jjg@283 92 int nextLine = nextLine(line);
jjg@283 93 for (int i = line + 1; i < nextLine; i++) {
jjg@283 94 print(indent);
jjg@283 95 print(String.format("(%4d)", i));
jjg@283 96 if (i < sourceLines.length)
jjg@283 97 print(sourceLines[i]);
jjg@283 98 println();
jjg@283 99 }
jjg@283 100 }
jjg@283 101 }
jjg@660 102 }
jjg@283 103
jjg@660 104 public boolean hasSource() {
jjg@660 105 return (sourceLines.length > 0);
jjg@283 106 }
jjg@283 107
jjg@283 108 private void setLineMap(Code_attribute attr) {
jjg@283 109 SortedMap<Integer, SortedSet<Integer>> map =
jjg@283 110 new TreeMap<Integer, SortedSet<Integer>>();
jjg@283 111 SortedSet<Integer> allLines = new TreeSet<Integer>();
jjg@283 112 for (Attribute a: attr.attributes) {
jjg@283 113 if (a instanceof LineNumberTable_attribute) {
jjg@283 114 LineNumberTable_attribute t = (LineNumberTable_attribute) a;
jjg@283 115 for (LineNumberTable_attribute.Entry e: t.line_number_table) {
jjg@283 116 int start_pc = e.start_pc;
jjg@283 117 int line = e.line_number;
jjg@283 118 SortedSet<Integer> pcLines = map.get(start_pc);
jjg@283 119 if (pcLines == null) {
jjg@283 120 pcLines = new TreeSet<Integer>();
jjg@283 121 map.put(start_pc, pcLines);
jjg@283 122 }
jjg@283 123 pcLines.add(line);
jjg@283 124 allLines.add(line);
jjg@283 125 }
jjg@283 126 }
jjg@283 127 }
jjg@283 128 lineMap = map;
jjg@283 129 lineList = new ArrayList<Integer>(allLines);
jjg@283 130 }
jjg@283 131
jjg@283 132 private void setSource(ClassFile cf) {
jjg@283 133 if (cf != classFile) {
jjg@283 134 classFile = cf;
jjg@283 135 sourceLines = splitLines(readSource(cf));
jjg@283 136 }
jjg@283 137 }
jjg@283 138
jjg@283 139 private String readSource(ClassFile cf) {
jjg@300 140 if (fileManager == null)
jjg@300 141 return null;
jjg@300 142
jjg@283 143 Location location;
jjg@283 144 if (fileManager.hasLocation((StandardLocation.SOURCE_PATH)))
jjg@283 145 location = StandardLocation.SOURCE_PATH;
jjg@283 146 else
jjg@283 147 location = StandardLocation.CLASS_PATH;
jjg@283 148
jjg@283 149 // Guess the source file for a class from the package name for this
jjg@283 150 // class and the base of the source file. This avoids having to read
jjg@283 151 // additional classes to determine the outmost class from any
jjg@283 152 // InnerClasses and EnclosingMethod attributes.
jjg@283 153 try {
jjg@283 154 String className = cf.getName();
jjg@283 155 SourceFile_attribute sf =
jjg@283 156 (SourceFile_attribute) cf.attributes.get(Attribute.SourceFile);
jjg@283 157 if (sf == null) {
jjg@283 158 report(messages.getMessage("err.no.SourceFile.attribute"));
jjg@283 159 return null;
jjg@283 160 }
jjg@283 161 String sourceFile = sf.getSourceFile(cf.constant_pool);
jjg@283 162 String fileBase = sourceFile.endsWith(".java")
jjg@283 163 ? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile;
jjg@283 164 int sep = className.lastIndexOf("/");
jjg@283 165 String pkgName = (sep == -1 ? "" : className.substring(0, sep+1));
jjg@283 166 String topClassName = (pkgName + fileBase).replace('/', '.');
jjg@283 167 JavaFileObject fo =
jjg@283 168 fileManager.getJavaFileForInput(location,
jjg@283 169 topClassName,
jjg@283 170 JavaFileObject.Kind.SOURCE);
jjg@283 171 if (fo == null) {
jjg@283 172 report(messages.getMessage("err.source.file.not.found"));
jjg@283 173 return null;
jjg@283 174 }
jjg@283 175 return fo.getCharContent(true).toString();
jjg@283 176 } catch (ConstantPoolException e) {
jjg@283 177 report(e);
jjg@283 178 return null;
jjg@283 179 } catch (IOException e) {
jjg@283 180 report(e.getLocalizedMessage());
jjg@283 181 return null;
jjg@283 182 }
jjg@283 183 }
jjg@283 184
jjg@283 185 private static String[] splitLines(String text) {
jjg@283 186 if (text == null)
jjg@283 187 return new String[0];
jjg@283 188
jjg@283 189 List<String> lines = new ArrayList<String>();
jjg@283 190 lines.add(""); // dummy line 0
jjg@283 191 try {
jjg@283 192 BufferedReader r = new BufferedReader(new StringReader(text));
jjg@283 193 String line;
jjg@283 194 while ((line = r.readLine()) != null)
jjg@283 195 lines.add(line);
jjg@283 196 } catch (IOException ignore) {
jjg@283 197 }
jjg@283 198 return lines.toArray(new String[lines.size()]);
jjg@283 199 }
jjg@283 200
jjg@283 201 private int nextLine(int line) {
jjg@283 202 int i = lineList.indexOf(line);
jjg@283 203 if (i == -1 || i == lineList.size() - 1)
jjg@283 204 return - 1;
jjg@283 205 return lineList.get(i + 1);
jjg@283 206 }
jjg@283 207
jjg@283 208 private JavaFileManager fileManager;
jjg@283 209 private ClassFile classFile;
jjg@283 210 private SortedMap<Integer, SortedSet<Integer>> lineMap;
jjg@283 211 private List<Integer> lineList;
jjg@283 212 private String[] sourceLines;
jjg@283 213 }

mercurial