Skip to content

Add classLoader to mapper options builder#1394

Merged
evanchooly merged 1 commit intoMorphiaOrg:masterfrom
jonahseguin:master
Oct 31, 2019
Merged

Add classLoader to mapper options builder#1394
evanchooly merged 1 commit intoMorphiaOrg:masterfrom
jonahseguin:master

Conversation

@jonahseguin
Copy link

This is necessary for some use cases (for example, when working with Spigot/Bukkit JavaPlugins) (see https://spigotmc.org) when the PluginClassLoader is used. In order to prevent NoClassDefFoundError for entity classes, the JavaPlugin's ClassLoader needs to be used, and I'm sure this is applicable to other applications as well.

Example of my use in the previous version (when it was possible to override the DefaultCreator's getClassLoaderForClass() method):

MapperOptions options = morphia.getMapper().getOptions();
        morphia.getMapper().setOptions(MapperOptions.builder(options)
                .objectFactory(new DefaultCreator(options) {
                    @Override
                    protected ClassLoader getClassLoaderForClass() {
                        return myClassLoader;
                    }
                })
                .build());

Example usage with this commit:

morphia.getMapper().setOptions(MapperOptions.builder(morphia.getMapper().getOptions())
                .classLoader(myClassLoader)
                .build());

@evanchooly
Copy link
Member

Have you tried this with 1.5.7? It was just published in the last day or two and I've done a fair bit of work in this exact area.

@jonahseguin
Copy link
Author

@evanchooly Yeah, just tried with 1.5.7 -- the classLoader variable is read-only in the MapperOptions class and there is no option for it in the builder

@evanchooly
Copy link
Member

and it's still necessary? That's disappointing...

Not the end of the world, though, i guess. It's a nice escape hatch, I suppose, for when morphia can't quite figure it all out. I'll take a look in the next day or two then.

@jonahseguin
Copy link
Author

@evanchooly Yeah, just for some use cases where we need to provide our own class loader.
Thanks

@evanchooly evanchooly added this to the 1.5.8 milestone Oct 31, 2019
@evanchooly evanchooly merged commit 776c737 into MorphiaOrg:master Oct 31, 2019
@evanchooly
Copy link
Member

I just pushed 1.5.8 so you should see this on central in the next hour or two.

evanchooly added a commit that referenced this pull request Nov 1, 2019
evanchooly added a commit that referenced this pull request Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants