Tue, 08 Jan 2013 10:17:29 +0100
8005184: Restructure DeferredAttr to allow pluggable deferred type completers
Summary: Add hooks to generalize deferred type completion via custom helper objects
Reviewed-by: jjg
jjg@46 | 1 | /* |
jjg@815 | 2 | * Copyright (c) 2007, 2011, 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@46 | 29 | import com.sun.tools.classfile.Annotation.Annotation_element_value; |
jjg@46 | 30 | import com.sun.tools.classfile.Annotation.Array_element_value; |
jjg@46 | 31 | import com.sun.tools.classfile.Annotation.Class_element_value; |
jjg@46 | 32 | import com.sun.tools.classfile.Annotation.Enum_element_value; |
jjg@46 | 33 | import com.sun.tools.classfile.Annotation.Primitive_element_value; |
jjg@338 | 34 | import com.sun.tools.classfile.ConstantPool; |
jjg@338 | 35 | import com.sun.tools.classfile.ConstantPoolException; |
jjg@338 | 36 | import com.sun.tools.classfile.Descriptor; |
jjg@338 | 37 | import com.sun.tools.classfile.Descriptor.InvalidDescriptor; |
jjg@46 | 38 | |
jjg@46 | 39 | /** |
jjg@46 | 40 | * A writer for writing annotations as text. |
jjg@46 | 41 | * |
jjg@581 | 42 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 43 | * If you write code that depends on this, you do so at your own risk. |
jjg@46 | 44 | * This code and its internal interfaces are subject to change or |
jjg@46 | 45 | * deletion without notice.</b> |
jjg@46 | 46 | */ |
jjg@46 | 47 | public class AnnotationWriter extends BasicWriter { |
jjg@46 | 48 | static AnnotationWriter instance(Context context) { |
jjg@46 | 49 | AnnotationWriter instance = context.get(AnnotationWriter.class); |
jjg@46 | 50 | if (instance == null) |
jjg@46 | 51 | instance = new AnnotationWriter(context); |
jjg@46 | 52 | return instance; |
jjg@46 | 53 | } |
jjg@46 | 54 | |
jjg@46 | 55 | protected AnnotationWriter(Context context) { |
jjg@46 | 56 | super(context); |
jjg@338 | 57 | classWriter = ClassWriter.instance(context); |
jjg@338 | 58 | constantWriter = ConstantWriter.instance(context); |
jjg@46 | 59 | } |
jjg@46 | 60 | |
jjg@46 | 61 | public void write(Annotation annot) { |
jjg@338 | 62 | write(annot, false); |
jjg@338 | 63 | } |
jjg@338 | 64 | |
jjg@338 | 65 | public void write(Annotation annot, boolean resolveIndices) { |
jjg@338 | 66 | writeDescriptor(annot.type_index, resolveIndices); |
jjg@338 | 67 | boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices; |
jjg@338 | 68 | if (showParens) |
jjg@338 | 69 | print("("); |
jjg@46 | 70 | for (int i = 0; i < annot.num_element_value_pairs; i++) { |
jjg@46 | 71 | if (i > 0) |
jjg@46 | 72 | print(","); |
jjg@338 | 73 | write(annot.element_value_pairs[i], resolveIndices); |
jjg@46 | 74 | } |
jjg@338 | 75 | if (showParens) |
jjg@338 | 76 | print(")"); |
jjg@46 | 77 | } |
jjg@46 | 78 | |
jjg@46 | 79 | public void write(Annotation.element_value_pair pair) { |
jjg@338 | 80 | write(pair, false); |
jjg@338 | 81 | } |
jjg@338 | 82 | |
jjg@338 | 83 | public void write(Annotation.element_value_pair pair, boolean resolveIndices) { |
jjg@338 | 84 | writeIndex(pair.element_name_index, resolveIndices); |
jjg@338 | 85 | print("="); |
jjg@338 | 86 | write(pair.value, resolveIndices); |
jjg@46 | 87 | } |
jjg@46 | 88 | |
jjg@46 | 89 | public void write(Annotation.element_value value) { |
jjg@338 | 90 | write(value, false); |
jjg@338 | 91 | } |
jjg@338 | 92 | |
jjg@338 | 93 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 94 | ev_writer.write(value, resolveIndices); |
jjg@338 | 95 | } |
jjg@338 | 96 | |
jjg@338 | 97 | private void writeDescriptor(int index, boolean resolveIndices) { |
jjg@338 | 98 | if (resolveIndices) { |
jjg@338 | 99 | try { |
jjg@338 | 100 | ConstantPool constant_pool = classWriter.getClassFile().constant_pool; |
jjg@338 | 101 | Descriptor d = new Descriptor(index); |
jjg@338 | 102 | print(d.getFieldType(constant_pool)); |
jjg@338 | 103 | return; |
jjg@338 | 104 | } catch (ConstantPoolException ignore) { |
jjg@338 | 105 | } catch (InvalidDescriptor ignore) { |
jjg@338 | 106 | } |
jjg@338 | 107 | } |
jjg@338 | 108 | |
jjg@338 | 109 | print("#" + index); |
jjg@338 | 110 | } |
jjg@338 | 111 | |
jjg@338 | 112 | private void writeIndex(int index, boolean resolveIndices) { |
jjg@338 | 113 | if (resolveIndices) { |
jjg@338 | 114 | print(constantWriter.stringValue(index)); |
jjg@338 | 115 | } else |
jjg@338 | 116 | print("#" + index); |
jjg@46 | 117 | } |
jjg@46 | 118 | |
jjg@46 | 119 | element_value_Writer ev_writer = new element_value_Writer(); |
jjg@46 | 120 | |
jjg@338 | 121 | class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> { |
jjg@338 | 122 | public void write(Annotation.element_value value, boolean resolveIndices) { |
jjg@338 | 123 | value.accept(this, resolveIndices); |
jjg@46 | 124 | } |
jjg@46 | 125 | |
jjg@338 | 126 | public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) { |
jjg@338 | 127 | if (resolveIndices) |
jjg@338 | 128 | writeIndex(ev.const_value_index, resolveIndices); |
jjg@338 | 129 | else |
jjg@338 | 130 | print(((char) ev.tag) + "#" + ev.const_value_index); |
jjg@46 | 131 | return null; |
jjg@46 | 132 | } |
jjg@46 | 133 | |
jjg@338 | 134 | public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) { |
jjg@338 | 135 | if (resolveIndices) { |
jjg@338 | 136 | writeIndex(ev.type_name_index, resolveIndices); |
jjg@338 | 137 | print("."); |
jjg@338 | 138 | writeIndex(ev.const_name_index, resolveIndices); |
jjg@338 | 139 | } else |
jjg@338 | 140 | print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index); |
jjg@46 | 141 | return null; |
jjg@46 | 142 | } |
jjg@46 | 143 | |
jjg@338 | 144 | public Void visitClass(Class_element_value ev, Boolean resolveIndices) { |
jjg@338 | 145 | if (resolveIndices) { |
jjg@338 | 146 | writeIndex(ev.class_info_index, resolveIndices); |
jjg@338 | 147 | print(".class"); |
jjg@338 | 148 | } else |
jjg@338 | 149 | print(((char) ev.tag) + "#" + ev.class_info_index); |
jjg@46 | 150 | return null; |
jjg@46 | 151 | } |
jjg@46 | 152 | |
jjg@338 | 153 | public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) { |
jjg@46 | 154 | print((char) ev.tag); |
jjg@338 | 155 | AnnotationWriter.this.write(ev.annotation_value, resolveIndices); |
jjg@46 | 156 | return null; |
jjg@46 | 157 | } |
jjg@46 | 158 | |
jjg@338 | 159 | public Void visitArray(Array_element_value ev, Boolean resolveIndices) { |
jjg@46 | 160 | print("["); |
jjg@46 | 161 | for (int i = 0; i < ev.num_values; i++) { |
jjg@46 | 162 | if (i > 0) |
jjg@46 | 163 | print(","); |
jjg@338 | 164 | write(ev.values[i], resolveIndices); |
jjg@46 | 165 | } |
jjg@46 | 166 | print("]"); |
jjg@46 | 167 | return null; |
jjg@46 | 168 | } |
jjg@46 | 169 | |
jjg@46 | 170 | } |
jjg@338 | 171 | |
jjg@338 | 172 | private ClassWriter classWriter; |
jjg@338 | 173 | private ConstantWriter constantWriter; |
jjg@46 | 174 | } |