Skip to content

LambdaFetchingSupport causes NoClassDefFoundError when having multiple classloader (OSGi use case) #3113

@LanaLkv

Description

@LanaLkv

We run into an issue with graphql.schema.fetching.LambdaFetchingSupport#mkCallFunction when having multiple classloader in our OSGi application.
The 'targetClass' parameter is already loaded with the correct classloader, but later the function call tries to load the class with the classloader of graphql-java, which produces NoClassDefFoundError.

It can also be reproduced in non-OSGi use case with a test that has two class loader:

class LambdaFetchingSupportTest extends Specification {
    def "Multiple classloader in LambdaFetchingSupport"() {
        when: "Load class using GroovyShell script in order to get different classloader"
        def shell = new GroovyShell(this.getClass().getClassLoader())
        def result = shell.run(''' package multiple.classloader.test

class MyClass{

    String getName(){
        return "Hello"
    }
}
return new MyClass()
''', "Test.script", [])

        then: "We have two different classloader now"
        result.getClass().getClassLoader() != this.getClass().getClassLoader()

        when:
        def loader1 = this.getClass().getClassLoader()
        def myClass = result.getClass()
        def myClassLoader = myClass.getClassLoader()

        // Loading MyClass with its loader.
        myClassLoader.loadClass(myClass.getName())
        then:
        noExceptionThrown()

        when: "Loading MyClass with another loader"
        loader1.loadClass(myClass.getName())
        then:
        thrown(ClassNotFoundException)

        when: "Also the LambdaFetchingSupport tries to load MyClass with different loader"
        def function = LambdaFetchingSupport.mkCallFunction(myClass, "getName", String.class)
        def expectedRes = function.apply(result)

        then:
        expectedRes == "Hello" // But gets NoClassDefFoundError
    }
}

Thank you in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions