KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > ExceptionsAttribute


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.bytecode;
30
31 import com.caucho.log.Log;
32 import com.caucho.vfs.TempBuffer;
33 import com.caucho.vfs.TempStream;
34 import com.caucho.vfs.WriteStream;
35
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Represents a generic attribute
42  */

43 public class ExceptionsAttribute extends Attribute {
44   static private final Logger JavaDoc log = Log.open(ExceptionsAttribute.class);
45
46   private ArrayList JavaDoc<String JavaDoc> _exceptions = new ArrayList JavaDoc<String JavaDoc>();
47
48   ExceptionsAttribute(String JavaDoc name)
49   {
50     super(name);
51   }
52
53   /**
54    * Adds an exception
55    */

56   public void addException(String JavaDoc exn)
57   {
58     _exceptions.add(exn);
59   }
60
61   /**
62    * Returns the exceptions.
63    */

64   public ArrayList JavaDoc<String JavaDoc> getExceptionList()
65   {
66     return _exceptions;
67   }
68
69   /**
70    * Writes the field to the output.
71    */

72   public void read(ByteCodeParser in)
73     throws IOException JavaDoc
74   {
75     int length = in.readInt();
76     
77     int exnCount = in.readShort();
78
79     for (int i = 0; i < exnCount; i++) {
80       int index = in.readShort();
81
82       if (index == 0)
83     _exceptions.add(null);
84       
85       _exceptions.add(in.getConstantPool().getClass(index).getName());
86     }
87   }
88
89   /**
90    * Writes the field to the output.
91    */

92   public void write(ByteCodeWriter out)
93     throws IOException JavaDoc
94   {
95     out.writeUTF8Const(getName());
96
97     TempStream ts = new TempStream();
98     ts.openWrite();
99     WriteStream ws = new WriteStream(ts);
100     ByteCodeWriter o2 = new ByteCodeWriter(ws, out.getJavaClass());
101
102     o2.writeShort(_exceptions.size());
103     for (int i = 0; i < _exceptions.size(); i++) {
104       String JavaDoc exn = _exceptions.get(i);
105
106       o2.writeClass(exn);
107     }
108     
109     ws.close();
110     
111     out.writeInt(ts.getLength());
112     
113     TempBuffer ptr = ts.getHead();
114
115     for (; ptr != null; ptr = ptr.getNext())
116       out.write(ptr.getBuffer(), 0, ptr.getLength());
117
118     ts.destroy();
119   }
120
121   /**
122    * Clones the attribute
123    */

124   public Attribute export(JavaClass cl, JavaClass target)
125   {
126     ConstantPool cp = target.getConstantPool();
127
128     cp.addUTF8(getName());
129     
130     ExceptionsAttribute attr = new ExceptionsAttribute(getName());
131
132     for (int i = 0; i < _exceptions.size(); i++) {
133       String JavaDoc exn = _exceptions.get(i);
134
135       cp.addClass(exn);
136
137       attr.addException(exn);
138     }
139
140     return attr;
141   }
142
143   public String JavaDoc toString()
144   {
145     return "ExceptionsAttribute[" + getName() + "]";
146   }
147 }
148
Popular Tags