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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 798
4868a36f6fd8
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial