langtools/src/share/classes/com/sun/tools/javap/AnnotationWriter.java
changeset 15385 ee1eebe7e210
parent 8031 d5fe2c1cecfc
child 15718 8e54c8e43d38
equal deleted inserted replaced
15384:5a8d00abf076 15385:ee1eebe7e210
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.javap;
    26 package com.sun.tools.javap;
    27 
    27 
    28 import com.sun.tools.classfile.Annotation;
    28 import com.sun.tools.classfile.Annotation;
       
    29 import com.sun.tools.classfile.TypeAnnotation;
    29 import com.sun.tools.classfile.Annotation.Annotation_element_value;
    30 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.Array_element_value;
    31 import com.sun.tools.classfile.Annotation.Class_element_value;
    32 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.Enum_element_value;
    33 import com.sun.tools.classfile.Annotation.Primitive_element_value;
    34 import com.sun.tools.classfile.Annotation.Primitive_element_value;
    74         }
    75         }
    75         if (showParens)
    76         if (showParens)
    76             print(")");
    77             print(")");
    77     }
    78     }
    78 
    79 
       
    80     public void write(TypeAnnotation annot) {
       
    81         write(annot, true, false);
       
    82     }
       
    83 
       
    84     public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {
       
    85         write(annot.annotation, resolveIndices);
       
    86         print(": ");
       
    87         write(annot.position, showOffsets);
       
    88     }
       
    89 
       
    90     public void write(TypeAnnotation.Position pos, boolean showOffsets) {
       
    91         print(pos.type);
       
    92 
       
    93         switch (pos.type) {
       
    94         // type cast
       
    95         case CAST:
       
    96         // instanceof
       
    97         case INSTANCEOF:
       
    98         // new expression
       
    99         case NEW:
       
   100             if (showOffsets) {
       
   101                 print(", offset=");
       
   102                 print(pos.offset);
       
   103             }
       
   104             break;
       
   105         // local variable
       
   106         case LOCAL_VARIABLE:
       
   107         // resource variable
       
   108         case RESOURCE_VARIABLE:
       
   109             if (pos.lvarOffset == null) {
       
   110                 print(", lvarOffset is Null!");
       
   111                 break;
       
   112             }
       
   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         // exception parameter
       
   128         case EXCEPTION_PARAMETER:
       
   129             print(", exception_index=");
       
   130             print(pos.exception_index);
       
   131             break;
       
   132         // method receiver
       
   133         case METHOD_RECEIVER:
       
   134             // Do nothing
       
   135             break;
       
   136         // type parameter
       
   137         case CLASS_TYPE_PARAMETER:
       
   138         case METHOD_TYPE_PARAMETER:
       
   139             print(", param_index=");
       
   140             print(pos.parameter_index);
       
   141             break;
       
   142         // type parameter bound
       
   143         case CLASS_TYPE_PARAMETER_BOUND:
       
   144         case METHOD_TYPE_PARAMETER_BOUND:
       
   145             print(", param_index=");
       
   146             print(pos.parameter_index);
       
   147             print(", bound_index=");
       
   148             print(pos.bound_index);
       
   149             break;
       
   150         // class extends or implements clause
       
   151         case CLASS_EXTENDS:
       
   152             print(", type_index=");
       
   153             print(pos.type_index);
       
   154             break;
       
   155         // throws
       
   156         case THROWS:
       
   157             print(", type_index=");
       
   158             print(pos.type_index);
       
   159             break;
       
   160         // method parameter
       
   161         case METHOD_FORMAL_PARAMETER:
       
   162             print(", param_index=");
       
   163             print(pos.parameter_index);
       
   164             break;
       
   165         // method/constructor/reference type argument
       
   166         case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
       
   167         case METHOD_INVOCATION_TYPE_ARGUMENT:
       
   168         case METHOD_REFERENCE_TYPE_ARGUMENT:
       
   169             if (showOffsets) {
       
   170                 print(", offset=");
       
   171                 print(pos.offset);
       
   172             }
       
   173             print(", type_index=");
       
   174             print(pos.type_index);
       
   175             break;
       
   176         // We don't need to worry about these
       
   177         case METHOD_RETURN:
       
   178         case FIELD:
       
   179             break;
       
   180         // lambda formal parameter
       
   181         case LAMBDA_FORMAL_PARAMETER:
       
   182             print(", param_index=");
       
   183             print(pos.parameter_index);
       
   184             break;
       
   185         case UNKNOWN:
       
   186             throw new AssertionError("AnnotationWriter: UNKNOWN target type should never occur!");
       
   187         default:
       
   188             throw new AssertionError("AnnotationWriter: Unknown target type for position: " + pos);
       
   189         }
       
   190 
       
   191         // Append location data for generics/arrays.
       
   192         if (!pos.location.isEmpty()) {
       
   193             print(", location=");
       
   194             print(pos.location);
       
   195         }
       
   196     }
       
   197 
    79     public void write(Annotation.element_value_pair pair) {
   198     public void write(Annotation.element_value_pair pair) {
    80         write(pair, false);
   199         write(pair, false);
    81     }
   200     }
    82 
   201 
    83     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
   202     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {