aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2012-02-05 09:49:25 +0000
committerchiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3>2012-02-05 09:49:25 +0000
commitb83522334f4abe2d9d3f83d0b6949fabd984cf75 (patch)
treed80eba3fd0114cfd56d483c021dcb0cacfe7d861 /src
parentc920b315b117a3db52fd16464c1e89176317e948 (diff)
downloadjavassist-b83522334f4abe2d9d3f83d0b6949fabd984cf75.tar.gz
javassist-b83522334f4abe2d9d3f83d0b6949fabd984cf75.zip
fixed JASSIST-147
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@610 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src')
-rw-r--r--src/main/javassist/ClassPool.java56
-rw-r--r--src/main/javassist/Loader.java14
-rw-r--r--src/test/javassist/JvstTest4.java11
3 files changed, 64 insertions, 17 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java
index abeaa8c1..dbbb99e5 100644
--- a/src/main/javassist/ClassPool.java
+++ b/src/main/javassist/ClassPool.java
@@ -70,6 +70,7 @@ import javassist.bytecode.Descriptor;
public class ClassPool {
// used by toClass().
private static java.lang.reflect.Method defineClass1, defineClass2;
+ private static java.lang.reflect.Method definePackage;
static {
try {
@@ -83,6 +84,11 @@ public class ClassPool {
defineClass2 = cl.getDeclaredMethod("defineClass",
new Class[] { String.class, byte[].class,
int.class, int.class, ProtectionDomain.class });
+
+ definePackage = cl.getDeclaredMethod("definePackage",
+ new Class[] { String.class, String.class, String.class,
+ String.class, String.class, String.class,
+ String.class, java.net.URL.class });
return null;
}
});
@@ -1080,7 +1086,7 @@ public class ClassPool {
new Integer(b.length), domain};
}
- return toClass2(method, loader, args);
+ return (Class)toClass2(method, loader, args);
}
catch (RuntimeException e) {
throw e;
@@ -1093,16 +1099,60 @@ public class ClassPool {
}
}
- private static synchronized Class toClass2(Method method,
+ private static synchronized Object toClass2(Method method,
ClassLoader loader, Object[] args)
throws Exception
{
method.setAccessible(true);
try {
- return (Class)method.invoke(loader, args);
+ return method.invoke(loader, args);
}
finally {
method.setAccessible(false);
}
}
+
+ /**
+ * Defines a new package. If the package is already defined, this method
+ * performs nothing.
+ *
+ * <p>You do not necessarily need to
+ * call this method. If this method is called, then
+ * <code>getPackage()</code> on the <code>Class</code> object returned
+ * by <code>toClass()</code> will return a non-null object.
+ *
+ * @param loader the class loader passed to <code>toClass()</code> or
+ * the default one obtained by <code>getClassLoader()</code>.
+ * @param name the package name.
+ * @see #getClassLoader()
+ * @see #toClass(CtClass)
+ * @see CtClass#toClass()
+ * @since 3.16
+ */
+ public void makePackage(ClassLoader loader, String name)
+ throws CannotCompileException
+ {
+ Object[] args = new Object[] {
+ name, null, null, null, null, null, null, null };
+ Throwable t;
+ try {
+ toClass2(definePackage, loader, args);
+ return;
+ }
+ catch (java.lang.reflect.InvocationTargetException e) {
+ t = e.getTargetException();
+ if (t == null)
+ t = e;
+ else if (t instanceof IllegalArgumentException) {
+ // if the package is already defined, an IllegalArgumentException
+ // is thrown.
+ return;
+ }
+ }
+ catch (Exception e) {
+ t = e;
+ }
+
+ throw new CannotCompileException(t);
+ }
}
diff --git a/src/main/javassist/Loader.java b/src/main/javassist/Loader.java
index f6ee963e..efbcf732 100644
--- a/src/main/javassist/Loader.java
+++ b/src/main/javassist/Loader.java
@@ -430,18 +430,4 @@ public class Loader extends ClassLoader {
else
return findSystemClass(classname);
}
-
- protected Package getPackage(String name) {
- return super.getPackage(name);
- }
- /*
- // Package p = super.getPackage(name);
- Package p = null;
- if (p == null)
- return definePackage(name, null, null, null,
- null, null, null, null);
- else
- return p;
- }
- */
}
diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java
index 93177f6e..78bfd99e 100644
--- a/src/test/javassist/JvstTest4.java
+++ b/src/test/javassist/JvstTest4.java
@@ -587,4 +587,15 @@ public class JvstTest4 extends JvstTestRoot {
}
});
}
+
+ public void testPackage() throws Throwable { // JASSIST-147
+ String packageName = "test4.pack";
+ ClassPool pool = ClassPool.getDefault();
+ pool.makePackage(pool.getClassLoader(), packageName);
+ pool.makePackage(pool.getClassLoader(), packageName);
+ CtClass ctcl = pool.makeClass("test4.pack.Clazz");
+ Class cl = ctcl.toClass();
+ Object obj = cl.newInstance();
+ assertEquals(packageName, obj.getClass().getPackage().getName());
+ }
}