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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 815
d17f37522154
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * 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;
    35 import com.sun.tools.classfile.ConstantPool;
    36 import com.sun.tools.classfile.ConstantPoolException;
    37 import com.sun.tools.classfile.Descriptor;
    38 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
    40 /**
    41  *  A writer for writing annotations as text.
    42  *
    43  *  <p><b>This is NOT part of any supported API.
    44  *  If you write code that depends on this, you do so at your own risk.
    45  *  This code and its internal interfaces are subject to change or
    46  *  deletion without notice.</b>
    47  */
    48 public class AnnotationWriter extends BasicWriter {
    49     static AnnotationWriter instance(Context context) {
    50         AnnotationWriter instance = context.get(AnnotationWriter.class);
    51         if (instance == null)
    52             instance = new AnnotationWriter(context);
    53         return instance;
    54     }
    56     protected AnnotationWriter(Context context) {
    57         super(context);
    58         classWriter = ClassWriter.instance(context);
    59         constantWriter = ConstantWriter.instance(context);
    60     }
    62     public void write(Annotation annot) {
    63         write(annot, false);
    64     }
    66     public void write(Annotation annot, boolean resolveIndices) {
    67         writeDescriptor(annot.type_index, resolveIndices);
    68         boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
    69         if (showParens)
    70             print("(");
    71         for (int i = 0; i < annot.num_element_value_pairs; i++) {
    72             if (i > 0)
    73                 print(",");
    74             write(annot.element_value_pairs[i], resolveIndices);
    75         }
    76         if (showParens)
    77             print(")");
    78     }
    80     public void write(ExtendedAnnotation annot) {
    81         write(annot, true, false);
    82     }
    84     public void write(ExtendedAnnotation annot, boolean showOffsets, boolean resolveIndices) {
    85         write(annot.annotation, resolveIndices);
    86         print(": ");
    87         write(annot.position, showOffsets);
    88     }
    90     public void write(ExtendedAnnotation.Position pos, boolean showOffsets) {
    91         print(pos.type);
    93         switch (pos.type) {
    94         // type case
    95         case TYPECAST:
    96         case TYPECAST_GENERIC_OR_ARRAY:
    97         // object creation
    98         case INSTANCEOF:
    99         case INSTANCEOF_GENERIC_OR_ARRAY:
   100         // new expression
   101         case NEW:
   102         case NEW_GENERIC_OR_ARRAY:
   103         case NEW_TYPE_ARGUMENT:
   104         case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   105             if (showOffsets) {
   106                 print(", offset=");
   107                 print(pos.offset);
   108             }
   109             break;
   110          // local variable
   111         case LOCAL_VARIABLE:
   112         case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
   113             print(", {");
   114             for (int i = 0; i < pos.lvarOffset.length; ++i) {
   115                 if (i != 0) print("; ");
   116                 if (showOffsets) {
   117                     print(", start_pc=");
   118                     print(pos.lvarOffset[i]);
   119                 }
   120                 print(", length=");
   121                 print(pos.lvarLength[i]);
   122                 print(", index=");
   123                 print(pos.lvarIndex[i]);
   124             }
   125             print("}");
   126             break;
   127          // method receiver
   128         case METHOD_RECEIVER:
   129             // Do nothing
   130             break;
   131         // type parameters
   132         case CLASS_TYPE_PARAMETER:
   133         case METHOD_TYPE_PARAMETER:
   134             print(", param_index=");
   135             print(pos.parameter_index);
   136             break;
   137         // type parameters bound
   138         case CLASS_TYPE_PARAMETER_BOUND:
   139         case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   140         case METHOD_TYPE_PARAMETER_BOUND:
   141         case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
   142             print(", param_index=");
   143             print(pos.parameter_index);
   144             print(", bound_index=");
   145             print(pos.bound_index);
   146             break;
   147          // wildcard
   148         case WILDCARD_BOUND:
   149         case WILDCARD_BOUND_GENERIC_OR_ARRAY:
   150             print(", wild_card=");
   151             print(pos.wildcard_position);
   152             break;
   153          // Class extends and implements clauses
   154         case CLASS_EXTENDS:
   155         case CLASS_EXTENDS_GENERIC_OR_ARRAY:
   156             print(", type_index=");
   157             print(pos.type_index);
   158             break;
   159         // throws
   160         case THROWS:
   161             print(", type_index=");
   162             print(pos.type_index);
   163             break;
   164         case CLASS_LITERAL:
   165         case CLASS_LITERAL_GENERIC_OR_ARRAY:
   166             if (showOffsets) {
   167                 print(", offset=");
   168                 print(pos.offset);
   169             }
   170             break;
   171         // method parameter: not specified
   172         case METHOD_PARAMETER_GENERIC_OR_ARRAY:
   173             print(", param_index=");
   174             print(pos.parameter_index);
   175             break;
   176         // method type argument: wasn't specified
   177         case METHOD_TYPE_ARGUMENT:
   178         case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
   179             if (showOffsets) {
   180                 print(", offset=");
   181                 print(pos.offset);
   182             }
   183             print(", type_index=");
   184             print(pos.type_index);
   185             break;
   186         // We don't need to worry abut these
   187         case METHOD_RETURN_GENERIC_OR_ARRAY:
   188         case FIELD_GENERIC_OR_ARRAY:
   189             break;
   190         case UNKNOWN:
   191             break;
   192         default:
   193             throw new AssertionError("unknown type: " + pos.type);
   194         }
   196         // Append location data for generics/arrays.
   197         if (pos.type.hasLocation()) {
   198             print(", location=");
   199             print(pos.location);
   200         }
   201     }
   203     public void write(Annotation.element_value_pair pair) {
   204         write(pair, false);
   205     }
   207     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
   208         writeIndex(pair.element_name_index, resolveIndices);
   209         print("=");
   210         write(pair.value, resolveIndices);
   211     }
   213     public void write(Annotation.element_value value) {
   214         write(value, false);
   215     }
   217     public void write(Annotation.element_value value, boolean resolveIndices) {
   218         ev_writer.write(value, resolveIndices);
   219     }
   221     private void writeDescriptor(int index, boolean resolveIndices) {
   222         if (resolveIndices) {
   223             try {
   224                 ConstantPool constant_pool = classWriter.getClassFile().constant_pool;
   225                 Descriptor d = new Descriptor(index);
   226                 print(d.getFieldType(constant_pool));
   227                 return;
   228             } catch (ConstantPoolException ignore) {
   229             } catch (InvalidDescriptor ignore) {
   230             }
   231         }
   233         print("#" + index);
   234     }
   236     private void writeIndex(int index, boolean resolveIndices) {
   237         if (resolveIndices) {
   238             print(constantWriter.stringValue(index));
   239         } else
   240             print("#" + index);
   241     }
   243     element_value_Writer ev_writer = new element_value_Writer();
   245     class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
   246         public void write(Annotation.element_value value, boolean resolveIndices) {
   247             value.accept(this, resolveIndices);
   248         }
   250         public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
   251             if (resolveIndices)
   252                 writeIndex(ev.const_value_index, resolveIndices);
   253             else
   254                 print(((char) ev.tag) + "#" + ev.const_value_index);
   255             return null;
   256         }
   258         public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
   259             if (resolveIndices) {
   260                 writeIndex(ev.type_name_index, resolveIndices);
   261                 print(".");
   262                 writeIndex(ev.const_name_index, resolveIndices);
   263             } else
   264                 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
   265             return null;
   266         }
   268         public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
   269             if (resolveIndices) {
   270                 writeIndex(ev.class_info_index, resolveIndices);
   271                 print(".class");
   272             } else
   273                 print(((char) ev.tag) + "#" + ev.class_info_index);
   274             return null;
   275         }
   277         public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
   278             print((char) ev.tag);
   279             AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
   280             return null;
   281         }
   283         public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
   284             print("[");
   285             for (int i = 0; i < ev.num_values; i++) {
   286                 if (i > 0)
   287                     print(",");
   288                 write(ev.values[i], resolveIndices);
   289             }
   290             print("]");
   291             return null;
   292         }
   294     }
   296     private ClassWriter classWriter;
   297     private ConstantWriter constantWriter;
   298 }

mercurial