Skip to content

Commit 1f1d429

Browse files
KimmingLaukriegaex
andcommitted
Improved fix for #285
1. Write SAME_BYTES to cacheMap when woven bytes are null 2. Fix TODO in SimpleCache::getAndInitialize, using Optional to help indicate cache hit for unwoven class 3. Improve test coverage (cache miss, cache hit for unwoven class) Relates to #285. Co-authored-by: Alexander Kriegisch <[email protected]> Signed-off-by: KimmingLau <[email protected]> Signed-off-by: Alexander Kriegisch <[email protected]>
1 parent 1cdf711 commit 1f1d429

File tree

3 files changed

+71
-48
lines changed

3 files changed

+71
-48
lines changed

loadtime/src/main/java/org/aspectj/weaver/loadtime/Aj.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
2324
import java.util.Set;
2425
import java.util.StringTokenizer;
2526

@@ -104,9 +105,9 @@ public byte[] preProcess(String className, final byte[] bytes, ClassLoader class
104105
synchronized (classLoader) {
105106

106107
if (SimpleCacheFactory.isEnabled()) {
107-
byte[] cacheBytes= laCache.getAndInitialize(className, bytes, classLoader, protectionDomain);
108-
if (cacheBytes!=null){
109-
return cacheBytes;
108+
Optional<byte[]> cacheBytes = laCache.getAndInitialize(className, bytes, classLoader, protectionDomain);
109+
if (cacheBytes != null){
110+
return cacheBytes.orElse(null);
110111
}
111112
}
112113

weaver/src/main/java/org/aspectj/weaver/tools/cache/SimpleCache.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Collections;
1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import java.util.Optional;
1819
import java.util.zip.CRC32;
1920

2021
import org.aspectj.weaver.Dump;
@@ -37,7 +38,7 @@
3738
public class SimpleCache {
3839

3940
private static final String SAME_BYTES_STRING = "IDEM";
40-
private static final byte[] SAME_BYTES = SAME_BYTES_STRING.getBytes();
41+
static final byte[] SAME_BYTES = SAME_BYTES_STRING.getBytes();
4142

4243
private Map<String, byte[]> cacheMap;
4344
private boolean enabled = false;
@@ -64,22 +65,20 @@ protected SimpleCache(String folder, boolean enabled) {
6465
}
6566
}
6667

67-
public byte[] getAndInitialize(String classname, byte[] bytes,
68+
public Optional<byte[]> getAndInitialize(String classname, byte[] bytes,
6869
ClassLoader loader, ProtectionDomain protectionDomain) {
6970
if (!enabled) {
7071
return null;
7172
}
7273
byte[] res = get(classname, bytes);
7374

7475
if (Arrays.equals(SAME_BYTES, res)) {
75-
return null;
76-
} else {
77-
if (res != null) {
78-
initializeClass(classname, res, loader, protectionDomain);
79-
}
80-
return res;
76+
return Optional.empty();
77+
} else if (res != null) {
78+
initializeClass(classname, res, loader, protectionDomain);
79+
return Optional.of(res);
8180
}
82-
81+
return null;
8382
}
8483

8584
private byte[] get(String classname, byte bytes[]) {

weaver/src/test/java/org/aspectj/weaver/tools/cache/SimpleClassCacheTest.java

+59-36
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,83 @@
1212

1313
package org.aspectj.weaver.tools.cache;
1414

15-
import java.io.File;
16-
1715
import junit.framework.TestCase;
1816

19-
/**
20-
*/
21-
public class SimpleClassCacheTest extends TestCase {
22-
byte[] FAKE_BYTES_V1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
23-
byte[] FAKE_BYTES_V2 = {1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17+
import java.io.File;
2418

25-
byte[] FAKE_WOVEN_BYTES_V1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10};
26-
byte[] FAKE_WOVEN_BYTES_V2 = {1, 1, 2, 3, 4, 5, 6, 7, 8, 9,10};
19+
public class SimpleClassCacheTest extends TestCase {
20+
byte[] FAKE_BYTES_V1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
21+
byte[] FAKE_BYTES_V2 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2722

23+
byte[] FAKE_WOVEN_BYTES_V1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
24+
byte[] FAKE_WOVEN_BYTES_V2 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2825

29-
private SimpleCache createCache() throws Exception {
30-
return new SimpleCache(System.getProperty("java.io.tmpdir"),true);
26+
private SimpleCache createCache() {
27+
return new SimpleCache(System.getProperty("java.io.tmpdir"), true);
3128
}
3229

33-
34-
public void testCache() throws Exception {
30+
public void testCache() {
3531
String classA = "com.generated.A";
3632
SimpleCache cache = createCache();
37-
3833
cache.put(classA, FAKE_BYTES_V1, FAKE_WOVEN_BYTES_V1);
3934

35+
// Returned woven bytes are the original ones
36+
byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
37+
assertNotNull(result);
38+
for (int i = 0; i < result.length; i++)
39+
assertEquals(
40+
"Cached version byte[" + i + "] should be equal to the original woven class",
41+
result[i], FAKE_WOVEN_BYTES_V1[i]
42+
);
43+
44+
// Class is properly backed up
45+
File f = new File(System.getProperty("java.io.tmpdir") + File.separator + "com.generated.A-1164760902");
46+
assertTrue(
47+
"Class should be backed up with CRC 1164760902",
48+
f.exists()
49+
);
50+
}
51+
52+
public void testDifferentVersionCache() {
53+
String classA = "com.generated.A";
54+
SimpleCache cache = createCache();
55+
cache.put(classA, FAKE_BYTES_V1, FAKE_WOVEN_BYTES_V1);
56+
cache.put(classA, FAKE_BYTES_V2, FAKE_WOVEN_BYTES_V2);
4057

41-
// Test the returned woven bytes are the original one
42-
byte result[] = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null);
43-
for(int i = 0; i < result.length; i ++){
44-
assertEquals("Cached version byte[" +i+"] should be equal to the original woven classe",result[i],FAKE_WOVEN_BYTES_V1[i]);
45-
}
58+
// Returned woven bytes are the original ones for v1
59+
byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
60+
assertNotNull(result);
61+
for (int i = 0; i < result.length; i++)
62+
assertEquals(
63+
"Cached version v1 byte[" + i + "] should be equal to the original woven class",
64+
result[i], FAKE_WOVEN_BYTES_V1[i]
65+
);
66+
67+
// Returned woven bytes are the original ones for v2
68+
result = cache.getAndInitialize(classA, FAKE_BYTES_V2, null, null).orElse(null);
69+
assertNotNull(result);
70+
for (int i = 0; i < result.length; i++)
71+
assertEquals(
72+
"Cached version v2 byte[" + i + "] should be equal to the original woven class",
73+
result[i], FAKE_WOVEN_BYTES_V2[i]
74+
);
75+
}
4676

47-
// Assure the class is properly backed up in the backing folder
48-
File f = new File (System.getProperty("java.io.tmpdir") + File.separator + "com.generated.A-1164760902");
49-
assertTrue("Class should be backed up to backing folder, with te CRC:1164760902 ",f.exists());
77+
public void testCacheMiss() {
78+
String classA = "com.generated.A";
79+
SimpleCache cache = createCache();
5080

81+
// Woven bytes not found in cache
82+
assertNull(cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null));
5183
}
5284

53-
public void testDifferentVersionCache() throws Exception {
85+
public void testCacheHitUnwoven() {
5486
String classA = "com.generated.A";
5587
SimpleCache cache = createCache();
56-
cache.put(classA, FAKE_BYTES_V1, FAKE_WOVEN_BYTES_V1);
57-
cache.put(classA, FAKE_BYTES_V2, FAKE_WOVEN_BYTES_V2);
88+
cache.put(classA, FAKE_BYTES_V1, SimpleCache.SAME_BYTES);
5889

59-
// Test the returned woven bytes are the original one for v1
60-
byte result[] = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null);
61-
for(int i = 0; i < result.length; i ++){
62-
assertEquals("Cached version v1 byte[" +i+"] should be equal to the original woven classe",result[i],FAKE_WOVEN_BYTES_V1[i]);
63-
}
64-
65-
// Test the returned woven bytes are the original one for v2
66-
result = cache.getAndInitialize(classA, FAKE_BYTES_V2, null, null);
67-
for(int i = 0; i < result.length; i ++){
68-
assertEquals("Cached version v2 byte[" +i+"] should be equal to the original woven classe",result[i],FAKE_WOVEN_BYTES_V2[i]);
69-
}
90+
// Returned woven bytes are null, indicating an unwoven class
91+
byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
92+
assertNull(result);
7093
}
7194
}

0 commit comments

Comments
 (0)