gpt4 book ai didi

Javassist 编译导致损坏的类文件

转载 作者:行者123 更新时间:2023-12-01 14:27:19 35 4
gpt4 key购买 nike

我正在尝试使用 javassist 在编译时插入一些代码片段

环境

  • Java 8
  • Spring 启动 2.2.6
  • hibernate 5.4.12.Final
  • Javasist
<dependency>
<groupId>nl.topicus.plugins</groupId>
<artifactId>javassist-maven-plugin-core</artifactId>
<version>2.0</version>
</dependency>

Pom 插件

            <plugin>
<groupId>nl.topicus.plugins</groupId>
<artifactId>javassist-maven-plugin</artifactId>
<version>2.0</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies>
<configuration>
<transformerClass>[PACKAGE].EACClassTransformer</transformerClass>
<packageName>c.w.c.e.entities</packageName>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>javassist</goal>
</goals>
</execution>
</executions>
</plugin>

变形金刚类

public class EACClassTransformer extends ClassTransformer {

@Override
public void applyTransformations(ClassPool classPool, CtClass classToTransform) throws TransformationException {
ACBCodeProcessor acb = new ACBCodeProcessor();
try {
if (classToTransform.getAnnotation(AccessControled.class) != null) {
getLogger().info("Processing class [" + classToTransform.getName() + "]");
acb.process(classToTransform);
}
} catch (ClassNotFoundException e) {

}
}

}

我遇到的编译错误是在 testCompile 步骤:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] [PATH]/EPAServiceTest.java:[17,44] cannot access [PACKAGE].EP
bad class file: [PATH]/EP.class
bad constant pool entry in RegularFileObject[[PATH]/EP.class]
expected CONSTANT_Utf8_info or CONSTANT_String_info at index 212
Please remove or make sure it appears in the correct subdirectory of the classpath.
[ERROR] [PATH]/EPAServiceTest.java:[6,1] cannot find symbol
symbol: static makeDTO
location: class
[INFO] 2 errors
[INFO] -------------------------------------------------------------

虽然错误提示无法访问文件,但是EP.class内容如下:


// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available

package corrupted_class_files;

清楚地显示损坏的文件

最佳答案

经过大量搜索,我发现插件使用的 javassist 版本是 3.18.0-GA 与 hibernate 不匹配>5.14.12.Final 使用。

所以我已经覆盖了插件 javassist 对通过编译步骤的版本 3.24.0-GA 的依赖。

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.0-GA</version>
</dependency>

关于Javassist 编译导致损坏的类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61817676/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com