src/jdk.jdeps/share/classes/com/sun/tools/javap/AnnotationWriter.java
changeset 47875 93bba74ed8a3
parent 47216 71c04702a3d5
equal deleted inserted replaced
47873:7944849362f3 47875:93bba74ed8a3
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2017, 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
    59         constantWriter = ConstantWriter.instance(context);
    59         constantWriter = ConstantWriter.instance(context);
    60     }
    60     }
    61 
    61 
    62     public void write(Annotation annot) {
    62     public void write(Annotation annot) {
    63         write(annot, false);
    63         write(annot, false);
       
    64         println();
       
    65         indent(+1);
       
    66         write(annot, true);
       
    67         indent(-1);
    64     }
    68     }
    65 
    69 
    66     public void write(Annotation annot, boolean resolveIndices) {
    70     public void write(Annotation annot, boolean resolveIndices) {
    67         writeDescriptor(annot.type_index, resolveIndices);
    71         writeDescriptor(annot.type_index, resolveIndices);
    68         boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices;
    72         if (resolveIndices) {
    69         if (showParens)
    73             boolean showParens = annot.num_element_value_pairs > 0;
       
    74             if (showParens) {
       
    75                 println("(");
       
    76                 indent(+1);
       
    77             }
       
    78             for (int i = 0; i < annot.num_element_value_pairs; i++) {
       
    79                 write(annot.element_value_pairs[i], true);
       
    80                 println();
       
    81             }
       
    82             if (showParens) {
       
    83                 indent(-1);
       
    84                 print(")");
       
    85             }
       
    86         } else {
    70             print("(");
    87             print("(");
    71         for (int i = 0; i < annot.num_element_value_pairs; i++) {
    88             for (int i = 0; i < annot.num_element_value_pairs; i++) {
    72             if (i > 0)
    89                 if (i > 0)
    73                 print(",");
    90                     print(",");
    74             write(annot.element_value_pairs[i], resolveIndices);
    91                 write(annot.element_value_pairs[i], false);
    75         }
    92             }
    76         if (showParens)
       
    77             print(")");
    93             print(")");
       
    94         }
    78     }
    95     }
    79 
    96 
    80     public void write(TypeAnnotation annot) {
    97     public void write(TypeAnnotation annot) {
    81         write(annot, true, false);
    98         write(annot, true, false);
       
    99         println();
       
   100         indent(+1);
       
   101         write(annot.annotation, true);
       
   102         indent(-1);
    82     }
   103     }
    83 
   104 
    84     public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {
   105     public void write(TypeAnnotation annot, boolean showOffsets, boolean resolveIndices) {
    85         write(annot.annotation, resolveIndices);
   106         write(annot.annotation, resolveIndices);
    86         print(": ");
   107         print(": ");
   192             print(", location=");
   213             print(", location=");
   193             print(pos.location);
   214             print(pos.location);
   194         }
   215         }
   195     }
   216     }
   196 
   217 
   197     public void write(Annotation.element_value_pair pair) {
       
   198         write(pair, false);
       
   199     }
       
   200 
       
   201     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
   218     public void write(Annotation.element_value_pair pair, boolean resolveIndices) {
   202         writeIndex(pair.element_name_index, resolveIndices);
   219         writeIndex(pair.element_name_index, resolveIndices);
   203         print("=");
   220         print("=");
   204         write(pair.value, resolveIndices);
   221         write(pair.value, resolveIndices);
   205     }
   222     }
   206 
   223 
   207     public void write(Annotation.element_value value) {
   224     public void write(Annotation.element_value value) {
   208         write(value, false);
   225         write(value, false);
       
   226         println();
       
   227         indent(+1);
       
   228         write(value, true);
       
   229         indent(-1);
   209     }
   230     }
   210 
   231 
   211     public void write(Annotation.element_value value, boolean resolveIndices) {
   232     public void write(Annotation.element_value value, boolean resolveIndices) {
   212         ev_writer.write(value, resolveIndices);
   233         ev_writer.write(value, resolveIndices);
   213     }
   234     }
   238     class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
   259     class element_value_Writer implements Annotation.element_value.Visitor<Void,Boolean> {
   239         public void write(Annotation.element_value value, boolean resolveIndices) {
   260         public void write(Annotation.element_value value, boolean resolveIndices) {
   240             value.accept(this, resolveIndices);
   261             value.accept(this, resolveIndices);
   241         }
   262         }
   242 
   263 
       
   264         @Override
   243         public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
   265         public Void visitPrimitive(Primitive_element_value ev, Boolean resolveIndices) {
   244             if (resolveIndices)
   266             if (resolveIndices) {
   245                 writeIndex(ev.const_value_index, resolveIndices);
   267                 int index = ev.const_value_index;
   246             else
   268                 switch (ev.tag) {
       
   269                     case 'B':
       
   270                         print("(byte) ");
       
   271                         print(constantWriter.stringValue(index));
       
   272                         break;
       
   273                     case 'C':
       
   274                         print("'");
       
   275                         print(constantWriter.charValue(index));
       
   276                         print("'");
       
   277                         break;
       
   278                     case 'D':
       
   279                     case 'F':
       
   280                     case 'I':
       
   281                     case 'J':
       
   282                         print(constantWriter.stringValue(index));
       
   283                         break;
       
   284                     case 'S':
       
   285                         print("(short) ");
       
   286                         print(constantWriter.stringValue(index));
       
   287                         break;
       
   288                     case 'Z':
       
   289                         print(constantWriter.booleanValue(index));
       
   290                         break;
       
   291                     case 's':
       
   292                         print("\"");
       
   293                         print(constantWriter.stringValue(index));
       
   294                         print("\"");
       
   295                         break;
       
   296                     default:
       
   297                         print(((char) ev.tag) + "#" + ev.const_value_index);
       
   298                         break;
       
   299                 }
       
   300             } else {
   247                 print(((char) ev.tag) + "#" + ev.const_value_index);
   301                 print(((char) ev.tag) + "#" + ev.const_value_index);
   248             return null;
   302             }
   249         }
   303             return null;
   250 
   304         }
       
   305 
       
   306         @Override
   251         public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
   307         public Void visitEnum(Enum_element_value ev, Boolean resolveIndices) {
   252             if (resolveIndices) {
   308             if (resolveIndices) {
   253                 writeIndex(ev.type_name_index, resolveIndices);
   309                 writeIndex(ev.type_name_index, resolveIndices);
   254                 print(".");
   310                 print(".");
   255                 writeIndex(ev.const_name_index, resolveIndices);
   311                 writeIndex(ev.const_name_index, resolveIndices);
   256             } else
   312             } else {
   257                 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
   313                 print(((char) ev.tag) + "#" + ev.type_name_index + ".#" + ev.const_name_index);
   258             return null;
   314             }
   259         }
   315             return null;
   260 
   316         }
       
   317 
       
   318         @Override
   261         public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
   319         public Void visitClass(Class_element_value ev, Boolean resolveIndices) {
   262             if (resolveIndices) {
   320             if (resolveIndices) {
       
   321                 print("class ");
   263                 writeIndex(ev.class_info_index, resolveIndices);
   322                 writeIndex(ev.class_info_index, resolveIndices);
   264                 print(".class");
   323             } else {
   265             } else
       
   266                 print(((char) ev.tag) + "#" + ev.class_info_index);
   324                 print(((char) ev.tag) + "#" + ev.class_info_index);
   267             return null;
   325             }
   268         }
   326             return null;
   269 
   327         }
       
   328 
       
   329         @Override
   270         public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
   330         public Void visitAnnotation(Annotation_element_value ev, Boolean resolveIndices) {
   271             print((char) ev.tag);
   331             print((char) ev.tag);
   272             AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
   332             AnnotationWriter.this.write(ev.annotation_value, resolveIndices);
   273             return null;
   333             return null;
   274         }
   334         }
   275 
   335 
       
   336         @Override
   276         public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
   337         public Void visitArray(Array_element_value ev, Boolean resolveIndices) {
   277             print("[");
   338             print("[");
   278             for (int i = 0; i < ev.num_values; i++) {
   339             for (int i = 0; i < ev.num_values; i++) {
   279                 if (i > 0)
   340                 if (i > 0)
   280                     print(",");
   341                     print(",");
   284             return null;
   345             return null;
   285         }
   346         }
   286 
   347 
   287     }
   348     }
   288 
   349 
   289     private ClassWriter classWriter;
   350     private final ClassWriter classWriter;
   290     private ConstantWriter constantWriter;
   351     private final ConstantWriter constantWriter;
   291 }
   352 }