/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.bcel.classfile;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;
import org.apache.bcel.Const;
import org.apache.bcel.util.Args;
/**
* This class represents a stack map entry recording the types of local variables and the of stack items at a given
* byte code offset. See CLDC specification 5.3.1.2.
*
* See also https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.4
*
* <pre>
* union stack_map_frame {
* same_frame;
* same_locals_1_stack_item_frame;
* same_locals_1_stack_item_frame_extended;
* chop_frame;
* same_frame_extended;
* append_frame;
* full_frame;
* }
* </pre>
*
* @see StackMap
* @see StackMapType
*/
public final class StackMapEntry implements Node, Cloneable {
static final StackMapEntry[] EMPTY_ARRAY = {};
private int frameType;
private int byteCodeOffset;
private StackMapType[] typesOfLocals;
private StackMapType[] typesOfStackItems;
private ConstantPool constantPool;
/**
* Constructs object from input stream.
*
* @param dataInput Input stream.
* @throws IOException Thrown if an I/O error occurs.
*/
StackMapEntry(final DataInput dataInput, final ConstantPool constantPool) throws IOException {
this(dataInput.readByte() & 0xFF, -1, null, null, constantPool); if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) { byteCodeOffset = frameType - Const.SAME_FRAME; } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { byteCodeOffset = frameType - Const.SAME_LOCALS_1_STACK_ITEM_FRAME; typesOfStackItems = new StackMapType[] { new StackMapType(dataInput, constantPool) }; } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { byteCodeOffset = dataInput.readUnsignedShort(); typesOfStackItems = new StackMapType[] { new StackMapType(dataInput, constantPool) }; } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) { byteCodeOffset = dataInput.readUnsignedShort(); } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { byteCodeOffset = dataInput.readUnsignedShort(); final int numberOfLocals = frameType - 251; typesOfLocals = new StackMapType[numberOfLocals]; for (int i = 0; i < numberOfLocals; i++) { typesOfLocals[i] = new StackMapType(dataInput, constantPool);
}
} else if (frameType == Const.FULL_FRAME) { byteCodeOffset = dataInput.readUnsignedShort(); final int numberOfLocals = dataInput.readUnsignedShort(); typesOfLocals = new StackMapType[numberOfLocals]; for (int i = 0; i < numberOfLocals; i++) { typesOfLocals[i] = new StackMapType(dataInput, constantPool);
}
final int numberOfStackItems = dataInput.readUnsignedShort(); typesOfStackItems = new StackMapType[numberOfStackItems]; for (int i = 0; i < numberOfStackItems; i++) { typesOfStackItems[i] = new StackMapType(dataInput, constantPool);
}
} else {
/* Can't happen */
throw new ClassFormatException("Invalid frame type found while parsing stack map table: %d", frameType);
}
}
/**
* DO NOT USE
*
* @param byteCodeOffset byte code offset.
* @param numberOfLocals NOT USED.
* @param typesOfLocals array of {@link StackMapType}s of locals.
* @param numberOfStackItems NOT USED.
* @param typesOfStackItems array ot {@link StackMapType}s of stack items.
* @param constantPool The constant pool.
* @deprecated Since 6.0, use {@link #StackMapEntry(int, int, StackMapType[], StackMapType[], ConstantPool)} instead.
*/
@java.lang.Deprecated
public StackMapEntry(final int byteCodeOffset, final int numberOfLocals, final StackMapType[] typesOfLocals, final int numberOfStackItems,
final StackMapType[] typesOfStackItems, final ConstantPool constantPool) { this.byteCodeOffset = byteCodeOffset; this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY; this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY; this.constantPool = constantPool; if (numberOfLocals < 0) { throw new IllegalArgumentException("numberOfLocals < 0");
}
if (numberOfStackItems < 0) { throw new IllegalArgumentException("numberOfStackItems < 0");
}
}
/**
* Create an instance
*
* @param tag The frameType to use.
* @param byteCodeOffset byte code offset.
* @param typesOfLocals array of {@link StackMapType}s of locals.
* @param typesOfStackItems array ot {@link StackMapType}s of stack items.
* @param constantPool The constant pool.
*/
public StackMapEntry(final int tag, final int byteCodeOffset, final StackMapType[] typesOfLocals, final StackMapType[] typesOfStackItems,
final ConstantPool constantPool) { this.frameType = tag; this.byteCodeOffset = byteCodeOffset; this.typesOfLocals = typesOfLocals != null ? typesOfLocals : StackMapType.EMPTY_ARRAY; this.typesOfStackItems = typesOfStackItems != null ? typesOfStackItems : StackMapType.EMPTY_ARRAY; this.constantPool = constantPool; }
/**
* Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
* That is, the hierarchy of methods, fields, attributes, and other elements forms a tree of objects.
*
* @param v Visitor object.
*/
@Override
public void accept(final Visitor v) {
v.visitStackMapEntry(this); }
/**
* Creates a deep copy of this object.
*
* @return deep copy of this object.
*/
public StackMapEntry copy() {
final StackMapEntry e;
try {
e = (StackMapEntry) clone(); } catch (final CloneNotSupportedException ex) { throw new UnsupportedOperationException("Clone Not Supported", ex); } e.typesOfLocals = new StackMapType[typesOfLocals.length]; Arrays.setAll(e.typesOfLocals, i -> typesOfLocals[i].copy()); e.typesOfStackItems = new StackMapType[typesOfStackItems.length]; Arrays.setAll(e.typesOfStackItems, i -> typesOfStackItems[i].copy()); return e;
}
/**
* Dumps stack map entry
*
* @param file Output file stream.
* @throws IOException Thrown if an I/O error occurs.
*/
public void dump(final DataOutputStream file) throws IOException {
file.write(frameType); if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { typesOfStackItems[0].dump(file); } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { file.writeShort(byteCodeOffset); typesOfStackItems[0].dump(file); } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) { file.writeShort(byteCodeOffset); } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { file.writeShort(byteCodeOffset); for (final StackMapType type : typesOfLocals) { type.dump(file);
}
} else if (frameType == Const.FULL_FRAME) { file.writeShort(byteCodeOffset); file.writeShort(Args.requireU2(typesOfLocals.length, "typesOfLocals.length")); for (final StackMapType type : typesOfLocals) { type.dump(file);
}
file.writeShort(Args.requireU2(typesOfStackItems.length, "typesOfStackItems.length")); for (final StackMapType type : typesOfStackItems) { type.dump(file);
}
} else if (!(frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX)) {
/* Can't happen */
throw new ClassFormatException("Invalid Stack map table tag: %d", frameType);
}
}
/**
* Gets the byte code offset.
*
* @return The byte code offset.
*/
public int getByteCodeOffset() {
return byteCodeOffset;
}
/**
* Gets the constant pool.
*
* @return Constant pool used by this object.
*/
public ConstantPool getConstantPool() {
return constantPool;
}
/**
* Gets the frame type.
*
* @return The frame type.
*/
public int getFrameType() {
return frameType;
}
/**
* Gets the stack map entry size.
*/
int getMapEntrySize() {
if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) { return 1;
}
if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { return 1 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
}
if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { return 3 + (typesOfStackItems[0].hasIndex() ? 3 : 1);
}
if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) { return 3;
}
if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { int len = 3; for (final StackMapType typesOfLocal : typesOfLocals) { len += typesOfLocal.hasIndex() ? 3 : 1;
}
return len;
}
if (frameType != Const.FULL_FRAME) { throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
}
int len = 7; for (final StackMapType typesOfLocal : typesOfLocals) { len += typesOfLocal.hasIndex() ? 3 : 1;
}
for (final StackMapType typesOfStackItem : typesOfStackItems) { len += typesOfStackItem.hasIndex() ? 3 : 1;
}
return len;
}
/**
* Gets the number of locals.
*
* @return The number of locals.
*/
public int getNumberOfLocals() {
return typesOfLocals.length;
}
/**
* Gets the number of stack items.
*
* @return The number of stack items.
*/
public int getNumberOfStackItems() {
return typesOfStackItems.length;
}
/**
* Gets the types of locals.
*
* @return The types of locals.
*/
public StackMapType[] getTypesOfLocals() {
return typesOfLocals;
}
/**
* Gets the types of stack items.
*
* @return The types of stack items.
*/
public StackMapType[] getTypesOfStackItems() {
return typesOfStackItems;
}
private boolean invalidFrameType(final int f) {
// @formatter:off
return f != Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED
&& !(f >= Const.CHOP_FRAME && f <= Const.CHOP_FRAME_MAX)
&& f != Const.SAME_FRAME_EXTENDED
&& !(f >= Const.APPEND_FRAME && f <= Const.APPEND_FRAME_MAX)
&& f != Const.FULL_FRAME;
// @formatter:on
}
/**
* Sets the byte code offset.
*
* @param newOffset The new offset.
*/
public void setByteCodeOffset(final int newOffset) {
if (newOffset < 0 || newOffset > 32767) { throw new IllegalArgumentException("Invalid StackMap offset: " + newOffset);
}
if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) { if (newOffset > Const.SAME_FRAME_MAX) { frameType = Const.SAME_FRAME_EXTENDED;
} else {
frameType = newOffset;
}
} else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { if (newOffset > Const.SAME_FRAME_MAX) { frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED;
} else {
frameType = Const.SAME_LOCALS_1_STACK_ITEM_FRAME + newOffset;
}
} else if (invalidFrameType(frameType)) { throw new IllegalStateException("Invalid StackMap frameType: " + frameType);
}
byteCodeOffset = newOffset; }
/**
* Sets the constant pool.
*
* @param constantPool Constant pool to be used for this object.
*/
public void setConstantPool(final ConstantPool constantPool) {
this.constantPool = constantPool; }
/**
* Sets the frame type.
*
* @param ft The frame type.
*/
public void setFrameType(final int ft) {
if (ft >= Const.SAME_FRAME && ft <= Const.SAME_FRAME_MAX) { byteCodeOffset = ft - Const.SAME_FRAME; } else if (ft >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && ft <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { byteCodeOffset = ft - Const.SAME_LOCALS_1_STACK_ITEM_FRAME; } else if (invalidFrameType(ft)) { throw new IllegalArgumentException("Invalid StackMap frameType");
}
frameType = ft; }
/**
* Sets the number of locals (deprecated).
*
* @param n The number of locals.
* @deprecated Since 6.0
*/
@java.lang.Deprecated
public void setNumberOfLocals(final int n) { // TODO unused
}
/**
* Sets the number of stack items (deprecated).
*
* @param n The number of stack items.
* @deprecated Since 6.0
*/
@java.lang.Deprecated
public void setNumberOfStackItems(final int n) { // TODO unused
}
/**
* Sets the types of locals.
*
* @param types The types of locals.
*/
public void setTypesOfLocals(final StackMapType[] types) {
typesOfLocals = types != null ? types : StackMapType.EMPTY_ARRAY; }
/**
* Sets the types of stack items.
*
* @param types The types of stack items.
*/
public void setTypesOfStackItems(final StackMapType[] types) {
typesOfStackItems = types != null ? types : StackMapType.EMPTY_ARRAY; }
/**
* @return String representation.
*/
@Override
public String toString() {
final StringBuilder buf = new StringBuilder(64); buf.append("("); if (frameType >= Const.SAME_FRAME && frameType <= Const.SAME_FRAME_MAX) { buf.append("SAME"); } else if (frameType >= Const.SAME_LOCALS_1_STACK_ITEM_FRAME && frameType <= Const.SAME_LOCALS_1_STACK_ITEM_FRAME_MAX) { buf.append("SAME_LOCALS_1_STACK"); } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { buf.append("SAME_LOCALS_1_STACK_EXTENDED"); } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) { buf.append("CHOP ").append(String.valueOf(251 - frameType)); } else if (frameType == Const.SAME_FRAME_EXTENDED) { buf.append("SAME_EXTENDED"); } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { buf.append("APPEND ").append(String.valueOf(frameType - 251)); } else if (frameType == Const.FULL_FRAME) { buf.append("FULL");
} else {
buf.append("UNKNOWN (").append(frameType).append(")");
}
buf.append(", offset delta=").append(byteCodeOffset); if (typesOfLocals.length > 0) { buf.append(", locals={"); for (int i = 0; i < typesOfLocals.length; i++) { buf.append(typesOfLocals[i]); if (i < typesOfLocals.length - 1) { buf.append(", ");
}
}
buf.append("}");
}
if (typesOfStackItems.length > 0) { buf.append(", stack items={"); for (int i = 0; i < typesOfStackItems.length; i++) { buf.append(typesOfStackItems[i]); if (i < typesOfStackItems.length - 1) { buf.append(", ");
}
}
buf.append("}");
}
buf.append(")"); return buf.toString();
}
/**
* Updates the distance (as an offset delta) from this StackMap entry to the next. Note that this might cause the
* frame type to change. Note also that delta may be negative.
*
* @param delta offset delta.
*/
public void updateByteCodeOffset(final int delta) {
setByteCodeOffset(byteCodeOffset + delta); }
}