001 /*
002 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javap;
027
028 import com.sun.tools.classfile.Annotation;
029 import com.sun.tools.classfile.Annotation.Annotation_element_value;
030 import com.sun.tools.classfile.Annotation.Array_element_value;
031 import com.sun.tools.classfile.Annotation.Class_element_value;
032 import com.sun.tools.classfile.Annotation.Enum_element_value;
033 import com.sun.tools.classfile.Annotation.Primitive_element_value;
034
035 /**
036 * A writer for writing annotations as text.
037 *
038 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
039 * you write code that depends on this, you do so at your own risk.
040 * This code and its internal interfaces are subject to change or
041 * deletion without notice.</b>
042 */
043 public class AnnotationWriter extends BasicWriter {
044 static AnnotationWriter instance(Context context) {
045 AnnotationWriter instance = context.get(AnnotationWriter.class);
046 if (instance == null)
047 instance = new AnnotationWriter(context);
048 return instance;
049 }
050
051 protected AnnotationWriter(Context context) {
052 super(context);
053 }
054
055 public void write(Annotation annot) {
056 print("#" + annot.type_index + "(");
057 for (int i = 0; i < annot.num_element_value_pairs; i++) {
058 if (i > 0)
059 print(",");
060 write(annot.element_value_pairs[i]);
061 }
062 print(")");
063 }
064
065 public void write(Annotation.element_value_pair pair) {
066 print("#" + pair.element_name_index + ":");
067 write(pair.value);
068 }
069
070 public void write(Annotation.element_value value) {
071 ev_writer.write(value);
072 }
073
074 element_value_Writer ev_writer = new element_value_Writer();
075
076 class element_value_Writer implements Annotation.element_value.Visitor<Void,Void> {
077 public void write(Annotation.element_value value) {
078 value.accept(this, null);
079 }
080
081 public Void visitPrimitive(Primitive_element_value ev, Void p) {
082 print(((char) ev.tag) + "#" + ev.const_value_index);
083 return null;
084 }
085
086 public Void visitEnum(Enum_element_value ev, Void p) {
087 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
088 return null;
089 }
090
091 public Void visitClass(Class_element_value ev, Void p) {
092 print(((char) ev.tag) + "#" + ev.class_info_index);
093 return null;
094 }
095
096 public Void visitAnnotation(Annotation_element_value ev, Void p) {
097 print((char) ev.tag);
098 AnnotationWriter.this.write(ev.annotation_value);
099 return null;
100 }
101
102 public Void visitArray(Array_element_value ev, Void p) {
103 print("[");
104 for (int i = 0; i < ev.num_values; i++) {
105 if (i > 0)
106 print(",");
107 write(ev.values[i]);
108 }
109 print("]");
110 return null;
111 }
112
113 }
114 }