View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import org.apache.bcel.Const;
24  
25  /**
26   * This class represents a stack map entry recording the types of
27   * local variables and the the of stack items at a given byte code offset.
28   * See CLDC specification 5.3.1.2
29   *
30   * @see     StackMap
31   * @see     StackMapType
32   */
33  public final class StackMapEntry implements Node, Cloneable
34  {
35  
36      private int frameType;
37      private int byteCodeOffset;
38      private StackMapType[] typesOfLocals;
39      private StackMapType[] typesOfStackItems;
40      private ConstantPool constantPool;
41  
42  
43      /**
44       * Construct object from input stream.
45       *
46       * @param input Input stream
47       * @throws IOException
48       */
49      StackMapEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
50          this(input.readByte() & 0xFF, -1, null, null, constantPool);
51  
52          if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
53              byteCodeOffset = frameType - Const.SAME_FRAME;
54          } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
55                     frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
56              byteCodeOffset = frameType - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
57              typesOfStackItems = new StackMapType[1];
58              typesOfStackItems[0] = new StackMapType(input, constantPool);
59          } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
60              byteCodeOffset = input.readShort();
61              typesOfStackItems = new StackMapType[1];
62              typesOfStackItems[0] = new StackMapType(input, constantPool);
63          } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
64              byteCodeOffset = input.readShort();
65          } else if (frameType == Const.SAME_FRAME_EXTENDED) {
66              byteCodeOffset = input.readShort();
67          } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
68              byteCodeOffset = input.readShort();
69              final int number_of_locals = frameType - 251;
70              typesOfLocals = new StackMapType[number_of_locals];
71              for (int i = 0; i < number_of_locals; i++) {
72                  typesOfLocals[i] = new StackMapType(input, constantPool);
73              }
74          } else if (frameType == Const.FULL_FRAME) {
75              byteCodeOffset = input.readShort();
76              final int number_of_locals = input.readShort();
77              typesOfLocals = new StackMapType[number_of_locals];
78              for (int i = 0; i < number_of_locals; i++) {
79                  typesOfLocals[i] = new StackMapType(input, constantPool);
80              }
81              final int number_of_stack_items = input.readShort();
82              typesOfStackItems = new StackMapType[number_of_stack_items];
83              for (int i = 0; i < number_of_stack_items; i++) {
84                  typesOfStackItems[i] = new StackMapType(input, constantPool);
85              }
86          } else {
87              /* Can't happen */
88              throw new ClassFormatException ("Invalid frame type found while parsing stack map table: " + frameType);
89          }
90      }
91  
92      /**
93       * DO NOT USE
94       *
95       * @param byteCodeOffset
96       * @param numberOfLocals NOT USED
97       * @param typesOfLocals array of {@link StackMapType}s of locals
98       * @param numberOfStackItems NOT USED
99       * @param typesOfStackItems array ot {@link StackMapType}s of stack items
100      * @param constantPool the constant pool
101      * @deprecated Since 6.0, use {@link #StackMapEntry(int, int, StackMapType[], StackMapType[], ConstantPool)}
102      * instead
103      */
104     @java.lang.Deprecated
105     public StackMapEntry(final int byteCodeOffset, final int numberOfLocals,
106             final StackMapType[] typesOfLocals, final int numberOfStackItems,
107             final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
108         this.byteCodeOffset = byteCodeOffset;
109         this.typesOfLocals = typesOfLocals != null ? typesOfLocals : new StackMapType[0];
110         this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : new StackMapType[0];
111         this.constantPool = constantPool;
112     }
113 
114     /**
115      * Create an instance
116      *
117      * @param tag the frameType to use
118      * @param byteCodeOffset
119      * @param typesOfLocals array of {@link StackMapType}s of locals
120      * @param typesOfStackItems array ot {@link StackMapType}s of stack items
121      * @param constantPool the constant pool
122      */
123     public StackMapEntry(final int tag, final int byteCodeOffset,
124             final StackMapType[] typesOfLocals,
125             final StackMapType[] typesOfStackItems, final ConstantPool constantPool) {
126         this.frameType = tag;
127         this.byteCodeOffset = byteCodeOffset;
128         this.typesOfLocals = typesOfLocals != null ? typesOfLocals : new StackMapType[0];
129         this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : new StackMapType[0];
130         this.constantPool = constantPool;
131     }
132 
133 
134     /**
135      * Dump stack map entry
136      *
137      * @param file Output file stream
138      * @throws IOException
139      */
140     public void dump( final DataOutputStream file ) throws IOException {
141         file.write(frameType);
142         if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
143             // nothing to be done
144         } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
145                    frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
146             typesOfStackItems[0].dump(file);
147         } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
148             file.writeShort(byteCodeOffset);
149             typesOfStackItems[0].dump(file);
150         } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
151             file.writeShort(byteCodeOffset);
152         } else if (frameType == Const.SAME_FRAME_EXTENDED) {
153             file.writeShort(byteCodeOffset);
154         } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
155             file.writeShort(byteCodeOffset);
156             for (final StackMapType type : typesOfLocals) {
157                 type.dump(file);
158             }
159         } else if (frameType == Const.FULL_FRAME) {
160             file.writeShort(byteCodeOffset);
161             file.writeShort(typesOfLocals.length);
162             for (final StackMapType type : typesOfLocals) {
163                 type.dump(file);
164             }
165             file.writeShort(typesOfStackItems.length);
166             for (final StackMapType type : typesOfStackItems) {
167                 type.dump(file);
168             }
169         } else {
170             /* Can't happen */
171             throw new ClassFormatException ("Invalid Stack map table tag: " + frameType);
172         }
173     }
174 
175 
176     /**
177      * @return String representation.
178      */
179     @Override
180     public String toString() {
181         final StringBuilder buf = new StringBuilder(64);
182         buf.append("(");
183         if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
184             buf.append("SAME");
185         } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
186                   frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
187             buf.append("SAME_LOCALS_1_STACK");
188         } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
189             buf.append("SAME_LOCALS_1_STACK_EXTENDED");
190         } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
191             buf.append("CHOP ").append(String.valueOf(251-frameType));
192         } else if (frameType == Const.SAME_FRAME_EXTENDED) {
193             buf.append("SAME_EXTENDED");
194         } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
195             buf.append("APPEND ").append(String.valueOf(frameType-251));
196         } else if (frameType == Const.FULL_FRAME) {
197             buf.append("FULL");
198         } else {
199             buf.append("UNKNOWN (").append(frameType).append(")");
200         }
201         buf.append(", offset delta=").append(byteCodeOffset);
202         if (typesOfLocals.length > 0) {
203             buf.append(", locals={");
204             for (int i = 0; i < typesOfLocals.length; i++) {
205                 buf.append(typesOfLocals[i]);
206                 if (i < typesOfLocals.length - 1) {
207                     buf.append(", ");
208                 }
209             }
210             buf.append("}");
211         }
212         if (typesOfStackItems.length > 0) {
213             buf.append(", stack items={");
214             for (int i = 0; i < typesOfStackItems.length; i++) {
215                 buf.append(typesOfStackItems[i]);
216                 if (i < typesOfStackItems.length - 1) {
217                     buf.append(", ");
218                 }
219             }
220             buf.append("}");
221         }
222         buf.append(")");
223         return buf.toString();
224     }
225 
226 
227     /**
228      * Calculate stack map entry size
229      *
230      */
231     int getMapEntrySize() {
232         if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) {
233             return 1;
234         } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
235                    frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
236             return 1 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
237         } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) {
238             return 3 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
239         } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) {
240             return 3;
241         } else if (frameType == Const.SAME_FRAME_EXTENDED) {
242             return 3;
243         } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) {
244             int len = 3;
245             for (final StackMapType types_of_local : typesOfLocals) {
246                 len += types_of_local.hasIndex() ? 3 : 1;
247             }
248             return len;
249         } else if (frameType == Const.FULL_FRAME) {
250             int len = 7;
251             for (final StackMapType types_of_local : typesOfLocals) {
252                 len += types_of_local.hasIndex() ? 3 : 1;
253             }
254             for (final StackMapType types_of_stack_item : typesOfStackItems) {
255                 len += types_of_stack_item.hasIndex() ? 3 : 1;
256             }
257             return len;
258         } else {
259             throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
260         }
261     }
262 
263 
264     public void setFrameType( final int f ) {
265         if (f >= Const.SAME_FRAME && f <= Const.SAME_FRAME_MAX) {
266             byteCodeOffset = f - Const.SAME_FRAME;
267         } else if (f >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
268                    f <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
269             byteCodeOffset = f - Const.SAME_LOCALS_1_STACK_ITEM_FRAME;
270         } else if (f == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
271         } else if (f >= Const.CHOP_FRAME && f <= Const.CHOP_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
272         } else if (f == Const.SAME_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
273         } else if (f >= Const.APPEND_FRAME && f <= Const.APPEND_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
274         } else if (f == Const.FULL_FRAME) { // CHECKSTYLE IGNORE EmptyBlock
275         } else {
276             throw new IllegalArgumentException("Invalid StackMap frameType");
277         }
278         frameType = f;
279     }
280 
281 
282     public int getFrameType() {
283         return frameType;
284     }
285 
286 
287     public void setByteCodeOffset( final int new_offset ) {
288         if (new_offset < 0 || new_offset > 32767) {
289             throw new IllegalArgumentException("Invalid StackMap offset: " + new_offset);
290         }
291 
292         if (frameType >= Const.SAME_FRAME &&
293             frameType <= Const.SAME_FRAME_MAX) {
294             if (new_offset > Const.SAME_FRAME_MAX) {
295                 frameType = Const.SAME_FRAME_EXTENDED;
296             } else {
297                 frameType = new_offset;
298             }
299         } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME &&
300                    frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) {
301             if (new_offset > Const.SAME_FRAME_MAX) {
302                 frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED;
303             } else {
304                 frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME + new_offset;
305             }
306         } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
307         } else if (frameType >= Const.CHOP_FRAME &&
308                    frameType <= Const.CHOP_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
309         } else if (frameType == Const.SAME_FRAME_EXTENDED) { // CHECKSTYLE IGNORE EmptyBlock
310         } else if (frameType >= Const.APPEND_FRAME &&
311                    frameType <= Const.APPEND_FRAME_MAX) { // CHECKSTYLE IGNORE EmptyBlock
312         } else if (frameType == Const.FULL_FRAME) { // CHECKSTYLE IGNORE EmptyBlock
313         } else {
314             throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
315         }
316         byteCodeOffset = new_offset;
317     }
318 
319 
320     /**
321      * Update the distance (as an offset delta) from this StackMap
322      * entry to the next.  Note that this might cause the the
323      * frame type to change.  Note also that delta may be negative.
324      *
325      * @param delta offset delta
326      */
327     public void updateByteCodeOffset(final int delta) {
328         setByteCodeOffset(byteCodeOffset + delta);
329     }
330 
331 
332     public int getByteCodeOffset() {
333         return byteCodeOffset;
334     }
335 
336 
337     /**
338      *
339      * @deprecated since 6.0
340      */
341     @java.lang.Deprecated
342     public void setNumberOfLocals( final int n ) { // TODO unused
343     }
344 
345 
346     public int getNumberOfLocals() {
347         return typesOfLocals.length;
348     }
349 
350 
351     public void setTypesOfLocals( final StackMapType[] types ) {
352         typesOfLocals = types != null ? types : new StackMapType[0];
353     }
354 
355 
356     public StackMapType[] getTypesOfLocals() {
357         return typesOfLocals;
358     }
359 
360 
361     /**
362      *
363      * @deprecated since 6.0
364      */
365     @java.lang.Deprecated
366     public void setNumberOfStackItems( final int n ) { // TODO unused
367     }
368 
369 
370     public int getNumberOfStackItems() {
371         return typesOfStackItems.length;
372     }
373 
374 
375     public void setTypesOfStackItems( final StackMapType[] types ) {
376         typesOfStackItems = types != null ? types : new StackMapType[0];
377     }
378 
379 
380     public StackMapType[] getTypesOfStackItems() {
381         return typesOfStackItems;
382     }
383 
384 
385     /**
386      * @return deep copy of this object
387      */
388     public StackMapEntry copy() {
389         StackMapEntry e;
390         try {
391             e = (StackMapEntry) clone();
392         } catch (final CloneNotSupportedException ex) {
393             throw new Error("Clone Not Supported");
394         }
395 
396         e.typesOfLocals = new StackMapType[typesOfLocals.length];
397         for (int i = 0; i < typesOfLocals.length; i++) {
398             e.typesOfLocals[i] = typesOfLocals[i].copy();
399         }
400         e.typesOfStackItems = new StackMapType[typesOfStackItems.length];
401         for (int i = 0; i < typesOfStackItems.length; i++) {
402             e.typesOfStackItems[i] = typesOfStackItems[i].copy();
403         }
404         return e;
405     }
406 
407 
408     /**
409      * Called by objects that are traversing the nodes of the tree implicitely
410      * defined by the contents of a Java class. I.e., the hierarchy of methods,
411      * fields, attributes, etc. spawns a tree of objects.
412      *
413      * @param v Visitor object
414      */
415     @Override
416     public void accept( final Visitor v ) {
417         v.visitStackMapEntry(this);
418     }
419 
420 
421     /**
422      * @return Constant pool used by this object.
423      */
424     public ConstantPool getConstantPool() {
425         return constantPool;
426     }
427 
428 
429     /**
430      * @param constantPool Constant pool to be used for this object.
431      */
432     public void setConstantPool( final ConstantPool constantPool ) {
433         this.constantPool = constantPool;
434     }
435 }