001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.classfile; 020 021import java.io.DataInput; 022import java.io.DataOutputStream; 023import java.io.IOException; 024import java.util.Arrays; 025 026import org.apache.bcel.Const; 027import org.apache.bcel.util.Args; 028 029/** 030 * This class represents a stack map attribute used for preverification of Java classes for the 031 * <a href="https://java.sun.com/j2me/"> Java 2 Micro Edition</a> (J2ME). This attribute is used by the 032 * <a href="https://java.sun.com/products/cldc/">KVM</a> and contained within the Code attribute of a method. See CLDC 033 * specification �5.3.1.2 034 * 035 * <pre> 036 * StackMapTable_attribute { 037 * u2 attribute_name_index; 038 * u4 attribute_length; 039 * u2 number_of_entries; 040 * stack_map_frame entries[number_of_entries]; 041 * } 042 * </pre> 043 * 044 * @see Code 045 * @see StackMapEntry 046 * @see StackMapType 047 */ 048public final class StackMap extends Attribute { 049 050 private StackMapEntry[] table; // Table of stack map entries 051 052 /** 053 * Constructs object from input stream. 054 * 055 * @param nameIndex Index of name 056 * @param length Content length in bytes 057 * @param dataInput Input stream 058 * @param constantPool Array of constants 059 * @throws IOException if an I/O error occurs. 060 */ 061 StackMap(final int nameIndex, final int length, final DataInput dataInput, final ConstantPool constantPool) throws IOException { 062 this(nameIndex, length, (StackMapEntry[]) null, constantPool); 063 final int mapLength = dataInput.readUnsignedShort(); 064 table = new StackMapEntry[mapLength]; 065 for (int i = 0; i < mapLength; i++) { 066 table[i] = new StackMapEntry(dataInput, constantPool); 067 } 068 } 069 070 /* 071 * @param nameIndex Index of name 072 * @param length Content length in bytes 073 * @param map Table of stack map entries 074 * @param constantPool Array of constants 075 */ 076 public StackMap(final int nameIndex, final int length, final StackMapEntry[] table, final ConstantPool constantPool) { 077 super(Const.ATTR_STACK_MAP, nameIndex, length, constantPool); 078 this.table = table != null ? table : StackMapEntry.EMPTY_ARRAY; 079 Args.requireU2(this.table.length, "table.length"); 080 } 081 082 /** 083 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 084 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 085 * 086 * @param v Visitor object 087 */ 088 @Override 089 public void accept(final Visitor v) { 090 v.visitStackMap(this); 091 } 092 093 /** 094 * @return deep copy of this attribute 095 */ 096 @Override 097 public Attribute copy(final ConstantPool constantPool) { 098 final StackMap c = (StackMap) clone(); 099 c.table = new StackMapEntry[table.length]; 100 Arrays.setAll(c.table, i -> table[i].copy()); 101 c.setConstantPool(constantPool); 102 return c; 103 } 104 105 /** 106 * Dump stack map table attribute to file stream in binary format. 107 * 108 * @param file Output file stream 109 * @throws IOException if an I/O error occurs. 110 */ 111 @Override 112 public void dump(final DataOutputStream file) throws IOException { 113 super.dump(file); 114 file.writeShort(table.length); 115 for (final StackMapEntry entry : table) { 116 entry.dump(file); 117 } 118 } 119 120 public int getMapLength() { 121 return table.length; 122 } 123 124 /** 125 * @return Array of stack map entries 126 */ 127 public StackMapEntry[] getStackMap() { 128 return table; 129 } 130 131 /** 132 * @param table Array of stack map entries 133 */ 134 public void setStackMap(final StackMapEntry[] table) { 135 this.table = table != null ? table : StackMapEntry.EMPTY_ARRAY; 136 int len = 2; // Length of 'number_of_entries' field prior to the array of stack maps 137 for (final StackMapEntry element : this.table) { 138 len += element.getMapEntrySize(); 139 } 140 setLength(len); 141 } 142 143 /** 144 * @return String representation. 145 */ 146 @Override 147 public String toString() { 148 final StringBuilder buf = new StringBuilder("StackMap("); 149 int runningOffset = -1; // no +1 on first entry 150 for (int i = 0; i < table.length; i++) { 151 runningOffset = table[i].getByteCodeOffset() + runningOffset + 1; 152 buf.append(String.format("%n@%03d %s", runningOffset, table[i])); 153 if (i < table.length - 1) { 154 buf.append(", "); 155 } 156 } 157 buf.append(')'); 158 return buf.toString(); 159 } 160}