Mon, 25 Mar 2013 16:55:14 -0700
8010521: jdk8 l10n resource file translation update 2
Reviewed-by: naoto, yhuang
jjg@46 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 2007, 2013, 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@1521 | 29 | import com.sun.tools.classfile.TypeAnnotation; |
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@1521 | 80 | public void write(TypeAnnotation annot) { |
jjg@1521 | 81 | write(annot, true, false); |
jjg@1521 | 82 | } |
jjg@1521 | 83 | |
jjg@1521 | 84 | public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) { |
jjg@1521 | 85 | write(annot.annotation, resolveIndices); |
jjg@1521 | 86 | print(": "); |
jjg@1521 | 87 | write(annot.position, showOffsets); |
jjg@1521 | 88 | } |
jjg@1521 | 89 | |
jjg@1521 | 90 | public void write(TypeAnnotation.Position pos, boolean showOffsets) { |
jjg@1521 | 91 | print(pos.type); |
jjg@1521 | 92 | |
jjg@1521 | 93 | switch (pos.type) { |
jjg@1521 | 94 | // instanceof |
jjg@1521 | 95 | case INSTANCEOF: |
jjg@1521 | 96 | // new expression |
jjg@1521 | 97 | case NEW: |
jjg@1563 | 98 | // constructor/method reference receiver |
jjg@1563 | 99 | case CONSTRUCTOR_REFERENCE: |
jjg@1563 | 100 | case METHOD_REFERENCE: |
jjg@1521 | 101 | if (showOffsets) { |
jjg@1521 | 102 | print(", offset="); |
jjg@1521 | 103 | print(pos.offset); |
jjg@1521 | 104 | } |
jjg@1521 | 105 | break; |
jjg@1521 | 106 | // local variable |
jjg@1521 | 107 | case LOCAL_VARIABLE: |
jjg@1521 | 108 | // resource variable |
jjg@1521 | 109 | case RESOURCE_VARIABLE: |
jjg@1521 | 110 | if (pos.lvarOffset == null) { |
jjg@1521 | 111 | print(", lvarOffset is Null!"); |
jjg@1521 | 112 | break; |
jjg@1521 | 113 | } |
jjg@1521 | 114 | print(", {"); |
jjg@1521 | 115 | for (int i = 0; i < pos.lvarOffset.length; ++i) { |
jjg@1521 | 116 | if (i != 0) print("; "); |
jjg@1521 | 117 | if (showOffsets) { |
jjg@1521 | 118 | print("start_pc="); |
jjg@1521 | 119 | print(pos.lvarOffset[i]); |
jjg@1521 | 120 | } |
jjg@1521 | 121 | print(", length="); |
jjg@1521 | 122 | print(pos.lvarLength[i]); |
jjg@1521 | 123 | print(", index="); |
jjg@1521 | 124 | print(pos.lvarIndex[i]); |
jjg@1521 | 125 | } |
jjg@1521 | 126 | print("}"); |
jjg@1521 | 127 | break; |
jjg@1521 | 128 | // exception parameter |
jjg@1521 | 129 | case EXCEPTION_PARAMETER: |
jjg@1521 | 130 | print(", exception_index="); |
jjg@1521 | 131 | print(pos.exception_index); |
jjg@1521 | 132 | break; |
jjg@1521 | 133 | // method receiver |
jjg@1521 | 134 | case METHOD_RECEIVER: |
jjg@1521 | 135 | // Do nothing |
jjg@1521 | 136 | break; |
jjg@1521 | 137 | // type parameter |
jjg@1521 | 138 | case CLASS_TYPE_PARAMETER: |
jjg@1521 | 139 | case METHOD_TYPE_PARAMETER: |
jjg@1521 | 140 | print(", param_index="); |
jjg@1521 | 141 | print(pos.parameter_index); |
jjg@1521 | 142 | break; |
jjg@1521 | 143 | // type parameter bound |
jjg@1521 | 144 | case CLASS_TYPE_PARAMETER_BOUND: |
jjg@1521 | 145 | case METHOD_TYPE_PARAMETER_BOUND: |
jjg@1521 | 146 | print(", param_index="); |
jjg@1521 | 147 | print(pos.parameter_index); |
jjg@1521 | 148 | print(", bound_index="); |
jjg@1521 | 149 | print(pos.bound_index); |
jjg@1521 | 150 | break; |
jjg@1521 | 151 | // class extends or implements clause |
jjg@1521 | 152 | case CLASS_EXTENDS: |
jjg@1521 | 153 | print(", type_index="); |
jjg@1521 | 154 | print(pos.type_index); |
jjg@1521 | 155 | break; |
jjg@1521 | 156 | // throws |
jjg@1521 | 157 | case THROWS: |
jjg@1521 | 158 | print(", type_index="); |
jjg@1521 | 159 | print(pos.type_index); |
jjg@1521 | 160 | break; |
jjg@1521 | 161 | // method parameter |
jjg@1521 | 162 | case METHOD_FORMAL_PARAMETER: |
jjg@1521 | 163 | print(", param_index="); |
jjg@1521 | 164 | print(pos.parameter_index); |
jjg@1521 | 165 | break; |
jjg@1563 | 166 | // type cast |
jjg@1563 | 167 | case CAST: |
jjg@1521 | 168 | // method/constructor/reference type argument |
jjg@1521 | 169 | case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: |
jjg@1521 | 170 | case METHOD_INVOCATION_TYPE_ARGUMENT: |
jjg@1563 | 171 | case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: |
jjg@1521 | 172 | case METHOD_REFERENCE_TYPE_ARGUMENT: |
jjg@1521 | 173 | if (showOffsets) { |
jjg@1521 | 174 | print(", offset="); |
jjg@1521 | 175 | print(pos.offset); |
jjg@1521 | 176 | } |
jjg@1521 | 177 | print(", type_index="); |
jjg@1521 | 178 | print(pos.type_index); |
jjg@1521 | 179 | break; |
jjg@1521 | 180 | // We don't need to worry about these |
jjg@1521 | 181 | case METHOD_RETURN: |
jjg@1521 | 182 | case FIELD: |
jjg@1521 | 183 | break; |
jjg@1521 | 184 | case UNKNOWN: |
jjg@1521 | 185 | throw new AssertionError("AnnotationWriter: UNKNOWN target type should never occur!"); |
jjg@1521 | 186 | default: |
jjg@1521 | 187 | throw new AssertionError("AnnotationWriter: Unknown target type for position: " + pos); |
jjg@1521 | 188 | } |
jjg@1521 | 189 | |
jjg@1521 | 190 | // Append location data for generics/arrays. |
jjg@1521 | 191 | if (!pos.location.isEmpty()) { |
jjg@1521 | 192 | print(", location="); |
jjg@1521 | 193 | print(pos.location); |
jjg@1521 | 194 | } |
jjg@1521 | 195 | } |
jjg@1521 | 196 | |
jjg@46 | 197 | public void write(Annotation.element_value_pair pair) { |
jjg@338 | 198 | write(pair, false); |
jjg@338 | 199 | } |
jjg@338 | 200 | |
jjg@338 | 201 | public void write(Annotation.element_value_pair pair, boolean resolveIndices) { |
jjg@338 | 202 | writeIndex(pair.element_name_index, resolveIndices); |
jjg@338 | 203 | print("="); |
jjg@338 | 204 | write(pair.value, resolveIndices); |
jjg@46 | 205 | } |
jjg@46 | 206 | |
jjg@46 | 207 | public void write(Annotation.element_value value) { |
jjg@338 | 208 | write(value, false); |
jjg@338 | 209 | } |
jjg@338 | 210 | |
jjg@338 | 211 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 212 | ev_writer.write(value, resolveIndices); |
jjg@338 | 213 | } |
jjg@338 | 214 | |
jjg@338 | 215 | private void writeDescriptor(int index, boolean resolveIndices) { |
jjg@338 | 216 | if (resolveIndices) { |
jjg@338 | 217 | try { |
jjg@338 | 218 | ConstantPool constant_pool = classWriter.getClassFile().constant_pool; |
jjg@338 | 219 | Descriptor d = new Descriptor(index); |
jjg@338 | 220 | print(d.getFieldType(constant_pool)); |
jjg@338 | 221 | return; |
jjg@338 | 222 | } catch (ConstantPoolException ignore) { |
jjg@338 | 223 | } catch (InvalidDescriptor ignore) { |
jjg@338 | 224 | } |
jjg@338 | 225 | } |
jjg@338 | 226 | |
jjg@338 | 227 | print("#" + index); |
jjg@338 | 228 | } |
jjg@338 | 229 | |
jjg@338 | 230 | private void writeIndex(int index, boolean resolveIndices) { |
jjg@338 | 231 | if (resolveIndices) { |
jjg@338 | 232 | print(constantWriter.stringValue(index)); |
jjg@338 | 233 | } else |
jjg@338 | 234 | print("#" + index); |
jjg@46 | 235 | } |
jjg@46 | 236 | |
jjg@46 | 237 | element_value_Writer ev_writer = new element_value_Writer(); |
jjg@46 | 238 | |
jjg@338 | 239 | class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> { |
jjg@338 | 240 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 241 | value.accept(this, resolveIndices); |
jjg@46 | 242 | } |
jjg@46 | 243 | |
jjg@338 | 244 | public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) { |
jjg@338 | 245 | if (resolveIndices) |
jjg@338 | 246 | writeIndex(ev.const_value_index, resolveIndices); |
jjg@338 | 247 | else |
jjg@338 | 248 | print(((char) ev.tag) + "#" + ev.const_value_index); |
jjg@46 | 249 | return null; |
jjg@46 | 250 | } |
jjg@46 | 251 | |
jjg@338 | 252 | public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) { |
jjg@338 | 253 | if (resolveIndices) { |
jjg@338 | 254 | writeIndex(ev.type_name_index, resolveIndices); |
jjg@338 | 255 | print("."); |
jjg@338 | 256 | writeIndex(ev.const_name_index, resolveIndices); |
jjg@338 | 257 | } else |
jjg@338 | 258 | print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index); |
jjg@46 | 259 | return null; |
jjg@46 | 260 | } |
jjg@46 | 261 | |
jjg@338 | 262 | public Void visitClass(Class_element_value ev, Boolean resolveIndices) { |
jjg@338 | 263 | if (resolveIndices) { |
jjg@338 | 264 | writeIndex(ev.class_info_index, resolveIndices); |
jjg@338 | 265 | print(".class"); |
jjg@338 | 266 | } else |
jjg@338 | 267 | print(((char) ev.tag) + "#" + ev.class_info_index); |
jjg@46 | 268 | return null; |
jjg@46 | 269 | } |
jjg@46 | 270 | |
jjg@338 | 271 | public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) { |
jjg@46 | 272 | print((char) ev.tag); |
jjg@338 | 273 | AnnotationWriter.this.write(ev.annotation_value, resolveIndices); |
jjg@46 | 274 | return null; |
jjg@46 | 275 | } |
jjg@46 | 276 | |
jjg@338 | 277 | public Void visitArray(Array_element_value ev, Boolean resolveIndices) { |
jjg@46 | 278 | print("["); |
jjg@46 | 279 | for (int i = 0; i < ev.num_values; i++) { |
jjg@46 | 280 | if (i > 0) |
jjg@46 | 281 | print(","); |
jjg@338 | 282 | write(ev.values[i], resolveIndices); |
jjg@46 | 283 | } |
jjg@46 | 284 | print("]"); |
jjg@46 | 285 | return null; |
jjg@46 | 286 | } |
jjg@46 | 287 | |
jjg@46 | 288 | } |
jjg@338 | 289 | |
jjg@338 | 290 | private ClassWriter classWriter; |
jjg@338 | 291 | private ConstantWriter constantWriter; |
jjg@46 | 292 | } |