[jboss-cvs] javassist SVN: r612 - in trunk/src: test and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sat Feb 11 09:54:44 EST 2012
Author: chiba
Date: 2012-02-11 09:54:43 -0500 (Sat, 11 Feb 2012)
New Revision: 612
Modified:
trunk/src/main/javassist/compiler/MemberResolver.java
trunk/src/test/Jassist150.java
trunk/src/test/javassist/JvstTest4.java
Log:
fixed JASSIST-150 (fixed a bug on WeakHashMap)
Modified: trunk/src/main/javassist/compiler/MemberResolver.java
===================================================================
--- trunk/src/main/javassist/compiler/MemberResolver.java 2012-02-11 03:12:41 UTC (rev 611)
+++ trunk/src/main/javassist/compiler/MemberResolver.java 2012-02-11 14:54:43 UTC (rev 612)
@@ -17,6 +17,7 @@
package javassist.compiler;
import java.util.Hashtable;
+import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
import java.util.List;
import java.util.Iterator;
@@ -416,14 +417,20 @@
private static WeakHashMap invalidNamesMap = new WeakHashMap();
private Hashtable invalidNames = null;
+ // for unit tests
+ public static int getInvalidMapSize() { return invalidNamesMap.size(); }
+
private Hashtable getInvalidNames() {
Hashtable ht = invalidNames;
if (ht == null) {
synchronized (MemberResolver.class) {
- ht = (Hashtable)invalidNamesMap.get(classPool);
+ WeakReference ref = (WeakReference)invalidNamesMap.get(classPool);
+ if (ref != null)
+ ht = (Hashtable)ref.get();
+
if (ht == null) {
ht = new Hashtable();
- invalidNamesMap.put(classPool, ht);
+ invalidNamesMap.put(classPool, new WeakReference(ht));
}
}
Modified: trunk/src/test/Jassist150.java
===================================================================
--- trunk/src/test/Jassist150.java 2012-02-11 03:12:41 UTC (rev 611)
+++ trunk/src/test/Jassist150.java 2012-02-11 14:54:43 UTC (rev 612)
@@ -5,64 +5,88 @@
import javassist.NotFoundException;
public class Jassist150 {
+ public static final String BASE_PATH = "./";
+ public static final String JAVASSIST_JAR = BASE_PATH + "javassist.jar";
+ public static final String CLASSES_FOLDER = BASE_PATH + "build/classes";
+ public static final String TEST_CLASSES_FOLDER = BASE_PATH
+ + "build/test-classes";
- public static final String BASE_PATH="./";
- public static final String JAVASSIST_JAR=BASE_PATH+"javassist.jar";
- public static final String CLASSES_FOLDER=BASE_PATH+"build/classes";
- public static final String TEST_CLASSES_FOLDER=BASE_PATH+"build/test-classes";
-
public static class Inner1 {
- public static int get() {
- return 0;
- }
+ public static int get() {
+ return 0;
+ }
}
- public static void implTestClassTailCache() throws NotFoundException, CannotCompileException {
- ClassPool pool = new ClassPool(true);
- for(int paths=0; paths<50; paths++) {
- pool.appendClassPath(JAVASSIST_JAR);
- pool.appendClassPath(CLASSES_FOLDER);
- pool.appendClassPath(TEST_CLASSES_FOLDER);
- }
- CtClass cc = pool.get("Jassist150$Inner1");
- CtMethod ccGet = cc.getDeclaredMethod("get");
- String code1 = "{ int n1 = Integer.valueOf(1); " +
- " int n2 = Integer.valueOf(2); " +
- " int n3 = Integer.valueOf(3); " +
- " int n4 = Integer.valueOf(4); " +
- " int n5 = Integer.valueOf(5); " +
- " return n1+n2+n3+n4+n5; }";
- String code2 = "{ int n1 = java.lang.Integer.valueOf(1); " +
- " int n2 = java.lang.Integer.valueOf(2); " +
- " int n3 = java.lang.Integer.valueOf(3); " +
- " int n4 = java.lang.Integer.valueOf(4); " +
- " int n5 = java.lang.Integer.valueOf(5); " +
- " return n1+n2+n3+n4+n5; }";
- String code3 = "{ int n1 = java.lang.Integer#valueOf(1); " +
- " int n2 = java.lang.Integer#valueOf(2); " +
- " int n3 = java.lang.Integer#valueOf(3); " +
- " int n4 = java.lang.Integer#valueOf(4); " +
- " int n5 = java.lang.Integer#valueOf(5); " +
- " return n1+n2+n3+n4+n5; }";
- loop(cc, ccGet, code1);
+ public static void implTestClassTailCache() throws NotFoundException,
+ CannotCompileException {
+ ClassPool pool = new ClassPool(true);
+ for (int paths = 0; paths < 50; paths++) {
+ pool.appendClassPath(JAVASSIST_JAR);
+ pool.appendClassPath(CLASSES_FOLDER);
+ pool.appendClassPath(TEST_CLASSES_FOLDER);
+ }
+ CtClass cc = pool.get("Jassist150$Inner1");
+ CtMethod ccGet = cc.getDeclaredMethod("get");
+ String code1 = "{ int n1 = Integer.valueOf(1); "
+ + " int n2 = Integer.valueOf(2); "
+ + " int n3 = Integer.valueOf(3); "
+ + " int n4 = Integer.valueOf(4); "
+ + " int n5 = Integer.valueOf(5); "
+ + " return n1+n2+n3+n4+n5; }";
+ String code2 = "{ int n1 = java.lang.Integer.valueOf(1); "
+ + " int n2 = java.lang.Integer.valueOf(2); "
+ + " int n3 = java.lang.Integer.valueOf(3); "
+ + " int n4 = java.lang.Integer.valueOf(4); "
+ + " int n5 = java.lang.Integer.valueOf(5); "
+ + " return n1+n2+n3+n4+n5; }";
+ String code3 = "{ int n1 = java.lang.Integer#valueOf(1); "
+ + " int n2 = java.lang.Integer#valueOf(2); "
+ + " int n3 = java.lang.Integer#valueOf(3); "
+ + " int n4 = java.lang.Integer#valueOf(4); "
+ + " int n5 = java.lang.Integer#valueOf(5); "
+ + " return n1+n2+n3+n4+n5; }";
+ loop(cc, ccGet, code1);
}
- public static void loop(CtClass cc, CtMethod ccGet, String code) throws CannotCompileException {
+ public static void loop(CtClass cc, CtMethod ccGet, String code)
+ throws CannotCompileException {
long startTime = System.currentTimeMillis();
- for(int replace=0; replace<1000; replace++) {
+ for (int replace = 0; replace < 1000; replace++) {
ccGet.setBody(code);
}
long endTime = System.currentTimeMillis();
- System.out.println("Test: Time (ms) "+(endTime-startTime));
+ System.out.println("Test: Time (ms) " + (endTime - startTime));
}
+ public static void implTestClassTailCache2() throws NotFoundException,
+ CannotCompileException {
+ ClassPool pool = new ClassPool(true);
+ for (int paths = 0; paths < 50; paths++) {
+ pool.appendClassPath(JAVASSIST_JAR);
+ pool.appendClassPath(CLASSES_FOLDER);
+ pool.appendClassPath(TEST_CLASSES_FOLDER);
+ }
+ CtClass cc = pool.get("Jassist150$Inner1");
+ CtMethod ccGet = cc.getDeclaredMethod("get");
+ String code3 = "{ int n1 = java.lang.Integer#valueOf(1); "
+ + " int n2 = java.lang.Integer#valueOf(2); "
+ + " int n3 = java.lang.Integer#valueOf(3); "
+ + " int n4 = java.lang.Integer#valueOf(4); "
+ + " int n5 = java.lang.Integer#valueOf(5); "
+ + " return n1+n2+n3+n4+n5; }";
+ ccGet.setBody(code3);
+ }
+
public static void main(String[] args) {
- for (int loop = 0; loop < 5; loop++) {
- try {
+ for (int loop = 0; loop < 5; loop++) {
+ try {
implTestClassTailCache();
+ for (int i = 0; i < 100; i++)
+ implTestClassTailCache2();
} catch (Exception e) {
e.printStackTrace();
}
+ }
+ System.out.println("size: " + javassist.compiler.MemberResolver.getInvalidMapSize());
}
- }
-}
\ No newline at end of file
+}
Modified: trunk/src/test/javassist/JvstTest4.java
===================================================================
--- trunk/src/test/javassist/JvstTest4.java 2012-02-11 03:12:41 UTC (rev 611)
+++ trunk/src/test/javassist/JvstTest4.java 2012-02-11 14:54:43 UTC (rev 612)
@@ -657,4 +657,31 @@
assertTrue(t2 < t1 * 2);
assertTrue(t3 < t1 * 2);
}
+
+ public void testJIRA150b() throws Exception {
+ int N = 100;
+ for (int k = 0; k < N; k++) {
+ ClassPool pool = new ClassPool(true);
+ for(int paths=0; paths<50; paths++) {
+ pool.appendClassPath(JAVASSIST_JAR);
+ pool.appendClassPath(CLASSES_FOLDER);
+ pool.appendClassPath(TEST_CLASSES_FOLDER);
+ }
+ CtClass cc = pool.get("Jassist150$Inner1");
+ CtMethod ccGet = cc.getDeclaredMethod("get");
+ for(int replace=0; replace < 5; replace++) {
+ ccGet.setBody(
+ "{ int n1 = java.lang.Integer#valueOf(1); " +
+ " int n2 = java.lang.Integer#valueOf(2); " +
+ " int n3 = java.lang.Integer#valueOf(3); " +
+ " int n4 = java.lang.Integer#valueOf(4); " +
+ " int n5 = java.lang.Integer#valueOf(5); " +
+ " return n1+n2+n3+n4+n5; }");
+ }
+ }
+ System.gc();
+ int size = javassist.compiler.MemberResolver.getInvalidMapSize();
+ System.out.println("JIRA150b " + size);
+ assertTrue(size < N - 10);
+ }
}
More information about the jboss-cvs-commits
mailing list