简体   繁体   English

Javassist 编译导致 Class 文件损坏

[英]Javassist Compilation resulting in Corrupted Class Files

I am trying to insert some code snippet at compile time using javassist我正在尝试使用 javassist 在编译时插入一些代码片段

Environment环境

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

Pom Plugin 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>

Transformer Class变压器 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) {

        }
    }

}

The Compilation Error I am getting is at the testCompile step:我得到的编译错误是在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] -------------------------------------------------------------

While the error shows that the file cannot be accessed the EP.class content is as follows:虽然错误显示无法访问该文件,但EP.class内容如下:


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

package corrupted_class_files;

Clearly showing a corrupted file清楚地显示损坏的文件

After a lot of search, I found that the version of javassist the plugin was using is 3.18.0-GA which doesn't match that which hibernate 5.14.12.Final use.经过大量搜索,我发现插件使用的javassist版本是3.18.0-GA ,与 hibernate 5.14.12.Final使用的版本不匹配。

So I have overridden the plugin javassist dependency to version 3.24.0-GA which passed the compilation step.因此,我已将插件javassist依赖项覆盖到通过编译步骤的3.24.0-GA版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM