< prev index next >

src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java

Print this page

   1 /*
   2  * Copyright (c) 2008, 2020, 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

 716         }
 717 
 718         @Override
 719         public Void visitSourceFile(SourceFile_attribute attr, ClassOutputStream out) {
 720             out.writeShort(attr.sourcefile_index);
 721             return null;
 722         }
 723 
 724         @Override
 725         public Void visitSourceID(SourceID_attribute attr, ClassOutputStream out) {
 726             out.writeShort(attr.sourceID_index);
 727             return null;
 728         }
 729 
 730         @Override
 731         public Void visitStackMap(StackMap_attribute attr, ClassOutputStream out) {
 732             if (stackMapWriter == null)
 733                 stackMapWriter = new StackMapTableWriter();
 734 
 735             out.writeShort(attr.entries.length);
 736             for (stack_map_frame f: attr.entries)
 737                 stackMapWriter.write(f, out);
 738             return null;
 739         }
 740 
 741         @Override
 742         public Void visitStackMapTable(StackMapTable_attribute attr, ClassOutputStream out) {
 743             if (stackMapWriter == null)
 744                 stackMapWriter = new StackMapTableWriter();
 745 
 746             out.writeShort(attr.entries.length);
 747             for (stack_map_frame f: attr.entries)
 748                 stackMapWriter.write(f, out);
 749             return null;
 750         }
 751 
 752         @Override
 753         public Void visitSynthetic(Synthetic_attribute attr, ClassOutputStream out) {
 754             return null;
 755         }
 756 








 757         protected void writeAccessFlags(AccessFlags flags, ClassOutputStream out) {
 758             out.writeShort(flags.flags);
 759         }
 760 
 761         protected StackMapTableWriter stackMapWriter;
 762     }
 763 
 764     /**
 765      * Writer for the frames of StackMap and StackMapTable attributes.
 766      */
 767     protected static class StackMapTableWriter
 768             implements stack_map_frame.Visitor<Void,ClassOutputStream> {
 769 
 770         public void write(stack_map_frame frame, ClassOutputStream out) {
 771             out.write(frame.frame_type);
 772             frame.accept(this, out);
 773         }
 774 
 775         @Override
 776         public Void visit_same_frame(same_frame frame, ClassOutputStream p) {
 777             return null;
 778         }
 779 
 780         @Override
 781         public Void visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, ClassOutputStream out) {
 782             writeVerificationTypeInfo(frame.stack[0], out);
 783             return null;
 784         }
 785 
 786         @Override
 787         public Void visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, ClassOutputStream out) {
 788             out.writeShort(frame.offset_delta);
 789             writeVerificationTypeInfo(frame.stack[0], out);
 790             return null;
 791         }

 805         @Override
 806         public Void visit_append_frame(append_frame frame, ClassOutputStream out) {
 807             out.writeShort(frame.offset_delta);
 808             for (verification_type_info l: frame.locals)
 809                 writeVerificationTypeInfo(l, out);
 810             return null;
 811         }
 812 
 813         @Override
 814         public Void visit_full_frame(full_frame frame, ClassOutputStream out) {
 815             out.writeShort(frame.offset_delta);
 816             out.writeShort(frame.locals.length);
 817             for (verification_type_info l: frame.locals)
 818                 writeVerificationTypeInfo(l, out);
 819             out.writeShort(frame.stack.length);
 820             for (verification_type_info s: frame.stack)
 821                 writeVerificationTypeInfo(s, out);
 822             return null;
 823         }
 824 









 825         protected void writeVerificationTypeInfo(verification_type_info info,
 826                 ClassOutputStream out)  {
 827             out.write(info.tag);
 828             switch (info.tag) {
 829             case ITEM_Top:
 830             case ITEM_Integer:
 831             case ITEM_Float:
 832             case ITEM_Long:
 833             case ITEM_Double:
 834             case ITEM_Null:
 835             case ITEM_UninitializedThis:
 836                 break;
 837 
 838             case ITEM_Object:
 839                 Object_variable_info o = (Object_variable_info) info;
 840                 out.writeShort(o.cpool_index);
 841                 break;
 842 
 843             case ITEM_Uninitialized:
 844                 Uninitialized_variable_info u = (Uninitialized_variable_info) info;

   1 /*
   2  * Copyright (c) 2008, 2025, 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

 716         }
 717 
 718         @Override
 719         public Void visitSourceFile(SourceFile_attribute attr, ClassOutputStream out) {
 720             out.writeShort(attr.sourcefile_index);
 721             return null;
 722         }
 723 
 724         @Override
 725         public Void visitSourceID(SourceID_attribute attr, ClassOutputStream out) {
 726             out.writeShort(attr.sourceID_index);
 727             return null;
 728         }
 729 
 730         @Override
 731         public Void visitStackMap(StackMap_attribute attr, ClassOutputStream out) {
 732             if (stackMapWriter == null)
 733                 stackMapWriter = new StackMapTableWriter();
 734 
 735             out.writeShort(attr.entries.length);
 736             for (stack_map_entry f: attr.entries)
 737                 stackMapWriter.write(f, out);
 738             return null;
 739         }
 740 
 741         @Override
 742         public Void visitStackMapTable(StackMapTable_attribute attr, ClassOutputStream out) {
 743             if (stackMapWriter == null)
 744                 stackMapWriter = new StackMapTableWriter();
 745 
 746             out.writeShort(attr.entries.length);
 747             for (stack_map_entry f: attr.entries)
 748                 stackMapWriter.write(f, out);
 749             return null;
 750         }
 751 
 752         @Override
 753         public Void visitSynthetic(Synthetic_attribute attr, ClassOutputStream out) {
 754             return null;
 755         }
 756 
 757         @Override
 758         public Void visitLoadableDescriptors(LoadableDescriptors_attribute attr, ClassOutputStream out) {
 759             out.writeShort(attr.descriptors.length);
 760             for (int index: attr.descriptors)
 761                 out.writeShort(index);
 762             return null;
 763         }
 764 
 765         protected void writeAccessFlags(AccessFlags flags, ClassOutputStream out) {
 766             out.writeShort(flags.flags);
 767         }
 768 
 769         protected StackMapTableWriter stackMapWriter;
 770     }
 771 
 772     /**
 773      * Writer for the frames of StackMap and StackMapTable attributes.
 774      */
 775     protected static class StackMapTableWriter
 776             implements stack_map_entry.Visitor<Void,ClassOutputStream> {
 777 
 778         public void write(stack_map_entry frame, ClassOutputStream out) {
 779             out.write(frame.entry_type);
 780             frame.accept(this, out);
 781         }
 782 
 783         @Override
 784         public Void visit_same_frame(same_frame frame, ClassOutputStream p) {
 785             return null;
 786         }
 787 
 788         @Override
 789         public Void visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, ClassOutputStream out) {
 790             writeVerificationTypeInfo(frame.stack[0], out);
 791             return null;
 792         }
 793 
 794         @Override
 795         public Void visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, ClassOutputStream out) {
 796             out.writeShort(frame.offset_delta);
 797             writeVerificationTypeInfo(frame.stack[0], out);
 798             return null;
 799         }

 813         @Override
 814         public Void visit_append_frame(append_frame frame, ClassOutputStream out) {
 815             out.writeShort(frame.offset_delta);
 816             for (verification_type_info l: frame.locals)
 817                 writeVerificationTypeInfo(l, out);
 818             return null;
 819         }
 820 
 821         @Override
 822         public Void visit_full_frame(full_frame frame, ClassOutputStream out) {
 823             out.writeShort(frame.offset_delta);
 824             out.writeShort(frame.locals.length);
 825             for (verification_type_info l: frame.locals)
 826                 writeVerificationTypeInfo(l, out);
 827             out.writeShort(frame.stack.length);
 828             for (verification_type_info s: frame.stack)
 829                 writeVerificationTypeInfo(s, out);
 830             return null;
 831         }
 832 
 833         @Override
 834         public Void visit_assert_unset_fields(assert_unset_fields frame, ClassOutputStream out) {
 835             out.writeShort(frame.number_of_unset_fields);
 836             for (int uf: frame.unset_fields) {
 837                 out.writeShort(uf);
 838             }
 839             return null;
 840         }
 841 
 842         protected void writeVerificationTypeInfo(verification_type_info info,
 843                 ClassOutputStream out)  {
 844             out.write(info.tag);
 845             switch (info.tag) {
 846             case ITEM_Top:
 847             case ITEM_Integer:
 848             case ITEM_Float:
 849             case ITEM_Long:
 850             case ITEM_Double:
 851             case ITEM_Null:
 852             case ITEM_UninitializedThis:
 853                 break;
 854 
 855             case ITEM_Object:
 856                 Object_variable_info o = (Object_variable_info) info;
 857                 out.writeShort(o.cpool_index);
 858                 break;
 859 
 860             case ITEM_Uninitialized:
 861                 Uninitialized_variable_info u = (Uninitialized_variable_info) info;
< prev index next >