1   package org.apache.bcel.generic;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache BCEL" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache BCEL", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import org.apache.bcel.Constants;
58  import org.apache.bcel.classfile.*;
59  
60  /*** 
61   * This class represents a local variable within a method. It contains its
62   * scope, name and type. The generated LocalVariable object can be obtained
63   * with getLocalVariable which needs the instruction list and the constant
64   * pool as parameters.
65   *
66   * @version $Id: LocalVariableGen.java,v 1.3 2002/07/11 19:39:04 mdahm Exp $
67   * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
68   * @see     LocalVariable
69   * @see     MethodGen
70   */
71  public class LocalVariableGen
72    implements InstructionTargeter, NamedAndTyped, Cloneable,
73  	     java.io.Serializable
74  {
75    private int         index;
76    private String      name;
77    private Type        type;
78    private InstructionHandle start, end;
79  
80    /***
81     * Generate a local variable that with index `index'. Note that double and long
82     * variables need two indexs. Index indices have to be provided by the user.
83     *
84     * @param index index of local variable
85     * @param name its name
86     * @param type its type
87     * @param start from where the instruction is valid (null means from the start)
88     * @param end until where the instruction is valid (null means to the end)
89     */
90    public LocalVariableGen(int index, String name, Type type,
91  			  InstructionHandle start, InstructionHandle end) {
92      if((index < 0) || (index > Constants.MAX_SHORT))
93        throw new ClassGenException("Invalid index index: " + index);
94      
95      this.name  = name;
96      this.type  = type;
97      this.index  = index;
98      setStart(start);
99      setEnd(end);
100   }
101 
102   /***
103    * Get LocalVariable object.
104    *
105    * This relies on that the instruction list has already been dumped to byte code or
106    * or that the `setPositions' methods has been called for the instruction list.
107    *
108    * Note that for local variables whose scope end at the last
109    * instruction of the method's code, the JVM specification is ambiguous:
110    * both a start_pc+length ending at the last instruction and
111    * start_pc+length ending at first index beyond the end of the code are
112    * valid.
113    *
114    * @param il instruction list (byte code) which this variable belongs to
115    * @param cp constant pool
116    */
117   public LocalVariable getLocalVariable(ConstantPoolGen cp) {
118     int start_pc        = start.getPosition();
119     int length          = end.getPosition() - start_pc;
120 
121     if(length > 0)
122       length += end.getInstruction().getLength();
123     
124     int name_index      = cp.addUtf8(name);
125     int signature_index = cp.addUtf8(type.getSignature());
126 
127     return new LocalVariable(start_pc, length, name_index,
128 			     signature_index, index, cp.getConstantPool());
129   }
130 
131   public void        setIndex(int index)           { this.index = index; }
132   public int         getIndex()                   { return index; }
133   public void        setName(String name)        { this.name = name; }
134   public String      getName()                   { return name; }
135   public void        setType(Type type)          { this.type = type; }
136   public Type        getType()                   { return type; }
137 
138   public InstructionHandle getStart()                  { return start; }
139   public InstructionHandle getEnd()                    { return end; }
140 
141   public void setStart(InstructionHandle start) {
142     BranchInstruction.notifyTarget(this.start, start, this);
143     this.start = start;
144   }
145 
146   public void setEnd(InstructionHandle end) {
147     BranchInstruction.notifyTarget(this.end, end, this);
148     this.end = end;
149   }
150 
151   /***
152    * @param old_ih old target, either start or end
153    * @param new_ih new target
154    */
155   public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
156     boolean targeted = false;
157 
158     if(start == old_ih) {
159       targeted = true;
160       setStart(new_ih);
161     }
162 
163     if(end == old_ih) {
164       targeted = true;
165       setEnd(new_ih);
166     }
167 
168     if(!targeted)
169       throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " +
170 				  end + "}");
171   }
172 
173   /***
174    * @return true, if ih is target of this variable
175    */
176   public boolean containsTarget(InstructionHandle ih) {
177     return (start == ih) || (end == ih);
178   }
179 
180   /***
181    * We consider to local variables to be equal, if the use the same index and
182    * are valid in the same range.
183    */
184   public boolean equals(Object o) {
185     if(!(o instanceof LocalVariableGen))
186       return false;
187 
188     LocalVariableGen l = (LocalVariableGen)o;
189     return (l.index == index) && (l.start == start) && (l.end == end);
190   } 
191 
192   public String toString() {
193     return "LocalVariableGen(" + name +  ", " + type +  ", " + start + ", " + end + ")";
194   }
195 
196   public Object clone() {
197     try {
198       return super.clone();
199     } catch(CloneNotSupportedException e) {
200       System.err.println(e);
201       return null;
202     }
203   }
204 }
This page was automatically generated by Maven