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

Tue, 28 Jul 2009 10:36:25 -0700

author
jjg
date
Tue, 28 Jul 2009 10:36:25 -0700
changeset 338
777a3efad0d5
parent 308
03944ee4fac4
child 485
b0a68258360a
permissions
-rw-r--r--

6855990: javap InstructionDetailWriter should support new 308 annotations attribute
Reviewed-by: mcimadamore

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@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@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 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@338 165 if (showOffsets) {
jjg@338 166 print(", offset=");
jjg@338 167 print(pos.offset);
jjg@338 168 }
jjg@338 169 break;
jjg@338 170 // method parameter: not specified
jjg@338 171 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@338 172 print(", param_index=");
jjg@338 173 print(pos.parameter_index);
jjg@338 174 break;
jjg@338 175 // method type argument: wasn't specified
jjg@338 176 case METHOD_TYPE_ARGUMENT:
jjg@338 177 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@338 178 if (showOffsets) {
jjg@338 179 print(", offset=");
jjg@338 180 print(pos.offset);
jjg@338 181 }
jjg@338 182 print(", type_index=");
jjg@338 183 print(pos.type_index);
jjg@338 184 break;
jjg@338 185 // We don't need to worry abut these
jjg@338 186 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@338 187 case FIELD_GENERIC_OR_ARRAY:
jjg@338 188 break;
jjg@338 189 case UNKNOWN:
jjg@338 190 break;
jjg@338 191 default:
jjg@338 192 throw new AssertionError("unknown type: " + pos.type);
jjg@338 193 }
jjg@338 194
jjg@338 195 // Append location data for generics/arrays.
jjg@338 196 if (pos.type.hasLocation()) {
jjg@338 197 print(", location=");
jjg@338 198 print(pos.location);
jjg@338 199 }
jjg@308 200 }
jjg@308 201
jjg@46 202 public void write(Annotation.element_value_pair pair) {
jjg@338 203 write(pair, false);
jjg@338 204 }
jjg@338 205
jjg@338 206 public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
jjg@338 207 writeIndex(pair.element_name_index, resolveIndices);
jjg@338 208 print("=");
jjg@338 209 write(pair.value, resolveIndices);
jjg@46 210 }
jjg@46 211
jjg@46 212 public void write(Annotation.element_value value) {
jjg@338 213 write(value, false);
jjg@338 214 }
jjg@338 215
jjg@338 216 public void write(Annotation.element_value value, boolean resolveIndices) {
jjg@338 217 ev_writer.write(value, resolveIndices);
jjg@338 218 }
jjg@338 219
jjg@338 220 private void writeDescriptor(int index, boolean resolveIndices) {
jjg@338 221 if (resolveIndices) {
jjg@338 222 try {
jjg@338 223 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
jjg@338 224 Descriptor d = new Descriptor(index);
jjg@338 225 print(d.getFieldType(constant_pool));
jjg@338 226 return;
jjg@338 227 } catch (ConstantPoolException ignore) {
jjg@338 228 } catch (InvalidDescriptor ignore) {
jjg@338 229 }
jjg@338 230 }
jjg@338 231
jjg@338 232 print("#" + index);
jjg@338 233 }
jjg@338 234
jjg@338 235 private void writeIndex(int index, boolean resolveIndices) {
jjg@338 236 if (resolveIndices) {
jjg@338 237 print(constantWriter.stringValue(index));
jjg@338 238 } else
jjg@338 239 print("#" + index);
jjg@46 240 }
jjg@46 241
jjg@46 242 element_value_Writer ev_writer = new element_value_Writer();
jjg@46 243
jjg@338 244 class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
jjg@338 245 public void write(Annotation.element_value value, boolean resolveIndices) {
jjg@338 246 value.accept(this, resolveIndices);
jjg@46 247 }
jjg@46 248
jjg@338 249 public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
jjg@338 250 if (resolveIndices)
jjg@338 251 writeIndex(ev.const_value_index, resolveIndices);
jjg@338 252 else
jjg@338 253 print(((char) ev.tag) + "#" + ev.const_value_index);
jjg@46 254 return null;
jjg@46 255 }
jjg@46 256
jjg@338 257 public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
jjg@338 258 if (resolveIndices) {
jjg@338 259 writeIndex(ev.type_name_index, resolveIndices);
jjg@338 260 print(".");
jjg@338 261 writeIndex(ev.const_name_index, resolveIndices);
jjg@338 262 } else
jjg@338 263 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
jjg@46 264 return null;
jjg@46 265 }
jjg@46 266
jjg@338 267 public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
jjg@338 268 if (resolveIndices) {
jjg@338 269 writeIndex(ev.class_info_index, resolveIndices);
jjg@338 270 print(".class");
jjg@338 271 } else
jjg@338 272 print(((char) ev.tag) + "#" + ev.class_info_index);
jjg@46 273 return null;
jjg@46 274 }
jjg@46 275
jjg@338 276 public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
jjg@46 277 print((char) ev.tag);
jjg@338 278 AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
jjg@46 279 return null;
jjg@46 280 }
jjg@46 281
jjg@338 282 public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
jjg@46 283 print("[");
jjg@46 284 for (int i = 0; i < ev.num_values; i++) {
jjg@46 285 if (i > 0)
jjg@46 286 print(",");
jjg@338 287 write(ev.values[i], resolveIndices);
jjg@46 288 }
jjg@46 289 print("]");
jjg@46 290 return null;
jjg@46 291 }
jjg@46 292
jjg@46 293 }
jjg@338 294
jjg@338 295 private ClassWriter classWriter;
jjg@338 296 private ConstantWriter constantWriter;
jjg@46 297 }

mercurial