简体   繁体   中英

Javassist Compilation resulting in Corrupted Class Files

I am trying to insert some code snippet at compile time using javassist

Environment

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

Pom Plugin

            <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

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:

[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:


  // 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.

So I have overridden the plugin javassist dependency to version 3.24.0-GA which passed the compilation step.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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