在 Java 11、Java 17 上运行使用 extcos 和 asm 构建的代码时出现异常

发布于 2025-01-12 03:46:48 字数 5187 浏览 0 评论 0原文

我在Intellij IDEA中创建了一个Maven项目来说明这个问题;该代码试图使用 extCos 扫描器类来查找实现接口的所有类。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>TestComponentScanner</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.sf.extcos</groupId>
            <artifactId>extcos</artifactId>
            <version>0.4b</version>
            <exclusions>
                <exclusion>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>9.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>
</project>

ExampleComponentScanner

package com.example;

import java.util.HashSet;
import java.util.Set;
import net.sf.extcos.ComponentQuery;
import net.sf.extcos.ComponentScanner;

public class ExampleComponentScanner {
    public Set<Class<?>> getImplementingClasses(){
    Set<Class<?>> classes = new HashSet<Class<?>>();

    ComponentScanner scanner = new ComponentScanner();
        Set<Class<?>> finalClasses = classes;
        classes = scanner.getClasses(new ComponentQuery() {
        @Override
        protected void query() {
            select().
                    from("com.example").
                    returning(allImplementing(ExampleInterface.class));
        }
    });
    return classes;
    }

    public static void main(String[] args){
        ExampleComponentScanner exConScan = new ExampleComponentScanner();
        Set<Class<?>> setClasses = exConScan.getImplementingClasses();
        for (Class exIntCl:setClasses) {
            System.out.println(exIntCl.getName());
            System.out.println(", ");
        }
    }
}

ExampleInterface

package com.example;

public interface ExampleInterface {
    public default String getInterfaceName(){
        return null;
    }
}

实现类

package com.example;

public class ExampleImplementer001 implements ExampleInterface{
}

使用 JDK 1.8 构建时,该项目按预期运行。它也可以使用 JDK 11 和 JDK 17 成功构建,但是当执行使用较新的 Java 版本(11 和 17)构建的 ExampleComponentScanner 类时,它会抛出以下异常:

C:\JDK\BellSoft\LibericaJDK-17-Full\bin\java.exe "-javaagent:C:\IntelliJ IDEA Community Edition 2020.3.3\lib\idea_rt.jar=64764:C:\IntelliJ IDEA Community Edition 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\absoluteBeginner\IdeaProjects\TestComponentScanner\target\classes;C:\Users\absoluteBeginner\.m2\repository\net\sf\extcos\extcos\0.4b\extcos-0.4b.jar;C:\Users\absoluteBeginner\.m2\repository\org\ow2\asm\asm\5.0\asm-5.0.jar;C:\Users\absoluteBeginner\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\absoluteBeginner\.m2\repository\org\slf4j\slf4j-jdk14\1.7.36\slf4j-jdk14-1.7.36.jar com.example.ExampleComponentScanner
Exception in thread "eXtcos managed thread 1" java.lang.IllegalArgumentException
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at net.sf.extcos.internal.JavaResourceAccessor.readClassData(JavaResourceAccessor.java:362)
    at net.sf.extcos.internal.JavaResourceAccessor.setResourceUrl(JavaResourceAccessor.java:333)
    at net.sf.extcos.internal.URLResource.getResourceAccessor(URLResource.java:93)
    at net.sf.extcos.internal.URLResource.isClass(URLResource.java:126)
    at net.sf.extcos.internal.RootFilter.filter(RootFilter.java:22)
    at net.sf.extcos.internal.AbstractChainedFilter.filter(AbstractChainedFilter.java:89)
    at net.sf.extcos.internal.ThreadingFilterInterceptor$1.run(ThreadingFilterInterceptor.java:48)
    at java.base/java.lang.Thread.run(Thread.java:833)

我正在使用 Bellsoft 的 Liberica JDK,因为我还需要集成一些JavaFX代码。

I have created a Maven project in Intellij IDEA to illustrate the issue; the code seeks to use the extCos scanner classes to find all classes implementing an interface.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>TestComponentScanner</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.sf.extcos</groupId>
            <artifactId>extcos</artifactId>
            <version>0.4b</version>
            <exclusions>
                <exclusion>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>9.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>
</project>

The ExampleComponentScanner class

package com.example;

import java.util.HashSet;
import java.util.Set;
import net.sf.extcos.ComponentQuery;
import net.sf.extcos.ComponentScanner;

public class ExampleComponentScanner {
    public Set<Class<?>> getImplementingClasses(){
    Set<Class<?>> classes = new HashSet<Class<?>>();

    ComponentScanner scanner = new ComponentScanner();
        Set<Class<?>> finalClasses = classes;
        classes = scanner.getClasses(new ComponentQuery() {
        @Override
        protected void query() {
            select().
                    from("com.example").
                    returning(allImplementing(ExampleInterface.class));
        }
    });
    return classes;
    }

    public static void main(String[] args){
        ExampleComponentScanner exConScan = new ExampleComponentScanner();
        Set<Class<?>> setClasses = exConScan.getImplementingClasses();
        for (Class exIntCl:setClasses) {
            System.out.println(exIntCl.getName());
            System.out.println(", ");
        }
    }
}

ExampleInterface

package com.example;

public interface ExampleInterface {
    public default String getInterfaceName(){
        return null;
    }
}

The implementing class

package com.example;

public class ExampleImplementer001 implements ExampleInterface{
}

This project runs as expected when built using JDK 1.8. It builds successfully with JDK 11 and JDK 17 as well, but when the ExampleComponentScanner class built with either of the newer Java versions (11 and 17) is executed, it throws the following exception:

C:\JDK\BellSoft\LibericaJDK-17-Full\bin\java.exe "-javaagent:C:\IntelliJ IDEA Community Edition 2020.3.3\lib\idea_rt.jar=64764:C:\IntelliJ IDEA Community Edition 2020.3.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\absoluteBeginner\IdeaProjects\TestComponentScanner\target\classes;C:\Users\absoluteBeginner\.m2\repository\net\sf\extcos\extcos\0.4b\extcos-0.4b.jar;C:\Users\absoluteBeginner\.m2\repository\org\ow2\asm\asm\5.0\asm-5.0.jar;C:\Users\absoluteBeginner\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\absoluteBeginner\.m2\repository\org\slf4j\slf4j-jdk14\1.7.36\slf4j-jdk14-1.7.36.jar com.example.ExampleComponentScanner
Exception in thread "eXtcos managed thread 1" java.lang.IllegalArgumentException
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at net.sf.extcos.internal.JavaResourceAccessor.readClassData(JavaResourceAccessor.java:362)
    at net.sf.extcos.internal.JavaResourceAccessor.setResourceUrl(JavaResourceAccessor.java:333)
    at net.sf.extcos.internal.URLResource.getResourceAccessor(URLResource.java:93)
    at net.sf.extcos.internal.URLResource.isClass(URLResource.java:126)
    at net.sf.extcos.internal.RootFilter.filter(RootFilter.java:22)
    at net.sf.extcos.internal.AbstractChainedFilter.filter(AbstractChainedFilter.java:89)
    at net.sf.extcos.internal.ThreadingFilterInterceptor$1.run(ThreadingFilterInterceptor.java:48)
    at java.base/java.lang.Thread.run(Thread.java:833)

I am using Bellsoft's Liberica JDK since I also need to integrate some JavaFX code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文