< prev index next >

src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java

Print this page




   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  */
  25 
  26 package com.sun.tools.javap;
  27 


  28 import com.sun.tools.classfile.AccessFlags;
  29 import com.sun.tools.classfile.AnnotationDefault_attribute;
  30 import com.sun.tools.classfile.Attribute;
  31 import com.sun.tools.classfile.Attributes;
  32 import com.sun.tools.classfile.BootstrapMethods_attribute;
  33 import com.sun.tools.classfile.CharacterRangeTable_attribute;
  34 import com.sun.tools.classfile.CharacterRangeTable_attribute.Entry;
  35 import com.sun.tools.classfile.Code_attribute;
  36 import com.sun.tools.classfile.CompilationID_attribute;
  37 import com.sun.tools.classfile.ConstantPool;
  38 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
  39 import com.sun.tools.classfile.ConstantPoolException;
  40 import com.sun.tools.classfile.ConstantValue_attribute;
  41 import com.sun.tools.classfile.DefaultAttribute;
  42 import com.sun.tools.classfile.Deprecated_attribute;


  43 import com.sun.tools.classfile.EnclosingMethod_attribute;
  44 import com.sun.tools.classfile.Exceptions_attribute;
  45 import com.sun.tools.classfile.InnerClasses_attribute;
  46 import com.sun.tools.classfile.InnerClasses_attribute.Info;
  47 import com.sun.tools.classfile.LineNumberTable_attribute;
  48 import com.sun.tools.classfile.LocalVariableTable_attribute;
  49 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
  50 import com.sun.tools.classfile.MethodParameters_attribute;
  51 import com.sun.tools.classfile.Module_attribute;
  52 import com.sun.tools.classfile.ModuleHashes_attribute;
  53 import com.sun.tools.classfile.ModuleMainClass_attribute;
  54 import com.sun.tools.classfile.ModulePackages_attribute;
  55 import com.sun.tools.classfile.ModuleResolution_attribute;
  56 import com.sun.tools.classfile.ModuleTarget_attribute;
  57 import com.sun.tools.classfile.NestHost_attribute;
  58 import com.sun.tools.classfile.NestMembers_attribute;

  59 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
  60 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
  61 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
  62 import com.sun.tools.classfile.RuntimeParameterAnnotations_attribute;
  63 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
  64 import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute;
  65 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;


  66 import com.sun.tools.classfile.Signature_attribute;
  67 import com.sun.tools.classfile.SourceDebugExtension_attribute;
  68 import com.sun.tools.classfile.SourceFile_attribute;
  69 import com.sun.tools.classfile.SourceID_attribute;
  70 import com.sun.tools.classfile.StackMapTable_attribute;
  71 import com.sun.tools.classfile.StackMap_attribute;
  72 import com.sun.tools.classfile.Synthetic_attribute;

  73 
  74 import static com.sun.tools.classfile.AccessFlags.*;
  75 
  76 import com.sun.tools.javac.util.Assert;
  77 import com.sun.tools.javac.util.StringUtils;
  78 




  79 /*
  80  *  A writer for writing Attributes as text.
  81  *
  82  *  <p><b>This is NOT part of any supported API.
  83  *  If you write code that depends on this, you do so at your own risk.
  84  *  This code and its internal interfaces are subject to change or
  85  *  deletion without notice.</b>
  86  */
  87 public class AttributeWriter extends BasicWriter
  88         implements Attribute.Visitor<Void,Void>
  89 {
  90     public static AttributeWriter instance(Context context) {
  91         AttributeWriter instance = context.get(AttributeWriter.class);
  92         if (instance == null)
  93             instance = new AttributeWriter(context);
  94         return instance;
  95     }
  96 
  97     protected AttributeWriter(Context context) {
  98         super(context);


 697         }
 698     }
 699 
 700     @Override
 701     public Void visitNestMembers(NestMembers_attribute attr, Void aVoid) {
 702         println("NestMembers:");
 703         indent(+1);
 704         try {
 705             CONSTANT_Class_info[] children = attr.getChildren(constant_pool);
 706             for (int i = 0; i < attr.members_indexes.length; i++) {
 707                 println(constantWriter.stringValue(children[i]));
 708             }
 709             indent(-1);
 710         } catch (ConstantPoolException ex) {
 711             throw new AssertionError(ex);
 712         }
 713         return null;
 714     }
 715 
 716     @Override







































 717     public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
 718         println("RuntimeVisibleAnnotations:");
 719         indent(+1);
 720         for (int i = 0; i < attr.annotations.length; i++) {
 721             print(i + ": ");
 722             annotationWriter.write(attr.annotations[i]);
 723             println();
 724         }
 725         indent(-1);
 726         return null;
 727     }
 728 
 729     @Override
 730     public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
 731         println("RuntimeInvisibleAnnotations:");
 732         indent(+1);
 733         for (int i = 0; i < attr.annotations.length; i++) {
 734             print(i + ": ");
 735             annotationWriter.write(attr.annotations[i]);
 736             println();


 773             indent(+1);
 774             for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
 775                 print(i + ": ");
 776                 annotationWriter.write(attr.parameter_annotations[param][i]);
 777                 println();
 778             }
 779             indent(-1);
 780         }
 781         indent(-1);
 782     }
 783 
 784     @Override
 785     public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
 786         visitParameterAnnotations("RuntimeVisibleParameterAnnotations:", (RuntimeParameterAnnotations_attribute) attr);
 787         return null;
 788     }
 789 
 790     @Override
 791     public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
 792         visitParameterAnnotations("RuntimeInvisibleParameterAnnotations:", (RuntimeParameterAnnotations_attribute) attr);
















 793         return null;
 794     }
 795 
 796     @Override
 797     public Void visitSignature(Signature_attribute attr, Void ignore) {
 798         print("Signature: #" + attr.signature_index);
 799         tab();
 800         println("// " + getSignature(attr));
 801         return null;
 802     }
 803 
 804     String getSignature(Signature_attribute info) {
 805         try {
 806             return info.getSignature(constant_pool);
 807         } catch (ConstantPoolException e) {
 808             return report(e);
 809         }
 810     }
 811 
 812     @Override




   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  */
  25 
  26 package com.sun.tools.javap;
  27 
  28 import java.util.Collection;
  29 
  30 import com.sun.tools.classfile.AccessFlags;
  31 import com.sun.tools.classfile.AnnotationDefault_attribute;
  32 import com.sun.tools.classfile.Attribute;
  33 import com.sun.tools.classfile.Attributes;
  34 import com.sun.tools.classfile.BootstrapMethods_attribute;
  35 import com.sun.tools.classfile.CharacterRangeTable_attribute;
  36 import com.sun.tools.classfile.CharacterRangeTable_attribute.Entry;
  37 import com.sun.tools.classfile.Code_attribute;
  38 import com.sun.tools.classfile.CompilationID_attribute;
  39 import com.sun.tools.classfile.ConstantPool;
  40 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
  41 import com.sun.tools.classfile.ConstantPoolException;
  42 import com.sun.tools.classfile.ConstantValue_attribute;
  43 import com.sun.tools.classfile.DefaultAttribute;
  44 import com.sun.tools.classfile.Deprecated_attribute;
  45 import com.sun.tools.classfile.Descriptor;
  46 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
  47 import com.sun.tools.classfile.EnclosingMethod_attribute;
  48 import com.sun.tools.classfile.Exceptions_attribute;
  49 import com.sun.tools.classfile.InnerClasses_attribute;
  50 import com.sun.tools.classfile.InnerClasses_attribute.Info;
  51 import com.sun.tools.classfile.LineNumberTable_attribute;
  52 import com.sun.tools.classfile.LocalVariableTable_attribute;
  53 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
  54 import com.sun.tools.classfile.MethodParameters_attribute;
  55 import com.sun.tools.classfile.Module_attribute;
  56 import com.sun.tools.classfile.ModuleHashes_attribute;
  57 import com.sun.tools.classfile.ModuleMainClass_attribute;
  58 import com.sun.tools.classfile.ModulePackages_attribute;
  59 import com.sun.tools.classfile.ModuleResolution_attribute;
  60 import com.sun.tools.classfile.ModuleTarget_attribute;
  61 import com.sun.tools.classfile.NestHost_attribute;
  62 import com.sun.tools.classfile.NestMembers_attribute;
  63 import com.sun.tools.classfile.Record_attribute;
  64 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
  65 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
  66 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
  67 import com.sun.tools.classfile.RuntimeParameterAnnotations_attribute;
  68 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
  69 import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute;
  70 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
  71 import com.sun.tools.classfile.PermittedSubtypes_attribute;
  72 import com.sun.tools.classfile.Signature;
  73 import com.sun.tools.classfile.Signature_attribute;
  74 import com.sun.tools.classfile.SourceDebugExtension_attribute;
  75 import com.sun.tools.classfile.SourceFile_attribute;
  76 import com.sun.tools.classfile.SourceID_attribute;
  77 import com.sun.tools.classfile.StackMapTable_attribute;
  78 import com.sun.tools.classfile.StackMap_attribute;
  79 import com.sun.tools.classfile.Synthetic_attribute;
  80 import com.sun.tools.classfile.Type;
  81 
  82 import static com.sun.tools.classfile.AccessFlags.*;
  83 
  84 import com.sun.tools.javac.util.Assert;
  85 import com.sun.tools.javac.util.StringUtils;
  86 
  87 import java.util.stream.Collectors;
  88 import java.util.stream.IntStream;
  89 import java.util.stream.Stream;
  90 
  91 /*
  92  *  A writer for writing Attributes as text.
  93  *
  94  *  <p><b>This is NOT part of any supported API.
  95  *  If you write code that depends on this, you do so at your own risk.
  96  *  This code and its internal interfaces are subject to change or
  97  *  deletion without notice.</b>
  98  */
  99 public class AttributeWriter extends BasicWriter
 100         implements Attribute.Visitor<Void,Void>
 101 {
 102     public static AttributeWriter instance(Context context) {
 103         AttributeWriter instance = context.get(AttributeWriter.class);
 104         if (instance == null)
 105             instance = new AttributeWriter(context);
 106         return instance;
 107     }
 108 
 109     protected AttributeWriter(Context context) {
 110         super(context);


 709         }
 710     }
 711 
 712     @Override
 713     public Void visitNestMembers(NestMembers_attribute attr, Void aVoid) {
 714         println("NestMembers:");
 715         indent(+1);
 716         try {
 717             CONSTANT_Class_info[] children = attr.getChildren(constant_pool);
 718             for (int i = 0; i < attr.members_indexes.length; i++) {
 719                 println(constantWriter.stringValue(children[i]));
 720             }
 721             indent(-1);
 722         } catch (ConstantPoolException ex) {
 723             throw new AssertionError(ex);
 724         }
 725         return null;
 726     }
 727 
 728     @Override
 729     public Void visitRecord(Record_attribute attr, Void p) {
 730         println("Record:");
 731         indent(+1);
 732         for (int i = 0; i < attr.num_params; i++) {
 733             println("#" + attr.accessors[i]);
 734         }
 735         indent(-1);
 736         return null;
 737     }
 738 
 739     void writeList(String prefix, Collection<?> items, String suffix) {
 740         print(prefix);
 741         String sep = "";
 742         for (Object item: items) {
 743             print(sep);
 744             print(item);
 745             sep = ", ";
 746         }
 747         print(suffix);
 748     }
 749 
 750     String getJavaFieldType(Descriptor d) {
 751         try {
 752             return getJavaName(d.getFieldType(constant_pool));
 753         } catch (ConstantPoolException e) {
 754             return report(e);
 755         } catch (InvalidDescriptor e) {
 756             return report(e);
 757         }
 758     }
 759 
 760     void writeModifiers(Collection<String> items) {
 761         for (Object item: items) {
 762             print(item);
 763             print(" ");
 764         }
 765     }
 766 
 767     @Override
 768     public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
 769         println("RuntimeVisibleAnnotations:");
 770         indent(+1);
 771         for (int i = 0; i < attr.annotations.length; i++) {
 772             print(i + ": ");
 773             annotationWriter.write(attr.annotations[i]);
 774             println();
 775         }
 776         indent(-1);
 777         return null;
 778     }
 779 
 780     @Override
 781     public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
 782         println("RuntimeInvisibleAnnotations:");
 783         indent(+1);
 784         for (int i = 0; i < attr.annotations.length; i++) {
 785             print(i + ": ");
 786             annotationWriter.write(attr.annotations[i]);
 787             println();


 824             indent(+1);
 825             for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
 826                 print(i + ": ");
 827                 annotationWriter.write(attr.parameter_annotations[param][i]);
 828                 println();
 829             }
 830             indent(-1);
 831         }
 832         indent(-1);
 833     }
 834 
 835     @Override
 836     public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
 837         visitParameterAnnotations("RuntimeVisibleParameterAnnotations:", (RuntimeParameterAnnotations_attribute) attr);
 838         return null;
 839     }
 840 
 841     @Override
 842     public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
 843         visitParameterAnnotations("RuntimeInvisibleParameterAnnotations:", (RuntimeParameterAnnotations_attribute) attr);
 844         return null;
 845     }
 846 
 847     @Override
 848     public Void visitPermittedSubtypes(PermittedSubtypes_attribute attr, Void ignore) {
 849         println("PermittedSubtypes:");
 850         indent(+1);
 851         try {
 852             CONSTANT_Class_info[] subtypes = attr.getSubtypes(constant_pool);
 853             for (int i = 0; i < subtypes.length; i++) {
 854                 println(constantWriter.stringValue(subtypes[i]));
 855             }
 856             indent(-1);
 857         } catch (ConstantPoolException ex) {
 858             throw new AssertionError(ex);
 859         }
 860         return null;
 861     }
 862 
 863     @Override
 864     public Void visitSignature(Signature_attribute attr, Void ignore) {
 865         print("Signature: #" + attr.signature_index);
 866         tab();
 867         println("// " + getSignature(attr));
 868         return null;
 869     }
 870 
 871     String getSignature(Signature_attribute info) {
 872         try {
 873             return info.getSignature(constant_pool);
 874         } catch (ConstantPoolException e) {
 875             return report(e);
 876         }
 877     }
 878 
 879     @Override


< prev index next >