}
// separate implementation so we don't need SoftReference to hold the field...
- private static final class CacheImpl implements Cache {
+ private static final class CacheImpl implements Cache {
private java.lang.ref.SoftReference toStringCacheRef;
- public CacheImpl() {
- toStringCacheRef = new java.lang.ref.SoftReference(new String[3]);
+
+ public CacheImpl() {
+ makeCache();
}
-
+
public String get(int cacheOffset) {
- String[] cachedArray = array();
- if (cachedArray == null) {
- return null;
- }
- return cachedArray[cacheOffset];
+ String[] cachedArray = array();
+ if (cachedArray == null) {
+ return null;
+ }
+ return cachedArray[cacheOffset];
}
public void set(int cacheOffset, String result) {
- array()[cacheOffset] = result;
+ String[] cachedArray = array();
+ if (cachedArray == null) {
+ cachedArray = makeCache();
+ }
+ cachedArray[cacheOffset] = result;
}
-
+
private String[] array() {
- return (String[])toStringCacheRef.get();
+ return (String[]) toStringCacheRef.get();
}
+
+ private String[] makeCache() {
+ String[] array = new String[3];
+ toStringCacheRef = new java.lang.ref.SoftReference(array);
+ return array;
+ }
+
}
}
*******************************************************************************/
package org.aspectj.runtime.reflect;
+import java.lang.ref.Reference;
+import java.lang.reflect.Field;
+
import junit.framework.TestCase;
/**
assertSame(longString,msi.toLongString()); // should be cached.
assertTrue("String representations should be different",!(shortString.equals(middleString) || middleString.equals(longString) || longString.equals(shortString)));
}
+
+ public void testClearCache() throws Exception {
+ MethodSignatureImpl msi = new MethodSignatureImpl(0,"test",SignatureTest.class,new Class[] { String.class, Integer.TYPE }, new String[] { "s", "i" }, new Class[] {}, Runnable.class);
+ String shortString = msi.toShortString();
+ assertSame(shortString,msi.toShortString());
+
+ Field field = SignatureImpl.class.getDeclaredField("stringCache");
+ field.setAccessible(true);
+ Object res = field.get(msi);
+
+ field = res.getClass().getDeclaredField("toStringCacheRef");
+ field.setAccessible(true);
+ Reference ref = (Reference)field.get(res);
+
+ ref.clear();
+ assertEquals(shortString,msi.toShortString());
+
+ String longString = msi.toLongString();
+ assertSame(longString,msi.toLongString()); // should be cached.
+ }
}