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