Skip to content

Commit

Permalink
WELD-2744 Change how we create ClassFile instances to avoid instantia…
Browse files Browse the repository at this point in the history
…ting DefaultClassFactory.INSTANCE
  • Loading branch information
manovotn committed Jun 12, 2023
1 parent 3bae436 commit 19b0ffb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jboss.weld.bean.proxy;

import org.jboss.classfilewriter.ClassFactory;
import org.jboss.weld.bean.proxy.util.WeldDefaultProxyServices;

import java.security.ProtectionDomain;

/**
* A dummy implementation which has only one purpose - to avoid instantiating {@code DefaultClassFactory.INSTANCE}.
* The sole method in this class is never used as we define classes using different means that further vary
* between in-container (such as WildFly) and SE setups.
* <p>
* See {@link WeldDefaultProxyServices#defineClass(Class, String, byte[], int, int)} for details on how we define
* classes.
*/
class DummyClassFactoryImpl implements ClassFactory {

private DummyClassFactoryImpl() {
}

// final so that there's only one instance that's being referenced from anywhere
static final DummyClassFactoryImpl INSTANCE = new DummyClassFactoryImpl();

@Override
public Class<?> defineClass(ClassLoader loader, String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError {
throw new UnsupportedOperationException("DummyClasFactoryImpl should not be used to define classes");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,10 @@ private Class<T> createProxyClass(Class<?> originalClass, String proxyClassName)

private ClassFile newClassFile(String name, int accessFlags, String superclass, String... interfaces) {
try {
return new ClassFile(name, accessFlags, superclass, interfaces);
// We need to use a (non-deprecated) method that avoids instantiating DefaultClassFactory.INSTANCE
// If that happens, we will have module accessibility issues and the need to use --add-opens clausules
// NOTE: the CL and ClassFactory are never really used to define the class, see WeldDefaultProxyServices
return new ClassFile(name, accessFlags, superclass, ProxyFactory.class.getClassLoader(), DummyClassFactoryImpl.INSTANCE, interfaces);
} catch (Exception e) {
throw BeanLogger.LOG.unableToCreateClassFile(name, e.getCause());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Class<?> defineClass(Class<?> originalClass, String className, byte[] cla
ClassLoader originalLoader = originalClass.getClassLoader();
if (originalLoader == null) {
originalLoader = Thread.currentThread().getContextClassLoader();
// is it's still null we cannot solve this issue and we need to throw an exception
// if it's still null we cannot solve this issue, and we need to throw an exception
if (originalLoader == null) {
throw BeanLogger.LOG.cannotDetermineClassLoader(className, originalClass);
}
Expand Down

0 comments on commit 19b0ffb

Please sign in to comment.