反射 - Java 8 - 无效的常量类型 [英] Reflections - Java 8 - invalid constant type

查看:18
本文介绍了反射 - Java 8 - 无效的常量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Reflections 库时遇到问题.我正在尝试动态加载实现特定接口的所有类.只要我不在这些类中使用 lambda 表达式(java 8),一切都正常(所有类都已加载).我尝试升级 lib 版本但效果是一样的(java.io.IOException: invalid constant type: 18).

I have a problem with Reflections library. I am trying to load dynamically all classes which implement specific interface. Everything works fine (all classes are loaded) as long as I do not use lambda expression in these classes (java 8). I tried upgrade lib version but effect was the same (java.io.IOException: invalid constant type: 18).

依赖并在 pom.xml 中构建

Dependency and build in pom.xml

      <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.10</version>
        <exclusions>
            <exclusion>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.19.0-GA</version>
    </dependency>
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    </build>

不排除是一样的效果.

代码:

    URL jarUrl = jarFile.toURI().toURL();
    URLClassLoader child = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader());
    ConfigurationBuilder builder = new ConfigurationBuilder()
            .addClassLoader(child)
            .addUrls(jarUrl)
            .setScanners(new SubTypesScanner());
    Reflections r = new Reflections(builder);
    return r.getSubTypesOf(cls);

如何使用 lambda 表达式加载类?

How can I load classes with lambda expression?

P.S 对不起英语:)

P.S Sorry for English :)

推荐答案

如果您查看 这个表,你会看到常量类型:18"指的是CONSTANT_InvokeDynamic 属性,其标签值为 18.

If you look at this table, you’ll see that "constant type: 18" refers to the CONSTANT_InvokeDynamic attribute whose tag value is 18.

因此,您使用的库具有与 Java 8 不兼容的类解析器.实际上,这个类解析器甚至与 Java 7 不兼容,因为这个常量值是从 Java 7 开始指定的.它只是侥幸逃脱了,因为普通的 Java 代码在 Java 7 中不使用此功能.但是当与不同的代码生成的代码进行交互时JVM 的编程语言,它甚至可能在 Java 7 中失败.

So you are using a library which has a class parser which is not Java 8 compatible. Actually, this class parser even isn’t Java 7 compatible as this constant value is specified since Java 7. It just got away with that as ordinary Java code doesn’t use this feature in Java 7. But when interacting with code produced by different programming languages for the JVM, it could even fail with Java 7.

Reflections 的错误跟踪器中的一项描述了您的问题.在底部,您会看到通知:

There’s an item in the bug tracker of Reflections describing your problem. At the bottom, you will find the notice:

有了这个修复:https://issues.jboss.org/browse/JASSIST-174 javassist得到了支持这个常数.因此,在 3.18.2-GA 中不会发生此错误.

With this fix: https://issues.jboss.org/browse/JASSIST-174 javassist got support for this constant. So with 3.18.2-GA this error doesn't occur.

这篇关于反射 - Java 8 - 无效的常量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆