Thu, 30 Jul 2009 09:18:55 -0700
4880672: javap does not output inner interfaces of an interface
Reviewed-by: mcimadamore
jjg@283 | 1 | /* |
jjg@283 | 2 | * Copyright 2009 Sun Microsystems, Inc. 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 |
jjg@283 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@283 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@283 | 9 | * by Sun 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 | * |
jjg@283 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@283 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@283 | 23 | * have any 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@283 | 55 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
jjg@283 | 56 | * 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@283 | 102 | |
jjg@283 | 103 | } |
jjg@283 | 104 | |
jjg@283 | 105 | private void setLineMap(Code_attribute attr) { |
jjg@283 | 106 | SortedMap<Integer, SortedSet<Integer>> map = |
jjg@283 | 107 | new TreeMap<Integer, SortedSet<Integer>>(); |
jjg@283 | 108 | SortedSet<Integer> allLines = new TreeSet<Integer>(); |
jjg@283 | 109 | for (Attribute a: attr.attributes) { |
jjg@283 | 110 | if (a instanceof LineNumberTable_attribute) { |
jjg@283 | 111 | LineNumberTable_attribute t = (LineNumberTable_attribute) a; |
jjg@283 | 112 | for (LineNumberTable_attribute.Entry e: t.line_number_table) { |
jjg@283 | 113 | int start_pc = e.start_pc; |
jjg@283 | 114 | int line = e.line_number; |
jjg@283 | 115 | SortedSet<Integer> pcLines = map.get(start_pc); |
jjg@283 | 116 | if (pcLines == null) { |
jjg@283 | 117 | pcLines = new TreeSet<Integer>(); |
jjg@283 | 118 | map.put(start_pc, pcLines); |
jjg@283 | 119 | } |
jjg@283 | 120 | pcLines.add(line); |
jjg@283 | 121 | allLines.add(line); |
jjg@283 | 122 | } |
jjg@283 | 123 | } |
jjg@283 | 124 | } |
jjg@283 | 125 | lineMap = map; |
jjg@283 | 126 | lineList = new ArrayList<Integer>(allLines); |
jjg@283 | 127 | } |
jjg@283 | 128 | |
jjg@283 | 129 | private void setSource(ClassFile cf) { |
jjg@283 | 130 | if (cf != classFile) { |
jjg@283 | 131 | classFile = cf; |
jjg@283 | 132 | sourceLines = splitLines(readSource(cf)); |
jjg@283 | 133 | } |
jjg@283 | 134 | } |
jjg@283 | 135 | |
jjg@283 | 136 | private String readSource(ClassFile cf) { |
jjg@300 | 137 | if (fileManager == null) |
jjg@300 | 138 | return null; |
jjg@300 | 139 | |
jjg@283 | 140 | Location location; |
jjg@283 | 141 | if (fileManager.hasLocation((StandardLocation.SOURCE_PATH))) |
jjg@283 | 142 | location = StandardLocation.SOURCE_PATH; |
jjg@283 | 143 | else |
jjg@283 | 144 | location = StandardLocation.CLASS_PATH; |
jjg@283 | 145 | |
jjg@283 | 146 | // Guess the source file for a class from the package name for this |
jjg@283 | 147 | // class and the base of the source file. This avoids having to read |
jjg@283 | 148 | // additional classes to determine the outmost class from any |
jjg@283 | 149 | // InnerClasses and EnclosingMethod attributes. |
jjg@283 | 150 | try { |
jjg@283 | 151 | String className = cf.getName(); |
jjg@283 | 152 | SourceFile_attribute sf = |
jjg@283 | 153 | (SourceFile_attribute) cf.attributes.get(Attribute.SourceFile); |
jjg@283 | 154 | if (sf == null) { |
jjg@283 | 155 | report(messages.getMessage("err.no.SourceFile.attribute")); |
jjg@283 | 156 | return null; |
jjg@283 | 157 | } |
jjg@283 | 158 | String sourceFile = sf.getSourceFile(cf.constant_pool); |
jjg@283 | 159 | String fileBase = sourceFile.endsWith(".java") |
jjg@283 | 160 | ? sourceFile.substring(0, sourceFile.length() - 5) : sourceFile; |
jjg@283 | 161 | int sep = className.lastIndexOf("/"); |
jjg@283 | 162 | String pkgName = (sep == -1 ? "" : className.substring(0, sep+1)); |
jjg@283 | 163 | String topClassName = (pkgName + fileBase).replace('/', '.'); |
jjg@283 | 164 | JavaFileObject fo = |
jjg@283 | 165 | fileManager.getJavaFileForInput(location, |
jjg@283 | 166 | topClassName, |
jjg@283 | 167 | JavaFileObject.Kind.SOURCE); |
jjg@283 | 168 | if (fo == null) { |
jjg@283 | 169 | report(messages.getMessage("err.source.file.not.found")); |
jjg@283 | 170 | return null; |
jjg@283 | 171 | } |
jjg@283 | 172 | return fo.getCharContent(true).toString(); |
jjg@283 | 173 | } catch (ConstantPoolException e) { |
jjg@283 | 174 | report(e); |
jjg@283 | 175 | return null; |
jjg@283 | 176 | } catch (IOException e) { |
jjg@283 | 177 | report(e.getLocalizedMessage()); |
jjg@283 | 178 | return null; |
jjg@283 | 179 | } |
jjg@283 | 180 | } |
jjg@283 | 181 | |
jjg@283 | 182 | private static String[] splitLines(String text) { |
jjg@283 | 183 | if (text == null) |
jjg@283 | 184 | return new String[0]; |
jjg@283 | 185 | |
jjg@283 | 186 | List<String> lines = new ArrayList<String>(); |
jjg@283 | 187 | lines.add(""); // dummy line 0 |
jjg@283 | 188 | try { |
jjg@283 | 189 | BufferedReader r = new BufferedReader(new StringReader(text)); |
jjg@283 | 190 | String line; |
jjg@283 | 191 | while ((line = r.readLine()) != null) |
jjg@283 | 192 | lines.add(line); |
jjg@283 | 193 | } catch (IOException ignore) { |
jjg@283 | 194 | } |
jjg@283 | 195 | return lines.toArray(new String[lines.size()]); |
jjg@283 | 196 | } |
jjg@283 | 197 | |
jjg@283 | 198 | private int nextLine(int line) { |
jjg@283 | 199 | int i = lineList.indexOf(line); |
jjg@283 | 200 | if (i == -1 || i == lineList.size() - 1) |
jjg@283 | 201 | return - 1; |
jjg@283 | 202 | return lineList.get(i + 1); |
jjg@283 | 203 | } |
jjg@283 | 204 | |
jjg@283 | 205 | private JavaFileManager fileManager; |
jjg@283 | 206 | private ClassFile classFile; |
jjg@283 | 207 | private SortedMap<Integer, SortedSet<Integer>> lineMap; |
jjg@283 | 208 | private List<Integer> lineList; |
jjg@283 | 209 | private String[] sourceLines; |
jjg@283 | 210 | } |