Tue, 16 Sep 2008 18:35:18 -0700
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
Reviewed-by: darcy, 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@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@46 | 34 | |
jjg@46 | 35 | /** |
jjg@46 | 36 | * A writer for writing annotations as text. |
jjg@46 | 37 | * |
jjg@46 | 38 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
jjg@46 | 39 | * you write code that depends on this, you do so at your own risk. |
jjg@46 | 40 | * This code and its internal interfaces are subject to change or |
jjg@46 | 41 | * deletion without notice.</b> |
jjg@46 | 42 | */ |
jjg@46 | 43 | public class AnnotationWriter extends BasicWriter { |
jjg@46 | 44 | static AnnotationWriter instance(Context context) { |
jjg@46 | 45 | AnnotationWriter instance = context.get(AnnotationWriter.class); |
jjg@46 | 46 | if (instance == null) |
jjg@46 | 47 | instance = new AnnotationWriter(context); |
jjg@46 | 48 | return instance; |
jjg@46 | 49 | } |
jjg@46 | 50 | |
jjg@46 | 51 | protected AnnotationWriter(Context context) { |
jjg@46 | 52 | super(context); |
jjg@46 | 53 | } |
jjg@46 | 54 | |
jjg@46 | 55 | public void write(Annotation annot) { |
jjg@46 | 56 | print("#" + annot.type_index + "("); |
jjg@46 | 57 | for (int i = 0; i < annot.num_element_value_pairs; i++) { |
jjg@46 | 58 | if (i > 0) |
jjg@46 | 59 | print(","); |
jjg@46 | 60 | write(annot.element_value_pairs[i]); |
jjg@46 | 61 | } |
jjg@46 | 62 | print(")"); |
jjg@46 | 63 | } |
jjg@46 | 64 | |
jjg@46 | 65 | public void write(Annotation.element_value_pair pair) { |
jjg@46 | 66 | print("#" + pair.element_name_index + ":"); |
jjg@46 | 67 | write(pair.value); |
jjg@46 | 68 | } |
jjg@46 | 69 | |
jjg@46 | 70 | public void write(Annotation.element_value value) { |
jjg@46 | 71 | ev_writer.write(value); |
jjg@46 | 72 | } |
jjg@46 | 73 | |
jjg@46 | 74 | element_value_Writer ev_writer = new element_value_Writer(); |
jjg@46 | 75 | |
jjg@46 | 76 | class element_value_Writer implements Annotation.element_value.Visitor<Void,Void> { |
jjg@46 | 77 | public void write(Annotation.element_value value) { |
jjg@46 | 78 | value.accept(this, null); |
jjg@46 | 79 | } |
jjg@46 | 80 | |
jjg@46 | 81 | public Void visitPrimitive(Primitive_element_value ev, Void p) { |
jjg@46 | 82 | print(((char) ev.tag) + "#" + ev.const_value_index); |
jjg@46 | 83 | return null; |
jjg@46 | 84 | } |
jjg@46 | 85 | |
jjg@46 | 86 | public Void visitEnum(Enum_element_value ev, Void p) { |
jjg@46 | 87 | print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index); |
jjg@46 | 88 | return null; |
jjg@46 | 89 | } |
jjg@46 | 90 | |
jjg@46 | 91 | public Void visitClass(Class_element_value ev, Void p) { |
jjg@46 | 92 | print(((char) ev.tag) + "#" + ev.class_info_index); |
jjg@46 | 93 | return null; |
jjg@46 | 94 | } |
jjg@46 | 95 | |
jjg@46 | 96 | public Void visitAnnotation(Annotation_element_value ev, Void p) { |
jjg@46 | 97 | print((char) ev.tag); |
jjg@46 | 98 | AnnotationWriter.this.write(ev.annotation_value); |
jjg@46 | 99 | return null; |
jjg@46 | 100 | } |
jjg@46 | 101 | |
jjg@46 | 102 | public Void visitArray(Array_element_value ev, Void p) { |
jjg@46 | 103 | print("["); |
jjg@46 | 104 | for (int i = 0; i < ev.num_values; i++) { |
jjg@46 | 105 | if (i > 0) |
jjg@46 | 106 | print(","); |
jjg@46 | 107 | write(ev.values[i]); |
jjg@46 | 108 | } |
jjg@46 | 109 | print("]"); |
jjg@46 | 110 | return null; |
jjg@46 | 111 | } |
jjg@46 | 112 | |
jjg@46 | 113 | } |
jjg@46 | 114 | } |