aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2004-04-07 07:06:28 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2004-04-07 07:06:28 +0000
commitbaf561d93e1672d87e337e1c1c9bd106baf49083 (patch)
treef7e1af48e1e68d38741fe165cbf7787fc2950d76
parent707e08af67f99805c22b1890296052ea6b14e5d0 (diff)
downloadjavassist-baf561d93e1672d87e337e1c1c9bd106baf49083.tar.gz
javassist-baf561d93e1672d87e337e1c1c9bd106baf49083.zip
renamed insertTranslator() in ClassPool to addTranslator() since
it appends a class path to the tail of the chain. ClassClassPath has been implemented. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@79 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
-rw-r--r--sample/evolve/DemoLoader.java2
-rw-r--r--src/main/javassist/ClassClassPath.java96
-rw-r--r--src/main/javassist/ClassPool.java23
-rw-r--r--src/main/javassist/ClassPoolTail.java37
-rw-r--r--src/main/javassist/LoaderClassPath.java2
-rw-r--r--src/main/javassist/reflect/Compiler.java2
-rw-r--r--src/main/javassist/reflect/Loader.java2
-rw-r--r--src/main/javassist/rmi/AppletServer.java2
8 files changed, 119 insertions, 47 deletions
diff --git a/sample/evolve/DemoLoader.java b/sample/evolve/DemoLoader.java
index 6c16c6dc..1d8c33f9 100644
--- a/sample/evolve/DemoLoader.java
+++ b/sample/evolve/DemoLoader.java
@@ -31,7 +31,7 @@ public class DemoLoader {
public static void main(String[] args) throws Throwable {
Evolution translator = new Evolution();
ClassPool cp = ClassPool.getDefault();
- cp.insertTranslator(translator);
+ cp.addTranslator(translator);
Loader cl = new Loader();
cl.setClassPool(cp);
diff --git a/src/main/javassist/ClassClassPath.java b/src/main/javassist/ClassClassPath.java
new file mode 100644
index 00000000..1f75ede8
--- /dev/null
+++ b/src/main/javassist/ClassClassPath.java
@@ -0,0 +1,96 @@
+/*
+ * Javassist, a Java-bytecode translator toolkit.
+ * Copyright (C) 1999-2004 Shigeru Chiba. All Rights Reserved.
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. Alternatively, the contents of this file may be used under
+ * the terms of the GNU Lesser General Public License Version 2.1 or later.
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ */
+
+package javassist;
+
+import java.io.InputStream;
+import java.net.URL;
+
+/**
+ * A search-path for obtaining a class file
+ * by <code>getResourceAsStream()</code> in <code>java.lang.Class</code>.
+ *
+ * <p>Try adding a <code>ClassClassPath</code> when a program is running
+ * with a user-defined class loader and any class files are not found with
+ * the default <code>ClassPool</code>. For example,
+ *
+ * <ul><pre>
+ * ClassPool cp = ClassPool.getDefault();
+ * cp.insertClassPath(new ClassClassPath(this.getClass()));
+ * </pre></ul>
+ *
+ * This code snippet permanently adds a <code>ClassClassPath</code>
+ * to the default <code>ClassPool</code>. Note that the default
+ * <code>ClassPool</code> is a singleton. The added
+ * <code>ClassClassPath</code> uses a class object representing
+ * the class including the code snippet above.
+ *
+ * @see ClassPool#insertClassPath(ClassPath)
+ * @see ClassPool#appendClassPath(ClassPath)
+ * @see LoaderClassPath
+ */
+public class ClassClassPath implements ClassPath {
+ private Class thisClass;
+
+ /** Creates a search path.
+ *
+ * @param c the <code>Class</code> object used to obtain a class
+ * file. <code>getResourceAsStream()</code> is called on
+ * this object.
+ */
+ public ClassClassPath(Class c) {
+ thisClass = c;
+ }
+
+ ClassClassPath() {
+ /* The value of thisClass was this.getClass() in early versions:
+ *
+ * thisClass = this.getClass();
+ *
+ * However, this made openClassfile() not search all the system
+ * class paths if javassist.jar is put in jre/lib/ext/
+ * (with JDK1.4).
+ */
+ this(java.lang.Object.class);
+ }
+
+ /**
+ * Obtains a class file by <code>getResourceAsStream()</code>.
+ */
+ public InputStream openClassfile(String classname) {
+ String jarname = "/" + classname.replace('.', '/') + ".class";
+ return thisClass.getResourceAsStream(jarname);
+ }
+
+ /**
+ * Obtains the URL of the specified class file.
+ *
+ * @return null if the class file could not be found.
+ */
+ public URL find(String classname) {
+ String jarname = "/" + classname.replace('.', '/') + ".class";
+ return thisClass.getResource(jarname);
+ }
+
+ /**
+ * Does nothing.
+ */
+ public void close() {
+ }
+
+ public String toString() {
+ return thisClass.getName() + ".class";
+ }
+}
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java
index e6c75613..a37afb82 100644
--- a/src/main/javassist/ClassPool.java
+++ b/src/main/javassist/ClassPool.java
@@ -123,17 +123,23 @@ public class ClassPool extends AbsClassPool {
}
/**
- * Inserts a new translator at the head of the translator chain.
+ * Adds a new translator at the end of the translator chain.
*
* @param trans a new translator associated with this class pool.
* @throws RuntimeException if trans.start() throws an exception.
*/
- public void insertTranslator(Translator trans) throws RuntimeException {
- ClassPool next = new ClassPool(source, parent);
- next.translator = trans;
- source = next;
+ public void addTranslator(Translator trans) throws RuntimeException {
+ ClassPool cp;
+ if (translator == null)
+ cp = this;
+ else {
+ cp = new ClassPool(source, parent);
+ source = cp;
+ }
+
+ cp.translator = trans;
try {
- trans.start(next);
+ trans.start(cp);
}
catch (Exception e) {
throw new RuntimeException(
@@ -160,7 +166,12 @@ public class ClassPool extends AbsClassPool {
* cp.appendSystemPath();
* </code></ul>
*
+ * <p>If the default class pool cannot find any class files,
+ * try <code>ClassClassPath</code> and <code>LoaderClassPath</code>.
+ *
* @param t null or the translator linked to the class pool.
+ * @see ClassClassPath
+ * @see LoaderClassPath
*/
public static synchronized ClassPool getDefault() {
if (defaultPool == null) {
diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java
index fb019803..4abe8e7c 100644
--- a/src/main/javassist/ClassPoolTail.java
+++ b/src/main/javassist/ClassPoolTail.java
@@ -31,40 +31,6 @@ final class ClassPathList {
}
}
-
-final class SystemClassPath implements ClassPath {
- Class thisClass;
-
- SystemClassPath() {
- /* The value of thisClass was this.getClass() in early versions:
- *
- * thisClass = this.getClass();
- *
- * However, this made openClassfile() not search all the system
- * class paths if javassist.jar is put in jre/lib/ext/
- * (with JDK1.4).
- */
- thisClass = java.lang.Object.class;
- }
-
- public InputStream openClassfile(String classname) {
- String jarname = "/" + classname.replace('.', '/') + ".class";
- return thisClass.getResourceAsStream(jarname);
- }
-
- public URL find(String classname) {
- String jarname = "/" + classname.replace('.', '/') + ".class";
- return thisClass.getResource(jarname);
- }
-
- public void close() {}
-
- public String toString() {
- return "*system class path*";
- }
-}
-
-
final class DirClassPath implements ClassPath {
String directory;
@@ -106,7 +72,6 @@ final class DirClassPath implements ClassPath {
}
}
-
final class JarClassPath implements ClassPath {
JarFile jarfile;
String jarfileURL;
@@ -279,7 +244,7 @@ final class ClassPoolTail extends AbsClassPool {
}
public ClassPath appendSystemPath() {
- return appendClassPath(new SystemClassPath());
+ return appendClassPath(new ClassClassPath());
}
public ClassPath insertClassPath(String pathname)
diff --git a/src/main/javassist/LoaderClassPath.java b/src/main/javassist/LoaderClassPath.java
index 2ef107c3..9e141fa7 100644
--- a/src/main/javassist/LoaderClassPath.java
+++ b/src/main/javassist/LoaderClassPath.java
@@ -31,9 +31,9 @@ import java.lang.ref.WeakReference;
* @author <a href="mailto:bill@jboss.org">Bill Burke</a>
* @author Shigeru Chiba
*
- * @see javassist.ClassPath
* @see ClassPool#insertClassPath(ClassPath)
* @see ClassPool#appendClassPath(ClassPath)
+ * @see ClassClassPath
*/
public class LoaderClassPath implements ClassPath {
private WeakReference clref;
diff --git a/src/main/javassist/reflect/Compiler.java b/src/main/javassist/reflect/Compiler.java
index c5650bd1..222683a6 100644
--- a/src/main/javassist/reflect/Compiler.java
+++ b/src/main/javassist/reflect/Compiler.java
@@ -91,7 +91,7 @@ public class Compiler {
{
Reflection implementor = new Reflection();
ClassPool pool = ClassPool.getDefault();
- pool.insertTranslator(implementor);
+ pool.addTranslator(implementor);
for (int i = 0; i < n; ++i) {
CtClass c = pool.get(entries[i].classname);
diff --git a/src/main/javassist/reflect/Loader.java b/src/main/javassist/reflect/Loader.java
index ee9b3004..626c329e 100644
--- a/src/main/javassist/reflect/Loader.java
+++ b/src/main/javassist/reflect/Loader.java
@@ -134,7 +134,7 @@ public class Loader extends javassist.Loader {
reflection = new Reflection();
ClassPool pool = ClassPool.getDefault();
- pool.insertTranslator(reflection);
+ pool.addTranslator(reflection);
setClassPool(pool);
}
diff --git a/src/main/javassist/rmi/AppletServer.java b/src/main/javassist/rmi/AppletServer.java
index a848c5c4..04628516 100644
--- a/src/main/javassist/rmi/AppletServer.java
+++ b/src/main/javassist/rmi/AppletServer.java
@@ -82,7 +82,7 @@ public class AppletServer extends Webserver {
exportedNames = new Hashtable();
exportedObjects = new Vector();
stubGen = gen;
- loader.insertTranslator(gen);
+ loader.addTranslator(gen);
setClassPool(loader);
}