View Javadoc

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