View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   package com.sun.org.apache.bcel.internal.classfile;
6   
7   /* ====================================================================
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2001 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution,
26   *    if any, must include the following acknowledgment:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowledgment may appear in the software itself,
30   *    if and wherever such third-party acknowledgments normally appear.
31   *
32   * 4. The names "Apache" and "Apache Software Foundation" and
33   *    "Apache BCEL" must not be used to endorse or promote products
34   *    derived from this software without prior written permission. For
35   *    written permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache",
38   *    "Apache BCEL", nor may "Apache" appear in their name, without
39   *    prior written permission of the Apache Software Foundation.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   */
60  
61  import  com.sun.org.apache.bcel.internal.Constants;
62  import  java.io.*;
63  
64  /**
65   * This class represents a stack map attribute used for
66   * preverification of Java classes for the <a
67   * href="http://java.sun.com/j2me/"> Java 2 Micro Edition</a>
68   * (J2ME). This attribute is used by the <a
69   * href="http://java.sun.com/products/cldc/">KVM</a> and contained
70   * within the Code attribute of a method. See CLDC specification
71   * 5.3.1.2
72   *
73   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
74   * @see     Code
75   * @see     StackMapEntry
76   * @see     StackMapType
77   */
78  public final class StackMap extends Attribute implements Node {
79    private int             map_length;
80    private StackMapEntry[] map; // Table of stack map entries
81  
82    /*
83     * @param name_index Index of name
84     * @param length Content length in bytes
85     * @param map Table of stack map entries
86     * @param constant_pool Array of constants
87     */
88    public StackMap(int name_index, int length,  StackMapEntry[] map,
89                    ConstantPool constant_pool)
90    {
91      super(Constants.ATTR_STACK_MAP, name_index, length, constant_pool);
92  
93      setStackMap(map);
94    }
95  
96    /**
97     * Construct object from file stream.
98     * @param name_index Index of name
99     * @param length Content length in bytes
100    * @param file Input stream
101    * @throws IOException
102    * @param constant_pool Array of constants
103    */
104   StackMap(int name_index, int length, DataInputStream file,
105            ConstantPool constant_pool) throws IOException
106   {
107     this(name_index, length, (StackMapEntry[])null, constant_pool);
108 
109     map_length = file.readUnsignedShort();
110     map = new StackMapEntry[map_length];
111 
112     for(int i=0; i < map_length; i++)
113       map[i] = new StackMapEntry(file, constant_pool);
114   }
115 
116   /**
117    * Dump line number table attribute to file stream in binary format.
118    *
119    * @param file Output file stream
120    * @throws IOException
121    */
122   public final void dump(DataOutputStream file) throws IOException
123   {
124     super.dump(file);
125     file.writeShort(map_length);
126     for(int i=0; i < map_length; i++)
127       map[i].dump(file);
128   }
129 
130   /**
131    * @return Array of stack map entries
132    */
133   public final StackMapEntry[] getStackMap() { return map; }
134 
135   /**
136    * @param map Array of stack map entries
137    */
138   public final void setStackMap(StackMapEntry[] map) {
139     this.map = map;
140 
141     map_length = (map == null)? 0 : map.length;
142   }
143 
144   /**
145    * @return String representation.
146    */
147   public final String toString() {
148     StringBuffer buf = new StringBuffer("StackMap(");
149 
150     for(int i=0; i < map_length; i++) {
151       buf.append(map[i].toString());
152 
153       if(i < map_length - 1)
154         buf.append(", ");
155     }
156 
157     buf.append(')');
158 
159     return buf.toString();
160   }
161 
162   /**
163    * @return deep copy of this attribute
164    */
165   public Attribute copy(ConstantPool constant_pool) {
166     StackMap c = (StackMap)clone();
167 
168     c.map = new StackMapEntry[map_length];
169     for(int i=0; i < map_length; i++)
170       c.map[i] = map[i].copy();
171 
172     c.constant_pool = constant_pool;
173     return c;
174   }
175 
176   /**
177    * Called by objects that are traversing the nodes of the tree implicitely
178    * defined by the contents of a Java class. I.e., the hierarchy of methods,
179    * fields, attributes, etc. spawns a tree of objects.
180    *
181    * @param v Visitor object
182    */
183    public void accept(Visitor v) {
184      v.visitStackMap(this);
185    }
186 
187   public final int getMapLength() { return map_length; }
188 }