001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import java.io.*;
034: import static java.io.ObjectStreamConstants.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class _StackTraceElement {
040:
041: public static StackTraceElement createNewInstance(
042: String declaringClass, String methodName, String fileName,
043: int lineNumber) {
044: if (declaringClass == null) {
045: throw new NullPointerException("Declaring class is null");
046: }
047: if (methodName == null) {
048: throw new NullPointerException("Method name is null");
049: }
050: try {
051: ObjectStreamClass streamClass = ObjectStreamClass
052: .lookup(StackTraceElement.class);
053: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
054: DataOutputStream stream = new DataOutputStream(outputStream);
055: stream.writeShort(STREAM_MAGIC);
056: stream.writeShort(STREAM_VERSION);
057: stream.writeByte(TC_OBJECT);
058: stream.writeByte(TC_CLASSDESC);
059: stream.writeUTF(streamClass.forClass().getName());
060: stream.writeLong(streamClass.getSerialVersionUID());
061: stream.writeByte(SC_SERIALIZABLE);
062: stream.writeShort(4);
063: stream.writeByte('I');
064: stream.writeUTF("lineNumber");
065: stream.writeByte('L');
066: stream.writeUTF("declaringClass");
067: stream.writeByte(TC_STRING);
068: stream.writeUTF("Ljava/lang/String;");
069: stream.writeByte('L');
070: stream.writeUTF("fileName");
071: stream.writeByte(TC_REFERENCE);
072: stream.writeInt(baseWireHandle + 1);
073: stream.writeByte('L');
074: stream.writeUTF("methodName");
075: stream.writeByte(TC_REFERENCE);
076: stream.writeInt(baseWireHandle + 1);
077: stream.writeByte(TC_ENDBLOCKDATA);
078: stream.writeByte(TC_NULL);
079: stream.writeInt(lineNumber);
080: stream.writeByte(TC_STRING);
081: stream.writeUTF(declaringClass);
082: if (fileName == null) {
083: stream.writeByte(TC_NULL);
084: } else {
085: stream.writeByte(TC_STRING);
086: stream.writeUTF(fileName);
087: }
088: stream.writeByte(TC_STRING);
089: stream.writeUTF(methodName);
090: stream.close();
091: ByteArrayInputStream inputStream = new ByteArrayInputStream(
092: outputStream.toByteArray());
093: return (StackTraceElement) new ObjectInputStream(
094: inputStream).readObject();
095: } catch (IOException e) {
096: throw new Error(e);
097: } catch (ClassNotFoundException e) {
098: throw new Error(e);
099: }
100: }
101:
102: }
|