Skip to content

Commit 353df8c

Browse files
committed
WIP
1 parent dff4982 commit 353df8c

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/main/java/com/flowingcode/vaadin/jsonmigration/ClassInstrumentationUtil.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.List;
3535
import java.util.Map;
36+
import java.util.WeakHashMap;
3637
import java.util.concurrent.ConcurrentHashMap;
3738
import lombok.experimental.UtilityClass;
3839
import org.objectweb.asm.ClassWriter;
@@ -62,7 +63,7 @@
6263
final class ClassInstrumentationUtil {
6364

6465
private static final Map<ClassLoader, InstrumentedClassLoader> classLoaderCache =
65-
new ConcurrentHashMap<>();
66+
new WeakHashMap<>();
6667

6768
/**
6869
* Creates and returns an instance of a dynamically instrumented class that extends the specified
@@ -122,7 +123,13 @@ public <T extends Component> Class<? extends T> instrumentClass(Class<T> parent)
122123
Constructor<?> defaultConstructor = parent.getDeclaredConstructor();
123124
if (!Modifier.isPublic(defaultConstructor.getModifiers())
124125
&& !Modifier.isProtected(defaultConstructor.getModifiers())) {
125-
defaultConstructor.setAccessible(true);
126+
try {
127+
defaultConstructor.setAccessible(true);
128+
} catch (Exception e) {
129+
throw new IllegalArgumentException(
130+
"Parent class must have an accessible no-argument constructor: " + parent.getName(),
131+
e);
132+
}
126133
}
127134
} catch (NoSuchMethodException e) {
128135
throw new IllegalArgumentException(
@@ -178,12 +185,17 @@ private static boolean hasJsonValueParameters(Method method) {
178185

179186
private <T extends Component> Class<? extends T> createInstrumentedClass(Class<T> parent,
180187
String className) throws Exception {
181-
ClassLoader parentClassLoader = parent.getClassLoader();
182188
InstrumentedClassLoader classLoader =
183-
classLoaderCache.computeIfAbsent(parentClassLoader, InstrumentedClassLoader::new);
189+
getOrCreateInstrumentedClassLoader(parent.getClassLoader());
184190
return classLoader.defineInstrumentedClass(className, parent).asSubclass(parent);
185191
}
186192

193+
private InstrumentedClassLoader getOrCreateInstrumentedClassLoader(ClassLoader parent) {
194+
synchronized (classLoaderCache) {
195+
return classLoaderCache.computeIfAbsent(parent, InstrumentedClassLoader::new);
196+
}
197+
}
198+
187199
private static final class InstrumentedClassLoader extends ClassLoader {
188200

189201
private final Map<Class<?>, Class<?>> instrumentedClassCache = new ConcurrentHashMap<>();
@@ -289,7 +301,11 @@ private void generateMethodOverride(ClassWriter cw, Method method, String intern
289301
}
290302

291303
// Return converted result or void
292-
mv.visitInsn(Opcodes.ARETURN);
304+
if (method.getReturnType() == Void.TYPE) {
305+
mv.visitInsn(Opcodes.RETURN);
306+
} else {
307+
mv.visitInsn(Opcodes.ARETURN);
308+
}
293309

294310
mv.visitMaxs(0, 0);
295311
mv.visitEnd();

0 commit comments

Comments
 (0)