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