001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.bytecode;
030:
031: import com.caucho.log.Log;
032: import com.caucho.vfs.TempBuffer;
033: import com.caucho.vfs.TempStream;
034: import com.caucho.vfs.WriteStream;
035:
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.logging.Logger;
039:
040: /**
041: * Represents a generic attribute
042: */
043: public class ExceptionsAttribute extends Attribute {
044: static private final Logger log = Log
045: .open(ExceptionsAttribute.class);
046:
047: private ArrayList<String> _exceptions = new ArrayList<String>();
048:
049: ExceptionsAttribute(String name) {
050: super (name);
051: }
052:
053: /**
054: * Adds an exception
055: */
056: public void addException(String exn) {
057: _exceptions.add(exn);
058: }
059:
060: /**
061: * Returns the exceptions.
062: */
063: public ArrayList<String> getExceptionList() {
064: return _exceptions;
065: }
066:
067: /**
068: * Writes the field to the output.
069: */
070: public void read(ByteCodeParser in) throws IOException {
071: int length = in.readInt();
072:
073: int exnCount = in.readShort();
074:
075: for (int i = 0; i < exnCount; i++) {
076: int index = in.readShort();
077:
078: if (index == 0)
079: _exceptions.add(null);
080:
081: _exceptions.add(in.getConstantPool().getClass(index)
082: .getName());
083: }
084: }
085:
086: /**
087: * Writes the field to the output.
088: */
089: public void write(ByteCodeWriter out) throws IOException {
090: out.writeUTF8Const(getName());
091:
092: TempStream ts = new TempStream();
093: ts.openWrite();
094: WriteStream ws = new WriteStream(ts);
095: ByteCodeWriter o2 = new ByteCodeWriter(ws, out.getJavaClass());
096:
097: o2.writeShort(_exceptions.size());
098: for (int i = 0; i < _exceptions.size(); i++) {
099: String exn = _exceptions.get(i);
100:
101: o2.writeClass(exn);
102: }
103:
104: ws.close();
105:
106: out.writeInt(ts.getLength());
107:
108: TempBuffer ptr = ts.getHead();
109:
110: for (; ptr != null; ptr = ptr.getNext())
111: out.write(ptr.getBuffer(), 0, ptr.getLength());
112:
113: ts.destroy();
114: }
115:
116: /**
117: * Clones the attribute
118: */
119: public Attribute export(JavaClass cl, JavaClass target) {
120: ConstantPool cp = target.getConstantPool();
121:
122: cp.addUTF8(getName());
123:
124: ExceptionsAttribute attr = new ExceptionsAttribute(getName());
125:
126: for (int i = 0; i < _exceptions.size(); i++) {
127: String exn = _exceptions.get(i);
128:
129: cp.addClass(exn);
130:
131: attr.addException(exn);
132: }
133:
134: return attr;
135: }
136:
137: public String toString() {
138: return "ExceptionsAttribute[" + getName() + "]";
139: }
140: }
|