You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin数据类Jacoco插桩错误解决方法咨询

问题描述

我有一个Kotlin项目,构建阶段出现大量Jacoco相关错误堆栈,多数和数据类有关。比如定义如下数据类:

data class FooDTO(val a: String, val b: String)

会触发如下错误:

java.lang.instrument.IllegalClassFormatException: Error while instrumenting path/to/FooDtoMethodAccess with JaCoCo 0.8.10.202304240956/8ea9668.
    at org.jacoco.agent.rt.internal_4a7f17c.CoverageTransformer.transform(CoverageTransformer.java:94)
    at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
    at sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.esotericsoftware.reflectasm.AccessClassLoader.defineClass(AccessClassLoader.java:73)
    at com.esotericsoftware.reflectasm.AccessClassLoader.defineAccessClass(AccessClassLoader.java:57)
    at com.esotericsoftware.reflectasm.MethodAccess.get(MethodAccess.java:276)
    at com.coxautodev.graphql.tools.MethodFieldResolverDataFetcher.<init>(MethodFieldResolver.kt:131)
    at com.coxautodev.graphql.tools.MethodFieldResolver.createDataFetcher(MethodFieldResolver.kt:98)
    at com.coxautodev.graphql.tools.SchemaParser$createObject$$inlined$forEach$lambda$1.apply(SchemaParser.kt:126)
    at com.coxautodev.graphql.tools.SchemaParser$createObject$$inlined$forEach$lambda$1.apply(SchemaParser.kt:46)
    at graphql.schema.GraphQLObjectType$Builder.field(GraphQLObjectType.java:207)
    at com.coxautodev.graphql.tools.SchemaParser.createObject(SchemaParser.kt:124)
    at com.coxautodev.graphql.tools.SchemaParser.parseSchemaObjects(SchemaParser.kt:85)
    at com.coxautodev.graphql.tools.SchemaParser.makeExecutableSchema(SchemaParser.kt:109)
    at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration.graphQLSchema(GraphQLJavaToolsAutoConfiguration.java:65)
    at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$6059daa9.CGLIB$graphQLSchema$0(<generated>)
    at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$6059daa9$$FastClassBySpringCGLIB$$8a2c10eb.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
06/07/2023 13:10:02     at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$6059daa9.graphQLSchema(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at 

当前Jacoco Maven插件配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.10</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
解决方案

这个问题是Jacoco尝试instrument由ReflectASM动态生成的*MethodAccess类导致的,这类类是GraphQL Tools底层用来加速反射调用的,不属于项目业务代码,不需要被Jacoco覆盖。可以通过以下两种方式解决:

1. 排除动态生成的MethodAccess类

修改Jacoco插件配置,添加excludes规则,排除所有*MethodAccess类:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.10</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/*MethodAccess.class</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

2. 升级Jacoco版本到最新稳定版

Jacoco在后续版本(比如0.8.11及以上)中修复了部分动态类instrument的兼容性问题,升级插件版本可能直接解决问题:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.12</version> <!-- 或更高稳定版 -->
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

补充说明

如果排除*MethodAccess类后仍有其他动态生成类的问题,可以进一步排查堆栈中的类来源,比如CGLIB生成的代理类,也可以添加对应的排除规则,例如**/*$$EnhancerBySpringCGLIB*.class。

内容的提问来源于stack exchange,提问作者Nariman Esmaiely Fard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:52:49