reflections icon indicating copy to clipboard operation
reflections copied to clipboard

could not create Vfs.Dir from url

Open RoRoche opened this issue 4 years ago • 2 comments

Hi,

I'm developing a custom Gradle plugin based on reflections: https://github.com/RoRoche/plantuml-gradle-plugin/tree/feature/using_reflections

I have to deal with some gradle specificities such as:

        final URL[] urls = project.sourceSets.main.output.classesDirs.files.collect {
            if (it != null) {
                it.toURI().toURL()
            }
        }.findAll {
            it != null
        } as URL[]
        getLogger().debug(
                "URLs to scan: " + urls
        )
        return new URLClassLoader(urls)

And then I construct the reflection such as:

/**
 * Utility class to find [Classes] in a given package.
 *
 * @property packageName The name of the package to scan.
 * @property reflections Utility to find classes in package.
 */
class ClsInPackage(
    private val packageName: String,
    private val reflections: Reflections
) : Classes {

    /**
     * Secondary constructor with reflections configuration.
     *
     * @param packageName The name of the package to scan.
     * @param configuration The [Configuration] to use.
     */
    constructor(
        packageName: String,
        configuration: Configuration
    ) : this(
        packageName = packageName,
        reflections = Reflections(configuration)
    )

    /**
     * Secondary constructor.
     *
     * @param packageName The name of the package to scan.
     * @param classLoader The [ClassLoader] to use.
     */
    constructor(
        packageName: String,
        classLoader: ClassLoader
    ) : this(
        packageName = packageName,
        configuration = ConfigurationBuilder(
        ).setUrls(
            ClasspathHelper.forClassLoader(classLoader)
        ).setScanners(
            SubTypesScanner(false),
            TypeAnnotationsScanner()
        ).addClassLoader(
            classLoader
        )
    )

    /**
     * Secondary constructor.
     *
     * @param packageName The name of the package to scan.
     * @param urls The [URL] array to use.
     */
    constructor(
        packageName: String,
        urls: Array<URL>
    ) : this(
        packageName = packageName,
        classLoader = URLClassLoader(urls)
    )

    /**
     * @return Classes to be used for diagram generation.
     */
    override fun list(): List<Class<out Any>> {
        val list = reflections.getSubTypesOf(
            Any::class.java
        ).asIterable().toList()
        if (list.isNullOrEmpty()) {
            throw InvalidPackageException(packageName)
        }
        return list
    }
}

After publishing my plugin to mavenLocal and use it in another project, I reach the following failure:

org.reflections.ReflectionsException: could not create Vfs.Dir from url, no matching UrlType was found [file:/Users/Romain/GitHub/eo-plantuml-builder/build/classes/java/main]
either use fromURL(final URL url, final List<UrlType> urlTypes) or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.

Any idea how to fix it?

RoRoche avatar May 07 '20 01:05 RoRoche

It seems that your url format are not correct, it can't match any of the default url types. Please see https://github.com/ronmamo/reflections/blob/9a1ac08d59311a91f3dad06f0c831eccdc47f1e3/src/main/java/org/reflections/vfs/Vfs.java#L207

dota17 avatar Jul 29 '20 08:07 dota17

I am also getting this warning, but in my case (which boils down to Reflections reflections = new Reflections(new ResourcesScanner());) it's because the classpath given by Java contains plenty of invalid file:/something... URLs in place of the valid file:///something... ones.

What is the best way to workaround this problem?

arkanovicz avatar Mar 05 '21 10:03 arkanovicz