Skip to content

Commit

Permalink
JPMS improvements: Experiments with GF Embedded
Browse files Browse the repository at this point in the history
Discovered a flaw in the solution for ProxyServiceImpl
  • Loading branch information
OndroMih committed Sep 6, 2024
1 parent 7cd0dc3 commit 3e85563
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,6 @@ public final class ClassGenerator {

private static final Logger LOG = Logger.getLogger(ClassGenerator.class.getName());

// private static Method defineClassMethod;
// private static Method defineClassMethodSM;
//
// static {
// try {
// final PrivilegedExceptionAction<Void> action = () -> {
// final Class<?> cl = Class.forName("java.lang.ClassLoader");
// final String name = "defineClass";
// defineClassMethod = cl.getDeclaredMethod(name, String.class, byte[].class, int.class, int.class);
// ClassLoaderMethods.defineClassMethod.setAccessible(true);
// defineClassMethodSM = cl.getDeclaredMethod(
// name, String.class, byte[].class, int.class, int.class, ProtectionDomain.class);
// ClassLoaderMethods.defineClassMethodSM.setAccessible(true);
// return null;
// };
// AccessController.doPrivileged(action);
// LOG.config("ClassLoader methods capable of generating classes were successfully detected.");
// } catch (final Exception e) {
// throw new Error("Could not initialize access to ClassLoader.defineClass method.", e);
// }
// }


private ClassGenerator() {
// hidden
}
Expand Down Expand Up @@ -256,7 +233,7 @@ private static boolean useMethodHandles(final ClassLoader loader, final Class<?>
}
// The bootstrap CL used by embedded glassfish doesn't remember generated classes
// Further ClassLoader.findClass calls will fail.
if (anchorClass == null || loader.getParent() == null || loader.getClass() == ASURLClassLoader.class) {
if (anchorClass == null || loader.getClass() == ASURLClassLoader.class) {
return false;
}
// Use MethodHandles.Lookup only if the anchor run-time Package defined by CL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class ProxyServicesImpl implements ProxyServices {

private final ClassLoaderHierarchy classLoaderHierarchy;

private Map<ClassLoader, ClassPool> classPoolMap = Collections.synchronizedMap(new WeakHashMap<>());
// probably shouldn't be static but moved to a singleton service
private static Map<ClassLoader, ClassPool> classPoolMap = Collections.synchronizedMap(new WeakHashMap<>());

/**
* @param services immediately used to find a {@link ClassLoaderHierarchy} service
Expand All @@ -59,13 +60,16 @@ public ProxyServicesImpl(final ServiceLocator services) {
classLoaderHierarchy = services.getService(ClassLoaderHierarchy.class);
}

/* This solution is not ideal - it creates the new class in the CL of the originalClass
(most often server CL or bootstrap CL), while the previous solution created it in the app classloader.
*/
@Override
public Class<?> defineClass(final Class<?> originalClass, final String className, final byte[] classBytes,
final int off, final int len) throws ClassFormatError {
try {
final String originalPackageName = originalClass.getPackageName();
String modifiedClassName = originalClass.getName() + "_GlassFishWeldProxy";
final ClassLoader originalClassCL = getClassLoaderforBean(originalClass);
final ClassLoader originalClassCL = originalClass.getClassLoader();
final ClassPool classPool = classPoolMap.computeIfAbsent(originalClassCL, cl -> new ClassPool());
while (classPool.getOrNull(modifiedClassName) != null) {
modifiedClassName += "_";
Expand Down

0 comments on commit 3e85563

Please sign in to comment.