Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ManualMorphiaConfig implements MorphiaConfig {
Boolean applyCaps;
Boolean applyDocumentValidations;
Boolean applyIndexes;
ClassLoader classLoader;

Optional<CodecProvider> codecProvider;

Expand Down Expand Up @@ -69,6 +70,7 @@ public ManualMorphiaConfig(MorphiaConfig base) {
applyCaps = base.applyCaps();
applyDocumentValidations = base.applyDocumentValidations();
applyIndexes = base.applyIndexes();
classLoader = base.classLoader();
codecProvider = base.codecProvider();
collectionNaming = base.collectionNaming();
database = base.database();
Expand Down Expand Up @@ -122,6 +124,11 @@ public Boolean applyCaps() {
return orDefault(applyCaps, FALSE);
}

@Override
public ClassLoader classLoader() {
return orDefault(classLoader, Thread.currentThread().getContextClassLoader());
}

public Boolean applyDocumentValidations() {
return orDefault(applyDocumentValidations, FALSE);
}
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/dev/morphia/config/MorphiaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@
@ConfigMapping(prefix = "morphia")
public interface MorphiaConfig {

/**
* The classloader to use for class lookups and bytecode generation.
*
* @return the configured classloader
* @since 3.0
*/
default ClassLoader classLoader() {
return Thread.currentThread().getContextClassLoader();
}

/**
* Updates this configuration with a new value and returns a new instance. The original instance is unchanged.
*
* @param value the new value
* @return a new instance with the updated configuration
* @since 3.0
*/
default MorphiaConfig classLoader(ClassLoader value) {
var newConfig = new ManualMorphiaConfig(this);

newConfig.classLoader = value;
return newConfig;
}

/**
* Tries to load a configuration from the default location.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public CritterClassLoader(ClassLoader parent) {
super(parent, Collections.emptyMap());
}

public CritterClassLoader() {
this(Thread.currentThread().getContextClassLoader());
}

public void register(String name, byte[] bytes) {
typeDefinitions.put(name, bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/dev/morphia/mapping/AbstractMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public abstract class AbstractMapper implements Mapper {
@MorphiaInternal
protected AbstractMapper(MorphiaConfig config) {
this.config = config;
this.contextClassLoader = Thread.currentThread().getContextClassLoader();
this.contextClassLoader = config.classLoader();
this.discriminatorLookup = new DiscriminatorLookup();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class GeneratorTest {
public static final CritterEntityModel entityModel;
public static final CritterClassLoader critterClassLoader = new CritterClassLoader();
public static final CritterClassLoader critterClassLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

static {
ClassGraph classGraph = new ClassGraph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.testng.annotations.DataProvider;

public class TestAccessorsMutators extends BaseCritterTest {
private final CritterClassLoader critterClassLoader = new CritterClassLoader();
private final CritterClassLoader critterClassLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

// @Test(dataProvider = "classes")
public void testPropertyAccessors(Class<?> type) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TestEntityModelGenerator {

private final CritterEntityModel control;
private final Mapper mapper = new ReflectiveMapper(new ManualMorphiaConfig());
private final CritterClassLoader critterClassLoader = new CritterClassLoader();
private final CritterClassLoader critterClassLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

public TestEntityModelGenerator() {
CritterEntityModel tmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.testng.annotations.NoInjection;

public class TestPropertyModelGenerator extends BaseCritterTest {
private final CritterClassLoader critterClassLoader = new CritterClassLoader();
private final CritterClassLoader critterClassLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

// @Test(dataProvider = "properties", testName = "")
public void testProperty(String control, String methodName, @NoInjection Method method) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TestVarHandleAccessor {

@BeforeClass
public void setup() {
classLoader = new CritterClassLoader();
classLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());
MorphiaConfig config = new ManualMorphiaConfig();
CritterGizmoGenerator.generate(Example.class, classLoader, new Generators(config, new ReflectiveMapper(config)), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

public class TestGizmoGeneration {
private final CritterClassLoader critterClassLoader = new CritterClassLoader();
private final CritterClassLoader critterClassLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

@Test
public void testMapStringExample() {
Expand Down Expand Up @@ -262,7 +262,7 @@ public void testConstructors() throws Exception {

@Test
public void testMethodBasedAccessors() throws Exception {
CritterClassLoader classLoader = new CritterClassLoader();
CritterClassLoader classLoader = new CritterClassLoader(Thread.currentThread().getContextClassLoader());

String resourceName = MethodExample.class.getName().replace('.', '/') + ".class";
var inputStream = MethodExample.class.getClassLoader().getResourceAsStream(resourceName);
Expand Down