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

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini

     1 /*
     2  * Copyright 2007 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.Annotation.Annotation_element_value;
    30 import com.sun.tools.classfile.Annotation.Array_element_value;
    31 import com.sun.tools.classfile.Annotation.Class_element_value;
    32 import com.sun.tools.classfile.Annotation.Enum_element_value;
    33 import com.sun.tools.classfile.Annotation.Primitive_element_value;
    35 /**
    36  *  A writer for writing annotations as text.
    37  *
    38  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    39  *  you write code that depends on this, you do so at your own risk.
    40  *  This code and its internal interfaces are subject to change or
    41  *  deletion without notice.</b>
    42  */
    43 public class AnnotationWriter extends BasicWriter {
    44     static AnnotationWriter instance(Context context) {
    45         AnnotationWriter instance = context.get(AnnotationWriter.class);
    46         if (instance == null)
    47             instance = new AnnotationWriter(context);
    48         return instance;
    49     }
    51     protected AnnotationWriter(Context context) {
    52         super(context);
    53     }
    55     public void write(Annotation annot) {
    56         print("#" + annot.type_index + "(");
    57         for (int i = 0; i < annot.num_element_value_pairs; i++) {
    58             if (i > 0)
    59                 print(",");
    60             write(annot.element_value_pairs[i]);
    61         }
    62         print(")");
    63     }
    65     public void write(Annotation.element_value_pair pair) {
    66         print("#" + pair.element_name_index + ":");
    67         write(pair.value);
    68     }
    70     public void write(Annotation.element_value value) {
    71         ev_writer.write(value);
    72     }
    74     element_value_Writer ev_writer = new element_value_Writer();
    76     class element_value_Writer implements Annotation.element_value.Visitor<Void,Void> {
    77         public void write(Annotation.element_value value) {
    78             value.accept(this, null);
    79         }
    81         public Void visitPrimitive(Primitive_element_value ev, Void p) {
    82             print(((char) ev.tag) + "#" + ev.const_value_index);
    83             return null;
    84         }
    86         public Void visitEnum(Enum_element_value ev, Void p) {
    87             print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
    88             return null;
    89         }
    91         public Void visitClass(Class_element_value ev, Void p) {
    92             print(((char) ev.tag) + "#" + ev.class_info_index);
    93             return null;
    94         }
    96         public Void visitAnnotation(Annotation_element_value ev, Void p) {
    97             print((char) ev.tag);
    98             AnnotationWriter.this.write(ev.annotation_value);
    99             return null;
   100         }
   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         }
   113     }
   114 }

mercurial