Javassist 编译导致 Class 文件损坏
Javassist Compilation resulting in Corrupted Class Files
我正在尝试使用 javassist 在编译时插入一些代码片段
环境
- Java 8
- Spring 引导 2.2.6
- 休眠 5.4.12.Final
- Java支持
<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>
变形金刚Class
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 在编译时插入一些代码片段
环境
- Java 8
- Spring 引导 2.2.6
- 休眠 5.4.12.Final
- Java支持
<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>
变形金刚Class
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>