Wed, 17 Nov 2010 15:07:43 -0800
7000973: isBogus needs to be called on the to-be-returned entry, not on the current entry
Reviewed-by: jjg
Contributed-by: jan.lahoda@oracle.com
jjg@46 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2007, 2008, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@46 | 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@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 | * |
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@46 | 24 | */ |
jjg@46 | 25 | |
jjg@46 | 26 | package com.sun.tools.javap; |
jjg@46 | 27 | |
jjg@46 | 28 | import com.sun.tools.classfile.Annotation; |
jjg@308 | 29 | import com.sun.tools.classfile.ExtendedAnnotation; |
jjg@46 | 30 | import com.sun.tools.classfile.Annotation.Annotation_element_value; |
jjg@46 | 31 | import com.sun.tools.classfile.Annotation.Array_element_value; |
jjg@46 | 32 | import com.sun.tools.classfile.Annotation.Class_element_value; |
jjg@46 | 33 | import com.sun.tools.classfile.Annotation.Enum_element_value; |
jjg@46 | 34 | import com.sun.tools.classfile.Annotation.Primitive_element_value; |
jjg@338 | 35 | import com.sun.tools.classfile.ConstantPool; |
jjg@338 | 36 | import com.sun.tools.classfile.ConstantPoolException; |
jjg@338 | 37 | import com.sun.tools.classfile.Descriptor; |
jjg@338 | 38 | import com.sun.tools.classfile.Descriptor.InvalidDescriptor; |
jjg@46 | 39 | |
jjg@46 | 40 | /** |
jjg@46 | 41 | * A writer for writing annotations as text. |
jjg@46 | 42 | * |
jjg@581 | 43 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 44 | * If 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 | public class AnnotationWriter extends BasicWriter { |
jjg@46 | 49 | static AnnotationWriter instance(Context context) { |
jjg@46 | 50 | AnnotationWriter instance = context.get(AnnotationWriter.class); |
jjg@46 | 51 | if (instance == null) |
jjg@46 | 52 | instance = new AnnotationWriter(context); |
jjg@46 | 53 | return instance; |
jjg@46 | 54 | } |
jjg@46 | 55 | |
jjg@46 | 56 | protected AnnotationWriter(Context context) { |
jjg@46 | 57 | super(context); |
jjg@338 | 58 | classWriter = ClassWriter.instance(context); |
jjg@338 | 59 | constantWriter = ConstantWriter.instance(context); |
jjg@46 | 60 | } |
jjg@46 | 61 | |
jjg@46 | 62 | public void write(Annotation annot) { |
jjg@338 | 63 | write(annot, false); |
jjg@338 | 64 | } |
jjg@338 | 65 | |
jjg@338 | 66 | public void write(Annotation annot, boolean resolveIndices) { |
jjg@338 | 67 | writeDescriptor(annot.type_index, resolveIndices); |
jjg@338 | 68 | boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices; |
jjg@338 | 69 | if (showParens) |
jjg@338 | 70 | print("("); |
jjg@46 | 71 | for (int i = 0; i < annot.num_element_value_pairs; i++) { |
jjg@46 | 72 | if (i > 0) |
jjg@46 | 73 | print(","); |
jjg@338 | 74 | write(annot.element_value_pairs[i], resolveIndices); |
jjg@46 | 75 | } |
jjg@338 | 76 | if (showParens) |
jjg@338 | 77 | print(")"); |
jjg@46 | 78 | } |
jjg@46 | 79 | |
jjg@308 | 80 | public void write(ExtendedAnnotation annot) { |
jjg@338 | 81 | write(annot, true, false); |
jjg@338 | 82 | } |
jjg@338 | 83 | |
jjg@338 | 84 | public void write(ExtendedAnnotation annot, boolean showOffsets, boolean resolveIndices) { |
jjg@338 | 85 | write(annot.annotation, resolveIndices); |
jjg@338 | 86 | print(": "); |
jjg@338 | 87 | write(annot.position, showOffsets); |
jjg@338 | 88 | } |
jjg@338 | 89 | |
jjg@338 | 90 | public void write(ExtendedAnnotation.Position pos, boolean showOffsets) { |
jjg@338 | 91 | print(pos.type); |
jjg@338 | 92 | |
jjg@338 | 93 | switch (pos.type) { |
jjg@338 | 94 | // type case |
jjg@338 | 95 | case TYPECAST: |
jjg@338 | 96 | case TYPECAST_GENERIC_OR_ARRAY: |
jjg@338 | 97 | // object creation |
jjg@338 | 98 | case INSTANCEOF: |
jjg@338 | 99 | case INSTANCEOF_GENERIC_OR_ARRAY: |
jjg@338 | 100 | // new expression |
jjg@338 | 101 | case NEW: |
jjg@338 | 102 | case NEW_GENERIC_OR_ARRAY: |
jjg@338 | 103 | case NEW_TYPE_ARGUMENT: |
jjg@338 | 104 | case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY: |
jjg@338 | 105 | if (showOffsets) { |
jjg@338 | 106 | print(", offset="); |
jjg@338 | 107 | print(pos.offset); |
jjg@338 | 108 | } |
jjg@338 | 109 | break; |
jjg@338 | 110 | // local variable |
jjg@338 | 111 | case LOCAL_VARIABLE: |
jjg@338 | 112 | case LOCAL_VARIABLE_GENERIC_OR_ARRAY: |
jjg@338 | 113 | print(", {"); |
jjg@338 | 114 | for (int i = 0; i < pos.lvarOffset.length; ++i) { |
jjg@338 | 115 | if (i != 0) print("; "); |
jjg@338 | 116 | if (showOffsets) { |
jjg@338 | 117 | print(", start_pc="); |
jjg@338 | 118 | print(pos.lvarOffset[i]); |
jjg@338 | 119 | } |
jjg@338 | 120 | print(", length="); |
jjg@338 | 121 | print(pos.lvarLength[i]); |
jjg@338 | 122 | print(", index="); |
jjg@338 | 123 | print(pos.lvarIndex[i]); |
jjg@338 | 124 | } |
jjg@338 | 125 | print("}"); |
jjg@338 | 126 | break; |
jjg@338 | 127 | // method receiver |
jjg@338 | 128 | case METHOD_RECEIVER: |
jjg@338 | 129 | // Do nothing |
jjg@338 | 130 | break; |
jjg@338 | 131 | // type parameters |
jjg@338 | 132 | case CLASS_TYPE_PARAMETER: |
jjg@338 | 133 | case METHOD_TYPE_PARAMETER: |
jjg@338 | 134 | print(", param_index="); |
jjg@338 | 135 | print(pos.parameter_index); |
jjg@338 | 136 | break; |
jjg@338 | 137 | // type parameters bound |
jjg@338 | 138 | case CLASS_TYPE_PARAMETER_BOUND: |
jjg@338 | 139 | case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY: |
jjg@338 | 140 | case METHOD_TYPE_PARAMETER_BOUND: |
jjg@338 | 141 | case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY: |
jjg@338 | 142 | print(", param_index="); |
jjg@338 | 143 | print(pos.parameter_index); |
jjg@338 | 144 | print(", bound_index="); |
jjg@338 | 145 | print(pos.bound_index); |
jjg@338 | 146 | break; |
jjg@338 | 147 | // wildcard |
jjg@338 | 148 | case WILDCARD_BOUND: |
jjg@338 | 149 | case WILDCARD_BOUND_GENERIC_OR_ARRAY: |
jjg@338 | 150 | print(", wild_card="); |
jjg@338 | 151 | print(pos.wildcard_position); |
jjg@338 | 152 | break; |
jjg@338 | 153 | // Class extends and implements clauses |
jjg@338 | 154 | case CLASS_EXTENDS: |
jjg@338 | 155 | case CLASS_EXTENDS_GENERIC_OR_ARRAY: |
jjg@338 | 156 | print(", type_index="); |
jjg@338 | 157 | print(pos.type_index); |
jjg@338 | 158 | break; |
jjg@338 | 159 | // throws |
jjg@338 | 160 | case THROWS: |
jjg@338 | 161 | print(", type_index="); |
jjg@338 | 162 | print(pos.type_index); |
jjg@338 | 163 | break; |
jjg@338 | 164 | case CLASS_LITERAL: |
jjg@485 | 165 | case CLASS_LITERAL_GENERIC_OR_ARRAY: |
jjg@338 | 166 | if (showOffsets) { |
jjg@338 | 167 | print(", offset="); |
jjg@338 | 168 | print(pos.offset); |
jjg@338 | 169 | } |
jjg@338 | 170 | break; |
jjg@338 | 171 | // method parameter: not specified |
jjg@338 | 172 | case METHOD_PARAMETER_GENERIC_OR_ARRAY: |
jjg@338 | 173 | print(", param_index="); |
jjg@338 | 174 | print(pos.parameter_index); |
jjg@338 | 175 | break; |
jjg@338 | 176 | // method type argument: wasn't specified |
jjg@338 | 177 | case METHOD_TYPE_ARGUMENT: |
jjg@338 | 178 | case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY: |
jjg@338 | 179 | if (showOffsets) { |
jjg@338 | 180 | print(", offset="); |
jjg@338 | 181 | print(pos.offset); |
jjg@338 | 182 | } |
jjg@338 | 183 | print(", type_index="); |
jjg@338 | 184 | print(pos.type_index); |
jjg@338 | 185 | break; |
jjg@338 | 186 | // We don't need to worry abut these |
jjg@338 | 187 | case METHOD_RETURN_GENERIC_OR_ARRAY: |
jjg@338 | 188 | case FIELD_GENERIC_OR_ARRAY: |
jjg@338 | 189 | break; |
jjg@338 | 190 | case UNKNOWN: |
jjg@338 | 191 | break; |
jjg@338 | 192 | default: |
jjg@338 | 193 | throw new AssertionError("unknown type: " + pos.type); |
jjg@338 | 194 | } |
jjg@338 | 195 | |
jjg@338 | 196 | // Append location data for generics/arrays. |
jjg@338 | 197 | if (pos.type.hasLocation()) { |
jjg@338 | 198 | print(", location="); |
jjg@338 | 199 | print(pos.location); |
jjg@338 | 200 | } |
jjg@308 | 201 | } |
jjg@308 | 202 | |
jjg@46 | 203 | public void write(Annotation.element_value_pair pair) { |
jjg@338 | 204 | write(pair, false); |
jjg@338 | 205 | } |
jjg@338 | 206 | |
jjg@338 | 207 | public void write(Annotation.element_value_pair pair, boolean resolveIndices) { |
jjg@338 | 208 | writeIndex(pair.element_name_index, resolveIndices); |
jjg@338 | 209 | print("="); |
jjg@338 | 210 | write(pair.value, resolveIndices); |
jjg@46 | 211 | } |
jjg@46 | 212 | |
jjg@46 | 213 | public void write(Annotation.element_value value) { |
jjg@338 | 214 | write(value, false); |
jjg@338 | 215 | } |
jjg@338 | 216 | |
jjg@338 | 217 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 218 | ev_writer.write(value, resolveIndices); |
jjg@338 | 219 | } |
jjg@338 | 220 | |
jjg@338 | 221 | private void writeDescriptor(int index, boolean resolveIndices) { |
jjg@338 | 222 | if (resolveIndices) { |
jjg@338 | 223 | try { |
jjg@338 | 224 | ConstantPool constant_pool = classWriter.getClassFile().constant_pool; |
jjg@338 | 225 | Descriptor d = new Descriptor(index); |
jjg@338 | 226 | print(d.getFieldType(constant_pool)); |
jjg@338 | 227 | return; |
jjg@338 | 228 | } catch (ConstantPoolException ignore) { |
jjg@338 | 229 | } catch (InvalidDescriptor ignore) { |
jjg@338 | 230 | } |
jjg@338 | 231 | } |
jjg@338 | 232 | |
jjg@338 | 233 | print("#" + index); |
jjg@338 | 234 | } |
jjg@338 | 235 | |
jjg@338 | 236 | private void writeIndex(int index, boolean resolveIndices) { |
jjg@338 | 237 | if (resolveIndices) { |
jjg@338 | 238 | print(constantWriter.stringValue(index)); |
jjg@338 | 239 | } else |
jjg@338 | 240 | print("#" + index); |
jjg@46 | 241 | } |
jjg@46 | 242 | |
jjg@46 | 243 | element_value_Writer ev_writer = new element_value_Writer(); |
jjg@46 | 244 | |
jjg@338 | 245 | class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> { |
jjg@338 | 246 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 247 | value.accept(this, resolveIndices); |
jjg@46 | 248 | } |
jjg@46 | 249 | |
jjg@338 | 250 | public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) { |
jjg@338 | 251 | if (resolveIndices) |
jjg@338 | 252 | writeIndex(ev.const_value_index, resolveIndices); |
jjg@338 | 253 | else |
jjg@338 | 254 | print(((char) ev.tag) + "#" + ev.const_value_index); |
jjg@46 | 255 | return null; |
jjg@46 | 256 | } |
jjg@46 | 257 | |
jjg@338 | 258 | public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) { |
jjg@338 | 259 | if (resolveIndices) { |
jjg@338 | 260 | writeIndex(ev.type_name_index, resolveIndices); |
jjg@338 | 261 | print("."); |
jjg@338 | 262 | writeIndex(ev.const_name_index, resolveIndices); |
jjg@338 | 263 | } else |
jjg@338 | 264 | print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index); |
jjg@46 | 265 | return null; |
jjg@46 | 266 | } |
jjg@46 | 267 | |
jjg@338 | 268 | public Void visitClass(Class_element_value ev, Boolean resolveIndices) { |
jjg@338 | 269 | if (resolveIndices) { |
jjg@338 | 270 | writeIndex(ev.class_info_index, resolveIndices); |
jjg@338 | 271 | print(".class"); |
jjg@338 | 272 | } else |
jjg@338 | 273 | print(((char) ev.tag) + "#" + ev.class_info_index); |
jjg@46 | 274 | return null; |
jjg@46 | 275 | } |
jjg@46 | 276 | |
jjg@338 | 277 | public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) { |
jjg@46 | 278 | print((char) ev.tag); |
jjg@338 | 279 | AnnotationWriter.this.write(ev.annotation_value, resolveIndices); |
jjg@46 | 280 | return null; |
jjg@46 | 281 | } |
jjg@46 | 282 | |
jjg@338 | 283 | public Void visitArray(Array_element_value ev, Boolean resolveIndices) { |
jjg@46 | 284 | print("["); |
jjg@46 | 285 | for (int i = 0; i < ev.num_values; i++) { |
jjg@46 | 286 | if (i > 0) |
jjg@46 | 287 | print(","); |
jjg@338 | 288 | write(ev.values[i], resolveIndices); |
jjg@46 | 289 | } |
jjg@46 | 290 | print("]"); |
jjg@46 | 291 | return null; |
jjg@46 | 292 | } |
jjg@46 | 293 | |
jjg@46 | 294 | } |
jjg@338 | 295 | |
jjg@338 | 296 | private ClassWriter classWriter; |
jjg@338 | 297 | private ConstantWriter constantWriter; |
jjg@46 | 298 | } |