Browse Source

fixed JASSIST-147

git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@610 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
tags/rel_3_17_1_ga
chiba 12 years ago
parent
commit
b83522334f
4 changed files with 65 additions and 18 deletions
  1. 1
    1
      Readme.html
  2. 53
    3
      src/main/javassist/ClassPool.java
  3. 0
    14
      src/main/javassist/Loader.java
  4. 11
    0
      src/test/javassist/JvstTest4.java

+ 1
- 1
Readme.html View File

@@ -283,7 +283,7 @@ see javassist.Dump.

<p>-version 3.16
<ul>
<li>JIRA JASSIST-126, 127, 144, 145, 146
<li>JIRA JASSIST-126, 127, 144, 145, 146, 147, 149.
<li><code>javassist.bytecode.analysis.ControlFlow</code> was added.
</ul>


+ 53
- 3
src/main/javassist/ClassPool.java View File

@@ -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);
}
}

+ 0
- 14
src/main/javassist/Loader.java View File

@@ -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;
}
*/
}

+ 11
- 0
src/test/javassist/JvstTest4.java View File

@@ -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());
}
}

Loading…
Cancel
Save