Skip to content

Commit 0776f0a

Browse files
committed
Initial commit of the tester library sources - version 1.5.0
(Older versions remain at the google code site and the http://www.ccs.neu.edu/javalib site.)
1 parent 1111cd7 commit 0776f0a

243 files changed

Lines changed: 60061 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Javassist, a Java-bytecode translator toolkit.
3+
* Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4+
*
5+
* The contents of this file are subject to the Mozilla Public License Version
6+
* 1.1 (the "License"); you may not use this file except in compliance with
7+
* the License. Alternatively, the contents of this file may be used under
8+
* the terms of the GNU Lesser General Public License Version 2.1 or later.
9+
*
10+
* Software distributed under the License is distributed on an "AS IS" basis,
11+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12+
* for the specific language governing rights and limitations under the
13+
* License.
14+
*/
15+
16+
package javassist;
17+
18+
import java.io.*;
19+
import java.net.URL;
20+
import java.net.MalformedURLException;
21+
22+
/**
23+
* A <code>ByteArrayClassPath</code> contains bytes that is served as
24+
* a class file to a <code>ClassPool</code>. It is useful to convert
25+
* a byte array to a <code>CtClass</code> object.
26+
*
27+
* <p>For example, if you want to convert a byte array <code>b</code>
28+
* into a <code>CtClass</code> object representing the class with a name
29+
* <code>classname</code>, then do as following:
30+
*
31+
* <ul><pre>
32+
* ClassPool cp = ClassPool.getDefault();
33+
* cp.insertClassPath(new ByteArrayClassPath(classname, b));
34+
* CtClass cc = cp.get(classname);
35+
* </pre></ul>
36+
*
37+
* <p>The <code>ClassPool</code> object <code>cp</code> uses the created
38+
* <code>ByteArrayClassPath</code> object as the source of the class file.
39+
*
40+
* <p>A <code>ByteArrayClassPath</code> must be instantiated for every
41+
* class. It contains only a single class file.
42+
*
43+
* @see javassist.ClassPath
44+
* @see ClassPool#insertClassPath(ClassPath)
45+
* @see ClassPool#appendClassPath(ClassPath)
46+
* @see ClassPool#makeClass(InputStream)
47+
*/
48+
public class ByteArrayClassPath implements ClassPath {
49+
protected String classname;
50+
protected byte[] classfile;
51+
52+
/*
53+
* Creates a <code>ByteArrayClassPath</code> containing the given
54+
* bytes.
55+
*
56+
* @param name a fully qualified class name
57+
* @param classfile the contents of a class file.
58+
*/
59+
public ByteArrayClassPath(String name, byte[] classfile) {
60+
this.classname = name;
61+
this.classfile = classfile;
62+
}
63+
64+
/**
65+
* Closes this class path.
66+
*/
67+
public void close() {}
68+
69+
public String toString() {
70+
return "byte[]:" + classname;
71+
}
72+
73+
/**
74+
* Opens the class file.
75+
*/
76+
public InputStream openClassfile(String classname) {
77+
if(this.classname.equals(classname))
78+
return new ByteArrayInputStream(classfile);
79+
else
80+
return null;
81+
}
82+
83+
/**
84+
* Obtains the URL.
85+
*/
86+
public URL find(String classname) {
87+
if(this.classname.equals(classname)) {
88+
String cname = classname.replace('.', '/') + ".class";
89+
try {
90+
// return new File(cname).toURL();
91+
return new URL("file:/ByteArrayClassPath/" + cname);
92+
}
93+
catch (MalformedURLException e) {}
94+
}
95+
96+
return null;
97+
}
98+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Javassist, a Java-bytecode translator toolkit.
3+
* Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4+
*
5+
* The contents of this file are subject to the Mozilla Public License Version
6+
* 1.1 (the "License"); you may not use this file except in compliance with
7+
* the License. Alternatively, the contents of this file may be used under
8+
* the terms of the GNU Lesser General Public License Version 2.1 or later.
9+
*
10+
* Software distributed under the License is distributed on an "AS IS" basis,
11+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12+
* for the specific language governing rights and limitations under the
13+
* License.
14+
*/
15+
16+
package javassist;
17+
18+
import javassist.compiler.CompileError;
19+
20+
/**
21+
* Thrown when bytecode transformation has failed.
22+
*/
23+
public class CannotCompileException extends Exception {
24+
private Throwable myCause;
25+
26+
/**
27+
* Gets the cause of this throwable.
28+
* It is for JDK 1.3 compatibility.
29+
*/
30+
public Throwable getCause() {
31+
return (myCause == this ? null : myCause);
32+
}
33+
34+
/**
35+
* Initializes the cause of this throwable.
36+
* It is for JDK 1.3 compatibility.
37+
*/
38+
public synchronized Throwable initCause(Throwable cause) {
39+
myCause = cause;
40+
return this;
41+
}
42+
43+
private String message;
44+
45+
/**
46+
* Gets a long message if it is available.
47+
*/
48+
public String getReason() {
49+
if (message != null)
50+
return message;
51+
else
52+
return this.toString();
53+
}
54+
55+
/**
56+
* Constructs a CannotCompileException with a message.
57+
*
58+
* @param msg the message.
59+
*/
60+
public CannotCompileException(String msg) {
61+
super(msg);
62+
message = msg;
63+
initCause(null);
64+
}
65+
66+
/**
67+
* Constructs a CannotCompileException with an <code>Exception</code>
68+
* representing the cause.
69+
*
70+
* @param e the cause.
71+
*/
72+
public CannotCompileException(Throwable e) {
73+
super("by " + e.toString());
74+
message = null;
75+
initCause(e);
76+
}
77+
78+
/**
79+
* Constructs a CannotCompileException with a detailed message
80+
* and an <code>Exception</code> representing the cause.
81+
*
82+
* @param msg the message.
83+
* @param e the cause.
84+
*/
85+
public CannotCompileException(String msg, Throwable e) {
86+
this(msg);
87+
initCause(e);
88+
}
89+
90+
/**
91+
* Constructs a CannotCompileException with a
92+
* <code>NotFoundException</code>.
93+
*/
94+
public CannotCompileException(NotFoundException e) {
95+
this("cannot find " + e.getMessage(), e);
96+
}
97+
98+
/**
99+
* Constructs a CannotCompileException with an <code>CompileError</code>.
100+
*/
101+
public CannotCompileException(CompileError e) {
102+
this("[source error] " + e.getMessage(), e);
103+
}
104+
105+
/**
106+
* Constructs a CannotCompileException
107+
* with a <code>ClassNotFoundException</code>.
108+
*/
109+
public CannotCompileException(ClassNotFoundException e, String name) {
110+
this("cannot find " + name, e);
111+
}
112+
113+
/**
114+
* Constructs a CannotCompileException with a ClassFormatError.
115+
*/
116+
public CannotCompileException(ClassFormatError e, String name) {
117+
this("invalid class format: " + name, e);
118+
}
119+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Javassist, a Java-bytecode translator toolkit.
3+
* Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4+
*
5+
* The contents of this file are subject to the Mozilla Public License Version
6+
* 1.1 (the "License"); you may not use this file except in compliance with
7+
* the License. Alternatively, the contents of this file may be used under
8+
* the terms of the GNU Lesser General Public License Version 2.1 or later.
9+
*
10+
* Software distributed under the License is distributed on an "AS IS" basis,
11+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12+
* for the specific language governing rights and limitations under the
13+
* License.
14+
*/
15+
16+
package javassist;
17+
18+
import java.io.InputStream;
19+
import java.net.URL;
20+
21+
/**
22+
* A search-path for obtaining a class file
23+
* by <code>getResourceAsStream()</code> in <code>java.lang.Class</code>.
24+
*
25+
* <p>Try adding a <code>ClassClassPath</code> when a program is running
26+
* with a user-defined class loader and any class files are not found with
27+
* the default <code>ClassPool</code>. For example,
28+
*
29+
* <ul><pre>
30+
* ClassPool cp = ClassPool.getDefault();
31+
* cp.insertClassPath(new ClassClassPath(this.getClass()));
32+
* </pre></ul>
33+
*
34+
* This code snippet permanently adds a <code>ClassClassPath</code>
35+
* to the default <code>ClassPool</code>. Note that the default
36+
* <code>ClassPool</code> is a singleton. The added
37+
* <code>ClassClassPath</code> uses a class object representing
38+
* the class including the code snippet above.
39+
*
40+
* @see ClassPool#insertClassPath(ClassPath)
41+
* @see ClassPool#appendClassPath(ClassPath)
42+
* @see LoaderClassPath
43+
*/
44+
public class ClassClassPath implements ClassPath {
45+
private Class thisClass;
46+
47+
/** Creates a search path.
48+
*
49+
* @param c the <code>Class</code> object used to obtain a class
50+
* file. <code>getResourceAsStream()</code> is called on
51+
* this object.
52+
*/
53+
public ClassClassPath(Class c) {
54+
thisClass = c;
55+
}
56+
57+
ClassClassPath() {
58+
/* The value of thisClass was this.getClass() in early versions:
59+
*
60+
* thisClass = this.getClass();
61+
*
62+
* However, this made openClassfile() not search all the system
63+
* class paths if javassist.jar is put in jre/lib/ext/
64+
* (with JDK1.4).
65+
*/
66+
this(java.lang.Object.class);
67+
}
68+
69+
/**
70+
* Obtains a class file by <code>getResourceAsStream()</code>.
71+
*/
72+
public InputStream openClassfile(String classname) {
73+
String jarname = "/" + classname.replace('.', '/') + ".class";
74+
return thisClass.getResourceAsStream(jarname);
75+
}
76+
77+
/**
78+
* Obtains the URL of the specified class file.
79+
*
80+
* @return null if the class file could not be found.
81+
*/
82+
public URL find(String classname) {
83+
String jarname = "/" + classname.replace('.', '/') + ".class";
84+
return thisClass.getResource(jarname);
85+
}
86+
87+
/**
88+
* Does nothing.
89+
*/
90+
public void close() {
91+
}
92+
93+
public String toString() {
94+
return thisClass.getName() + ".class";
95+
}
96+
}

0 commit comments

Comments
 (0)