Tue, 11 Aug 2009 01:13:14 +0100
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg
jjg@46 | 1 | /* |
xdono@54 | 2 | * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@46 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@46 | 4 | * |
jjg@46 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@46 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@46 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@46 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@46 | 9 | * by Sun in the LICENSE file that accompanied this code. |
jjg@46 | 10 | * |
jjg@46 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@46 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@46 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@46 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@46 | 15 | * accompanied this code). |
jjg@46 | 16 | * |
jjg@46 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@46 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@46 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@46 | 20 | * |
jjg@46 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@46 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@46 | 23 | * have any questions. |
jjg@46 | 24 | */ |
jjg@46 | 25 | |
jjg@46 | 26 | package com.sun.tools.javap; |
jjg@46 | 27 | |
jjg@283 | 28 | import java.util.ArrayList; |
jjg@283 | 29 | import java.util.List; |
jjg@283 | 30 | |
jjg@46 | 31 | import com.sun.tools.classfile.AccessFlags; |
jjg@46 | 32 | import com.sun.tools.classfile.Code_attribute; |
jjg@46 | 33 | import com.sun.tools.classfile.ConstantPool; |
jjg@46 | 34 | import com.sun.tools.classfile.ConstantPoolException; |
jjg@46 | 35 | import com.sun.tools.classfile.DescriptorException; |
jjg@255 | 36 | import com.sun.tools.classfile.Instruction; |
jjg@255 | 37 | import com.sun.tools.classfile.Instruction.TypeKind; |
jjg@46 | 38 | import com.sun.tools.classfile.Method; |
jjg@46 | 39 | |
jjg@46 | 40 | /* |
jjg@46 | 41 | * Write the contents of a Code attribute. |
jjg@46 | 42 | * |
jjg@46 | 43 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
jjg@46 | 44 | * you write code that depends on this, you do so at your own risk. |
jjg@46 | 45 | * This code and its internal interfaces are subject to change or |
jjg@46 | 46 | * deletion without notice.</b> |
jjg@46 | 47 | */ |
jjg@46 | 48 | class CodeWriter extends BasicWriter { |
jjg@46 | 49 | static CodeWriter instance(Context context) { |
jjg@46 | 50 | CodeWriter instance = context.get(CodeWriter.class); |
jjg@46 | 51 | if (instance == null) |
jjg@46 | 52 | instance = new CodeWriter(context); |
jjg@46 | 53 | return instance; |
jjg@46 | 54 | } |
jjg@46 | 55 | |
jjg@46 | 56 | protected CodeWriter(Context context) { |
jjg@46 | 57 | super(context); |
jjg@46 | 58 | context.put(CodeWriter.class, this); |
jjg@46 | 59 | attrWriter = AttributeWriter.instance(context); |
jjg@46 | 60 | classWriter = ClassWriter.instance(context); |
jjg@46 | 61 | constantWriter = ConstantWriter.instance(context); |
jjg@283 | 62 | sourceWriter = SourceWriter.instance(context); |
jjg@283 | 63 | tryBlockWriter = TryBlockWriter.instance(context); |
jjg@283 | 64 | stackMapWriter = StackMapWriter.instance(context); |
jjg@283 | 65 | localVariableTableWriter = LocalVariableTableWriter.instance(context); |
jjg@283 | 66 | localVariableTypeTableWriter = LocalVariableTypeTableWriter.instance(context); |
jjg@338 | 67 | typeAnnotationWriter = TypeAnnotationWriter.instance(context); |
jjg@283 | 68 | options = Options.instance(context); |
jjg@46 | 69 | } |
jjg@46 | 70 | |
jjg@46 | 71 | void write(Code_attribute attr, ConstantPool constant_pool) { |
jjg@348 | 72 | println("Code:"); |
jjg@348 | 73 | indent(+1); |
jjg@46 | 74 | writeVerboseHeader(attr, constant_pool); |
jjg@46 | 75 | writeInstrs(attr); |
jjg@46 | 76 | writeExceptionTable(attr); |
jjg@46 | 77 | attrWriter.write(attr, attr.attributes, constant_pool); |
jjg@348 | 78 | indent(-1); |
jjg@46 | 79 | } |
jjg@46 | 80 | |
jjg@46 | 81 | public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) { |
jjg@46 | 82 | Method method = classWriter.getMethod(); |
jjg@46 | 83 | String argCount; |
jjg@46 | 84 | try { |
jjg@46 | 85 | int n = method.descriptor.getParameterCount(constant_pool); |
jjg@46 | 86 | if (!method.access_flags.is(AccessFlags.ACC_STATIC)) |
jjg@46 | 87 | ++n; // for 'this' |
jjg@46 | 88 | argCount = Integer.toString(n); |
jjg@46 | 89 | } catch (ConstantPoolException e) { |
jjg@46 | 90 | argCount = report(e); |
jjg@46 | 91 | } catch (DescriptorException e) { |
jjg@46 | 92 | argCount = report(e); |
jjg@46 | 93 | } |
jjg@46 | 94 | |
jjg@348 | 95 | println("stack=" + attr.max_stack + |
jjg@348 | 96 | ", locals=" + attr.max_locals + |
jjg@348 | 97 | ", args_size=" + argCount); |
jjg@46 | 98 | |
jjg@46 | 99 | } |
jjg@46 | 100 | |
jjg@46 | 101 | public void writeInstrs(Code_attribute attr) { |
jjg@283 | 102 | List<InstructionDetailWriter> detailWriters = getDetailWriters(attr); |
jjg@283 | 103 | |
jjg@255 | 104 | for (Instruction instr: attr.getInstructions()) { |
jjg@255 | 105 | try { |
jjg@283 | 106 | for (InstructionDetailWriter w: detailWriters) |
jjg@283 | 107 | w.writeDetails(instr); |
jjg@255 | 108 | writeInstr(instr); |
jjg@255 | 109 | } catch (ArrayIndexOutOfBoundsException e) { |
jjg@255 | 110 | println(report("error at or after byte " + instr.getPC())); |
jjg@255 | 111 | break; |
jjg@46 | 112 | } |
jjg@46 | 113 | } |
jjg@283 | 114 | |
jjg@283 | 115 | for (InstructionDetailWriter w: detailWriters) |
jjg@283 | 116 | w.flush(); |
jjg@46 | 117 | } |
jjg@46 | 118 | |
jjg@255 | 119 | public void writeInstr(Instruction instr) { |
jjg@355 | 120 | print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic())); |
jjg@255 | 121 | instr.accept(instructionPrinter, null); |
jjg@255 | 122 | println(); |
jjg@255 | 123 | } |
jjg@255 | 124 | // where |
jjg@255 | 125 | Instruction.KindVisitor<Void,Void> instructionPrinter = |
jjg@255 | 126 | new Instruction.KindVisitor<Void,Void>() { |
jjg@255 | 127 | |
jjg@255 | 128 | public Void visitNoOperands(Instruction instr, Void p) { |
jjg@255 | 129 | return null; |
jjg@255 | 130 | } |
jjg@255 | 131 | |
jjg@255 | 132 | public Void visitArrayType(Instruction instr, TypeKind kind, Void p) { |
jjg@255 | 133 | print(" " + kind.name); |
jjg@255 | 134 | return null; |
jjg@255 | 135 | } |
jjg@255 | 136 | |
jjg@255 | 137 | public Void visitBranch(Instruction instr, int offset, Void p) { |
jjg@348 | 138 | print((instr.getPC() + offset)); |
jjg@255 | 139 | return null; |
jjg@255 | 140 | } |
jjg@255 | 141 | |
jjg@255 | 142 | public Void visitConstantPoolRef(Instruction instr, int index, Void p) { |
jjg@354 | 143 | print("#" + index); |
jjg@348 | 144 | tab(); |
jjg@348 | 145 | print("// "); |
jjg@255 | 146 | printConstant(index); |
jjg@255 | 147 | return null; |
jjg@255 | 148 | } |
jjg@255 | 149 | |
jjg@255 | 150 | public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) { |
jjg@354 | 151 | print("#" + index + ", " + value); |
jjg@348 | 152 | tab(); |
jjg@348 | 153 | print("// "); |
jjg@255 | 154 | printConstant(index); |
jjg@255 | 155 | return null; |
jjg@255 | 156 | } |
jjg@255 | 157 | |
jjg@255 | 158 | public Void visitLocal(Instruction instr, int index, Void p) { |
jjg@348 | 159 | print(index); |
jjg@255 | 160 | return null; |
jjg@255 | 161 | } |
jjg@255 | 162 | |
jjg@255 | 163 | public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) { |
jjg@348 | 164 | print(index + ", " + value); |
jjg@255 | 165 | return null; |
jjg@255 | 166 | } |
jjg@255 | 167 | |
jjg@255 | 168 | public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets) { |
jjg@255 | 169 | int pc = instr.getPC(); |
jjg@348 | 170 | print("{ // " + npairs); |
jjg@348 | 171 | indent(+1); |
jjg@255 | 172 | for (int i = 0; i < npairs; i++) { |
jjg@354 | 173 | print("\n" + matches[i] + ": " + (pc + offsets[i])); |
jjg@46 | 174 | } |
jjg@348 | 175 | print("\ndefault: " + (pc + default_) + " }"); |
jjg@348 | 176 | indent(-1); |
jjg@255 | 177 | return null; |
jjg@255 | 178 | } |
jjg@255 | 179 | |
jjg@255 | 180 | public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets) { |
jjg@255 | 181 | int pc = instr.getPC(); |
jjg@255 | 182 | print("{ //" + low + " to " + high); |
jjg@348 | 183 | indent(+1); |
jjg@255 | 184 | for (int i = 0; i < offsets.length; i++) { |
jjg@354 | 185 | print("\n" + (low + i) + ": " + (pc + offsets[i])); |
jjg@46 | 186 | } |
jjg@348 | 187 | print("\ndefault: " + (pc + default_) + " }"); |
jjg@348 | 188 | indent(-1); |
jjg@255 | 189 | return null; |
jjg@46 | 190 | } |
jjg@255 | 191 | |
jjg@255 | 192 | public Void visitValue(Instruction instr, int value, Void p) { |
jjg@348 | 193 | print(value); |
jjg@255 | 194 | return null; |
jjg@46 | 195 | } |
jjg@255 | 196 | |
jjg@255 | 197 | public Void visitUnknown(Instruction instr, Void p) { |
jjg@255 | 198 | return null; |
jjg@46 | 199 | } |
jjg@255 | 200 | }; |
jjg@255 | 201 | |
jjg@46 | 202 | |
jjg@46 | 203 | public void writeExceptionTable(Code_attribute attr) { |
jjg@46 | 204 | if (attr.exception_table_langth > 0) { |
jjg@348 | 205 | println("Exception table:"); |
jjg@348 | 206 | indent(+1); |
jjg@348 | 207 | println(" from to target type"); |
jjg@46 | 208 | for (int i = 0; i < attr.exception_table.length; i++) { |
jjg@46 | 209 | Code_attribute.Exception_data handler = attr.exception_table[i]; |
jjg@348 | 210 | print(String.format(" %5d %5d %5d", |
jjg@348 | 211 | handler.start_pc, handler.end_pc, handler.handler_pc)); |
jjg@46 | 212 | print(" "); |
jjg@46 | 213 | int catch_type = handler.catch_type; |
jjg@46 | 214 | if (catch_type == 0) { |
jjg@46 | 215 | println("any"); |
jjg@46 | 216 | } else { |
jjg@46 | 217 | print("Class "); |
jjg@46 | 218 | println(constantWriter.stringValue(catch_type)); |
jjg@46 | 219 | } |
jjg@46 | 220 | } |
jjg@348 | 221 | indent(-1); |
jjg@46 | 222 | } |
jjg@46 | 223 | |
jjg@46 | 224 | } |
jjg@46 | 225 | |
jjg@46 | 226 | private void printConstant(int index) { |
jjg@46 | 227 | constantWriter.write(index); |
jjg@46 | 228 | } |
jjg@46 | 229 | |
jjg@283 | 230 | private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) { |
jjg@283 | 231 | List<InstructionDetailWriter> detailWriters = |
jjg@283 | 232 | new ArrayList<InstructionDetailWriter>(); |
jjg@283 | 233 | if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) { |
jjg@283 | 234 | sourceWriter.reset(classWriter.getClassFile(), attr); |
jjg@283 | 235 | detailWriters.add(sourceWriter); |
jjg@283 | 236 | } |
jjg@283 | 237 | |
jjg@283 | 238 | if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) { |
jjg@283 | 239 | localVariableTableWriter.reset(attr); |
jjg@283 | 240 | detailWriters.add(localVariableTableWriter); |
jjg@283 | 241 | } |
jjg@283 | 242 | |
jjg@283 | 243 | if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VAR_TYPES)) { |
jjg@283 | 244 | localVariableTypeTableWriter.reset(attr); |
jjg@283 | 245 | detailWriters.add(localVariableTypeTableWriter); |
jjg@283 | 246 | } |
jjg@283 | 247 | |
jjg@283 | 248 | if (options.details.contains(InstructionDetailWriter.Kind.STACKMAPS)) { |
jjg@283 | 249 | stackMapWriter.reset(attr); |
jjg@283 | 250 | stackMapWriter.writeInitialDetails(); |
jjg@283 | 251 | detailWriters.add(stackMapWriter); |
jjg@283 | 252 | } |
jjg@283 | 253 | |
jjg@283 | 254 | if (options.details.contains(InstructionDetailWriter.Kind.TRY_BLOCKS)) { |
jjg@283 | 255 | tryBlockWriter.reset(attr); |
jjg@283 | 256 | detailWriters.add(tryBlockWriter); |
jjg@283 | 257 | } |
jjg@283 | 258 | |
jjg@338 | 259 | if (options.details.contains(InstructionDetailWriter.Kind.TYPE_ANNOS)) { |
jjg@338 | 260 | typeAnnotationWriter.reset(attr); |
jjg@338 | 261 | detailWriters.add(typeAnnotationWriter); |
jjg@338 | 262 | } |
jjg@338 | 263 | |
jjg@283 | 264 | return detailWriters; |
jjg@46 | 265 | } |
jjg@46 | 266 | |
jjg@46 | 267 | private AttributeWriter attrWriter; |
jjg@46 | 268 | private ClassWriter classWriter; |
jjg@46 | 269 | private ConstantWriter constantWriter; |
jjg@283 | 270 | private LocalVariableTableWriter localVariableTableWriter; |
jjg@283 | 271 | private LocalVariableTypeTableWriter localVariableTypeTableWriter; |
jjg@338 | 272 | private TypeAnnotationWriter typeAnnotationWriter; |
jjg@283 | 273 | private SourceWriter sourceWriter; |
jjg@283 | 274 | private StackMapWriter stackMapWriter; |
jjg@283 | 275 | private TryBlockWriter tryBlockWriter; |
jjg@283 | 276 | private Options options; |
jjg@46 | 277 | } |