Skip to content

LoadClass keeps a reference to ScanResult #399

@cdprete

Description

@cdprete

Loading a class with the loadClass method causes the loaded class to have a ClassGraph specific class loader which has still a reference to the ScanResult object returned by the scan() method.

Trying to access an inner, non Java default, property of the loaded classes outside the use{...} block causes in Kotlin an error like "The scan result cannot be access after it has been closed".

Example:

// given 
enum class MyEnum { SOMETHING }

interface MyInterface {
    val myEnum: MyEnum
}

class MyClass : MyInterface {
    override val myEnum: MyEnum
        get() = SOMETHING
}

val myList = mutableListOf<MyInterface>()
ClassGraph()...config...scan().use {
    it.getClassesImplementing(MyInterface::class.java.canonicalName).forEach {
        myList.add(it.loadClass(MyInterface::class.java))
    }
}

// then, outside the use block (given that there is an element)
print(myList[0].myEnum)

I would expect that once loaded the class is completely unbound from ClassGraph and the ScanResult

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions