功能代码上的调试流跟踪会抛出IncompatibleClassChangeError

问题描述 投票:2回答:1

当我尝试通过IntelliJ中的Stream Trace调试下面代码中的流时,调试器无法评估foreach,因为会抛出下面的错误。我不知道它是什么,代码本身运行良好。

完全更新的IntelliJ社区版,JUnit 5,Spring Boot,Maven,Java 11。

仅在流跟踪调试期间发生的错误:

java.lang.IncompatibleClassChangeError:输入com.progonkpa.file.FileService $ GeneratedEvaluationClass $ 5不是com.progonkpa.file.FileService的嵌套成员:类型在不同的包中

包含流的代码:

public class FileService {

    public void createDirs(File parentDir, String[] fileNames) {
        Stream.of(fileNames)
                .map(fileName -> new File(parentDir, fileName))
                .forEach(file -> {
                    if (file.mkdirs())
                        System.out.println("Created file: " + file);
                    else
                        System.err.println("Failed to create file: " + file);
                });
    }
}

调用上述方法的测试:

public class FileServiceTest {

    private FileService fileService = new FileService();

    @Test
    public void generateDirs_createsList() {
        File tmpDir = new File("/tmp");
        String[] dirNamesList = {"dir1", "dir2"};
        File createdDir1 = new File(tmpDir, dirNamesList[0]);
        File createdDir2 = new File(tmpDir, dirNamesList[1]);

        fileService.createDirs(tmpDir, dirNamesList);

        assertTrue(createdDir1.exists());
        assertTrue(createdDir2.exists());
        assertTrue(createdDir1.delete());
        assertTrue(createdDir2.delete());
        assertTrue(tmpDir.delete());
    }
}

pom.hml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.unknown.somefunction</groupId>
    <artifactId>joske</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-test</artifactId>-->
            <!--<scope>test</scope>-->
        <!--</dependency>-->
        <!--Data processing-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>org.simplejavamail</groupId>
            <artifactId>simple-java-mail</artifactId>
            <version>5.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.simplejavamail</groupId>
            <artifactId>outlook-message-parser</artifactId>
            <version>1.1.17</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.vatbub</groupId>
            <artifactId>mslinks</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>
        <!--Testing-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Stream Trace in IntelliJ Community Edition

java intellij-idea junit stream
1个回答
4
投票

Stream调试器显然生成字节码并动态定义类以计算表达式。相关的源文件是

CompilingEvaluator.java CompilingEvaluatorImpl.java

并且YouTrack上有一个当前已打开的问题,但完全相同

Type some.Type$GeneratedEvaluationClass$1 is not a nest member of some.Type: types are in different packages

IDEA-204665

这仅适用于大于10的JDK版本,不幸的是你有

<java.version>11</java.version>

正如问题所暗示的,它发生的原因是

JDK 11具有“基于嵌套的访问控制”功能 (https://cr.openjdk.java.net/~dlsmith/nestmates.html

JEP 181

对其他工具的影响

对类文件进行操作或生成或处理字节码的任何工具都可能受到这些更改的影响。这些工具至少必须容忍新类文件属性的存在,并允许更改字节码规则。例如:

javap类文件检查工具,Pack200实现和ASM字节码操作框架,它也在JDK内部使用。

© www.soinside.com 2019 - 2024. All rights reserved.