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  import java.util.Vector;
22  import java.util.Enumeration;
23  
24  /***
25   * Represents the LocalVariableTable attribute within a
26   * method in a class file.
27   */
28  public class LocalVariableTableAttribute extends ClassAttribute {
29      /* The expected attribute name */
30      public static final String expectedAttrName = "LocalVariableTable";
31  
32      /* The list of local variables */
33      private Vector localTable;
34  
35      /* public accessors */
36  
37      /***
38       * Returns an enumeration of the local variables in the table
39       * Each element is a LocalVariable
40       */
41      Enumeration variables() {
42          return localTable.elements();
43      }
44  
45      /***
46       * Constructor for a local variable table
47       */
48      public LocalVariableTableAttribute(
49  	ConstUtf8 nameAttr, Vector lvarTable) {
50          super(nameAttr);
51          localTable = lvarTable;
52      }
53  
54      /* 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 */
55  
56      static LocalVariableTableAttribute read(
57  	ConstUtf8 attrName, DataInputStream data, CodeEnv env)
58          throws IOException {
59          int nVars = data.readUnsignedShort();
60          Vector lvarTable = new Vector();
61          while (nVars-- > 0) {
62              lvarTable.addElement(LocalVariable.read(data, env));
63          }
64          
65          return new LocalVariableTableAttribute(attrName, lvarTable);
66      }
67  
68      void write(DataOutputStream out) throws IOException {
69          out.writeShort(attrName().getIndex());
70          ByteArrayOutputStream baos = new ByteArrayOutputStream();
71          DataOutputStream tmp_out = new DataOutputStream(baos);
72          tmp_out.writeShort(localTable.size());
73          for (int i=0; i<localTable.size(); i++)
74              ((LocalVariable) localTable.elementAt(i)).write(tmp_out);
75  
76          tmp_out.flush();
77          byte tmp_bytes[] = baos.toByteArray();
78          out.writeInt(tmp_bytes.length);
79          out.write(tmp_bytes, 0, tmp_bytes.length);
80      }
81  
82      void print(PrintStream out, int indent) {
83          ClassPrint.spaces(out, indent);
84          out.println("LocalVariables: ");
85          for (int i=0; i<localTable.size(); i++) {
86              ((LocalVariable) localTable.elementAt(i)).print(out, indent+2);
87          }
88      }
89  }
90