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

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

问题描述

我在 Reflections 库中遇到问题。
我试图动态加载实现特定接口的所有类。
只要我不在这些类中使用lambda表达式(java 8),一切正常(加载所有类)。
我试过升级lib版本,但效果是一样的(java.io.IOException:无效的常量类型: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>

不排除具有相同的效果。

without exclusion is the same effect.

代码:

    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?

PS抱歉英语:)

推荐答案

如果你看一下这个表,你会看到常量类型: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.

反映错误跟踪器中的一个项目描述您的问题。在底部,您会看到通知:

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天全站免登陆