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

Fri, 26 Jun 2009 18:51:39 -0700

author
jjg
date
Fri, 26 Jun 2009 18:51:39 -0700
changeset 308
03944ee4fac4
parent 54
eaf608c64fec
child 338
777a3efad0d5
permissions
-rw-r--r--

6843077: JSR 308: Annotations on types
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

     1 /*
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javap;
    28 import com.sun.tools.classfile.Annotation;
    29 import com.sun.tools.classfile.ExtendedAnnotation;
    30 import com.sun.tools.classfile.Annotation.Annotation_element_value;
    31 import com.sun.tools.classfile.Annotation.Array_element_value;
    32 import com.sun.tools.classfile.Annotation.Class_element_value;
    33 import com.sun.tools.classfile.Annotation.Enum_element_value;
    34 import com.sun.tools.classfile.Annotation.Primitive_element_value;
    36 /**
    37  *  A writer for writing annotations as text.
    38  *
    39  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    40  *  you write code that depends on this, you do so at your own risk.
    41  *  This code and its internal interfaces are subject to change or
    42  *  deletion without notice.</b>
    43  */
    44 public class AnnotationWriter extends BasicWriter {
    45     static AnnotationWriter instance(Context context) {
    46         AnnotationWriter instance = context.get(AnnotationWriter.class);
    47         if (instance == null)
    48             instance = new AnnotationWriter(context);
    49         return instance;
    50     }
    52     protected AnnotationWriter(Context context) {
    53         super(context);
    54     }
    56     public void write(Annotation annot) {
    57         print("#" + annot.type_index + "(");
    58         for (int i = 0; i < annot.num_element_value_pairs; i++) {
    59             if (i > 0)
    60                 print(",");
    61             write(annot.element_value_pairs[i]);
    62         }
    63         print(")");
    64     }
    66     public void write(ExtendedAnnotation annot) {
    67         write(annot.annotation);
    68         print('@');
    69         print(annot.position.toString());
    70     }
    72     public void write(Annotation.element_value_pair pair) {
    73         print("#" + pair.element_name_index + ":");
    74         write(pair.value);
    75     }
    77     public void write(Annotation.element_value value) {
    78         ev_writer.write(value);
    79     }
    81     element_value_Writer ev_writer = new element_value_Writer();
    83     class element_value_Writer implements Annotation.element_value.Visitor<Void,Void> {
    84         public void write(Annotation.element_value value) {
    85             value.accept(this, null);
    86         }
    88         public Void visitPrimitive(Primitive_element_value ev, Void p) {
    89             print(((char) ev.tag) + "#" + ev.const_value_index);
    90             return null;
    91         }
    93         public Void visitEnum(Enum_element_value ev, Void p) {
    94             print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
    95             return null;
    96         }
    98         public Void visitClass(Class_element_value ev, Void p) {
    99             print(((char) ev.tag) + "#" + ev.class_info_index);
   100             return null;
   101         }
   103         public Void visitAnnotation(Annotation_element_value ev, Void p) {
   104             print((char) ev.tag);
   105             AnnotationWriter.this.write(ev.annotation_value);
   106             return null;
   107         }
   109         public Void visitArray(Array_element_value ev, Void p) {
   110             print("[");
   111             for (int i = 0; i < ev.num_values; i++) {
   112                 if (i > 0)
   113                     print(",");
   114                 write(ev.values[i]);
   115             }
   116             print("]");
   117             return null;
   118         }
   120     }
   121 }

mercurial