Skip to content

Commit

Permalink
Change how we create ClassFile instances to avoid instantiating Defau…
Browse files Browse the repository at this point in the history
…ltClassFactory.INSTANCE
  • Loading branch information
manovotn committed Jun 7, 2023
1 parent 91d6c0d commit 51ce29d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.jboss.weld.Container;
import org.jboss.weld.bean.AbstractProducerBean;
import org.jboss.weld.bean.builtin.AbstractBuiltInBean;
import org.jboss.weld.bean.proxy.util.WeldDefaultProxyServices;
import org.jboss.weld.config.WeldConfiguration;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
Expand Down Expand Up @@ -499,7 +500,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(), WeldDefaultProxyServices.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 @@ -17,6 +17,7 @@

package org.jboss.weld.bean.proxy.util;

import org.jboss.classfilewriter.ClassFactory;
import org.jboss.weld.bean.proxy.ProxyFactory;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.serialization.spi.ProxyServices;
Expand Down Expand Up @@ -46,7 +47,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 Expand Up @@ -183,4 +184,25 @@ public final Class<?> publicDefineClass(String name, byte[] b, int off, int len,
}
}
}

/**
* 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.
*
* See {@link WeldDefaultProxyServices#defineClass(Class, String, byte[], int, int)} for details on how we define
* classes.
*/
public static class DummyClassFactoryImpl implements ClassFactory {

private DummyClassFactoryImpl() {}

// volatile because of possible multiple tread defining classes simultaneously
public static volatile 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");
}
}
}

0 comments on commit 51ce29d

Please sign in to comment.