View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  import java.io.*;
21  
22  /***
23   * Represents a local variable within a LocalVariableTable within
24   * a CodeAttribute in a class file.
25   */
26  public class LocalVariable {
27      /* The pc at which the variable becomes effecive */
28      private InsnTarget varStartPC; /* inclusive */
29  
30      /* The pc at which the variable becomes in-effecive */
31      private InsnTarget varEndPC;   /* exclusive */
32  
33      /* The name of the variable */
34      private ConstUtf8 varName;
35  
36      /* The type signature of the variable */
37      private ConstUtf8 varSig;
38  
39      /* The slot to which the variable is assigned */
40      private int varSlot;
41  
42      /* public accessors */
43  
44      /***
45       * Constructor for a local variable
46       */
47      public LocalVariable(InsnTarget startPC, InsnTarget endPC,
48                           ConstUtf8 name, ConstUtf8 sig, int slot) {
49          varStartPC = startPC;
50          varEndPC = endPC;
51          varName = name;
52          varSig = sig;
53          varSlot = slot;
54      }
55  
56      /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
57  
58      static LocalVariable read(DataInputStream data, CodeEnv env)
59          throws IOException {
60          int startPC = data.readUnsignedShort();
61          InsnTarget startPCTarget = env.getTarget(startPC);
62          int length = data.readUnsignedShort();
63          InsnTarget endPCTarget = env.getTarget(startPC+length);
64          ConstUtf8 name = 
65              (ConstUtf8) env.pool().constantAt(data.readUnsignedShort());
66          ConstUtf8 sig = 
67              (ConstUtf8) env.pool().constantAt(data.readUnsignedShort());
68          int slot = data.readUnsignedShort();
69          return new LocalVariable(startPCTarget, endPCTarget, name, sig, slot);
70      }
71  
72      void write(DataOutputStream out) throws IOException {
73          out.writeShort(varStartPC.offset());
74          out.writeShort(varEndPC.offset() - varStartPC.offset());
75          out.writeShort((varName == null) ? 0 : varName.getIndex());
76          out.writeShort((varSig == null) ? 0 : varSig.getIndex());
77          out.writeShort(varSlot);
78      }
79  
80      public void print(PrintStream out, int indent) {
81          ClassPrint.spaces(out, indent);
82          out.print("'" + ((varName == null) ? "(null)" : varName.asString()) + "'");
83          out.print(" sig = " + ((varSig == null) ? "(null)" : varSig.asString()));
84          out.print(" start_pc = " + Integer.toString(varStartPC.offset()));
85          out.print(" length = " +
86                    Integer.toString(varEndPC.offset() - varStartPC.offset()));
87          out.println(" slot = " + Integer.toString(varSlot));
88      }
89  }