Skip to content

Commit 014ad9d

Browse files
committed
No need to use locks for reflection stuff. Use single-racy reads instead.
1 parent 816f585 commit 014ad9d

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,13 +1029,16 @@ public void flushGeneratedClassesFor(String className) {
10291029
}
10301030
}
10311031

1032-
private Unsafe unsafe;
1032+
private static Unsafe unsafe;
1033+
private static Method defineClassMethod;
10331034

1034-
private Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
1035+
private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
1036+
Unsafe unsafe = ClassLoaderWeavingAdaptor.unsafe;
10351037
if (unsafe == null) {
10361038
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
10371039
theUnsafeField.setAccessible(true);
1038-
return (Unsafe) theUnsafeField.get(null);
1040+
ClassLoaderWeavingAdaptor.unsafe = unsafe = (Unsafe) theUnsafeField.get(null);
1041+
return unsafe;
10391042
}
10401043
return unsafe;
10411044
}
@@ -1105,15 +1108,14 @@ private void defineClass(ClassLoader loader, String name, byte[] bytes, Protecti
11051108
}
11061109
} else {
11071110
try {
1108-
if (defineClassMethod == null) {
1109-
synchronized (lock) {
1110-
getUnsafe();
1111-
defineClassMethod =
1112-
Unsafe.class.getDeclaredMethod("defineClass", String.class,byte[].class,Integer.TYPE,Integer.TYPE, ClassLoader.class,ProtectionDomain.class);
1113-
}
1111+
Unsafe unsafe = getUnsafe();
1112+
Method defineClass = defineClassMethod;
1113+
if (defineClass == null) {
1114+
defineClass = Unsafe.class.getDeclaredMethod("defineClass", String.class,byte[].class,Integer.TYPE,Integer.TYPE, ClassLoader.class,ProtectionDomain.class);
1115+
defineClass.setAccessible(true);
1116+
defineClassMethod = defineClass;
11141117
}
1115-
defineClassMethod.setAccessible(true);
1116-
clazz = defineClassMethod.invoke(getUnsafe(), name,bytes,0,bytes.length,loader,protectionDomain);
1118+
clazz = defineClassMethod.invoke(unsafe, name,bytes,0,bytes.length,loader,protectionDomain);
11171119
} catch (LinkageError le) {
11181120
le.printStackTrace();
11191121
// likely thrown due to defining something that already exists?
@@ -1130,8 +1132,6 @@ private void defineClass(ClassLoader loader, String name, byte[] bytes, Protecti
11301132
trace.exit("defineClass", clazz);
11311133
}
11321134
}
1133-
static Method defineClassMethod;
1134-
private static final Object lock = new Object();
11351135

11361136

11371137
// /*

0 commit comments

Comments
 (0)