< 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)


 694             return constant_pool.getUTF8Value(attr.target_platform_index);
 695         } catch (ConstantPoolException e) {
 696             return report(e);
 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++) {




   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.Record_attribute.Param_data;
  65 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
  66 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
  67 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
  68 import com.sun.tools.classfile.RuntimeParameterAnnotations_attribute;
  69 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
  70 import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute;
  71 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_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 /*
  88  *  A writer for writing Attributes as text.
  89  *
  90  *  <p><b>This is NOT part of any supported API.
  91  *  If you write code that depends on this, you do so at your own risk.
  92  *  This code and its internal interfaces are subject to change or
  93  *  deletion without notice.</b>
  94  */
  95 public class AttributeWriter extends BasicWriter
  96         implements Attribute.Visitor<Void,Void>
  97 {
  98     public static AttributeWriter instance(Context context) {
  99         AttributeWriter instance = context.get(AttributeWriter.class);
 100         if (instance == null)


 702             return constant_pool.getUTF8Value(attr.target_platform_index);
 703         } catch (ConstantPoolException e) {
 704             return report(e);
 705         }
 706     }
 707 
 708     @Override
 709     public Void visitNestMembers(NestMembers_attribute attr, Void aVoid) {
 710         println("NestMembers:");
 711         indent(+1);
 712         try {
 713             CONSTANT_Class_info[] children = attr.getChildren(constant_pool);
 714             for (int i = 0; i < attr.members_indexes.length; i++) {
 715                 println(constantWriter.stringValue(children[i]));
 716             }
 717             indent(-1);
 718         } catch (ConstantPoolException ex) {
 719             throw new AssertionError(ex);
 720         }
 721         return null;
 722     }
 723 
 724     @Override
 725     public Void visitRecord(Record_attribute attr, Void p) {
 726         println("Record:");
 727         indent(+1);
 728         for (int i = 0; i < attr.num_params; i++) {
 729             writeParamData(attr.params[i]);
 730         }
 731         write(attr, attr.attributes, constant_pool);
 732         indent(-1);
 733         return null;
 734     }
 735 
 736     void writeParamData(Param_data pd) {
 737         AccessFlags flags = new AccessFlags(pd.param_flags);
 738         writeModifiers(flags.getFieldModifiers());
 739         Descriptor descriptor = new Descriptor(pd.param_descriptor);
 740         print(getJavaFieldType(descriptor));
 741         print(" ");
 742         try {
 743             print(pd.getName(constant_pool));
 744         } catch (ConstantPoolException cpe) {
 745             // ignore
 746         }
 747         println(";");
 748         try {
 749             indent(+1);
 750             println("descriptor: " + descriptor.getValue(constant_pool));
 751             writeList(String.format("flags: (0x%04x) ", flags.flags), flags.getFieldFlags(), "\n");
 752             Signature signature = new Signature(pd.param_signature);
 753             Type t = signature.getType(constant_pool);
 754             println("signature: " + getJavaName(t.toString()));
 755             indent(-1);
 756         } catch (ConstantPoolException cpe) {
 757             // ignore
 758         }
 759         println();
 760     }
 761 
 762     void writeList(String prefix, Collection<?> items, String suffix) {
 763         print(prefix);
 764         String sep = "";
 765         for (Object item: items) {
 766             print(sep);
 767             print(item);
 768             sep = ", ";
 769         }
 770         print(suffix);
 771     }
 772 
 773     String getJavaFieldType(Descriptor d) {
 774         try {
 775             return getJavaName(d.getFieldType(constant_pool));
 776         } catch (ConstantPoolException e) {
 777             return report(e);
 778         } catch (InvalidDescriptor e) {
 779             return report(e);
 780         }
 781     }
 782 
 783     void writeModifiers(Collection<String> items) {
 784         for (Object item: items) {
 785             print(item);
 786             print(" ");
 787         }
 788     }
 789 
 790     @Override
 791     public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
 792         println("RuntimeVisibleAnnotations:");
 793         indent(+1);
 794         for (int i = 0; i < attr.annotations.length; i++) {
 795             print(i + ": ");
 796             annotationWriter.write(attr.annotations[i]);
 797             println();
 798         }
 799         indent(-1);
 800         return null;
 801     }
 802 
 803     @Override
 804     public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
 805         println("RuntimeInvisibleAnnotations:");
 806         indent(+1);
 807         for (int i = 0; i < attr.annotations.length; i++) {


< prev index next >