KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > LocalVariableTableAttribute


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.*;
28 import java.util.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 /**
32  * Represents the LocalVariableTable attribute within a
33  * method in a class file.
34  */

35
36 public class LocalVariableTableAttribute extends ClassAttribute {
37   /* The expected attribute name */
38     public static final String JavaDoc expectedAttrName = "LocalVariableTable";//NOI18N
39

40   /* The list of local variables */
41   private Vector JavaDoc localTable;
42
43   /* public accessors */
44
45   /**
46    * Returns an enumeration of the local variables in the table
47    * Each element is a LocalVariable
48    */

49   Enumeration JavaDoc variables() {
50     return localTable.elements();
51   }
52
53   /**
54    * Constructor for a local variable table
55    */

56   public LocalVariableTableAttribute(
57     ConstUtf8 nameAttr, Vector JavaDoc lvarTable) {
58     super(nameAttr);
59     localTable = lvarTable;
60   }
61
62   /* package local methods */
63
64   static LocalVariableTableAttribute read(
65     ConstUtf8 attrName, DataInputStream data, CodeEnv env)
66     throws IOException {
67     int nVars = data.readUnsignedShort();
68     Vector JavaDoc lvarTable = new Vector JavaDoc();
69     while (nVars-- > 0) {
70       lvarTable.addElement(LocalVariable.read(data, env));
71     }
72         
73     return new LocalVariableTableAttribute(attrName, lvarTable);
74   }
75
76   void write(DataOutputStream out) throws IOException {
77     out.writeShort(attrName().getIndex());
78     ByteArrayOutputStream baos = new ByteArrayOutputStream();
79     DataOutputStream tmp_out = new DataOutputStream(baos);
80     tmp_out.writeShort(localTable.size());
81     for (int i=0; i<localTable.size(); i++)
82       ((LocalVariable) localTable.elementAt(i)).write(tmp_out);
83
84     tmp_out.flush();
85     byte tmp_bytes[] = baos.toByteArray();
86     out.writeInt(tmp_bytes.length);
87     out.write(tmp_bytes, 0, tmp_bytes.length);
88   }
89
90   void print(PrintStream out, int indent) {
91     ClassPrint.spaces(out, indent);
92     out.println("LocalVariables: ");//NOI18N
93
for (int i=0; i<localTable.size(); i++) {
94       ((LocalVariable) localTable.elementAt(i)).print(out, indent+2);
95     }
96   }
97 }
98
99
Popular Tags