Javassist Maven Plugin

A maven plugin based on Javassist to manipulate bytecode of project classes at post-compile time.

License

License

Categories

Categories

Maven Build Tools Javassist Application Layer Libs Bytecode Manipulation
GroupId

GroupId

de.icongmbh.oss.maven.plugins
ArtifactId

ArtifactId

javassist-maven-plugin
Last Version

Last Version

2.0.2
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

Javassist Maven Plugin
A maven plugin based on Javassist to manipulate bytecode of project classes at post-compile time.
Project URL

Project URL

https://github.com/icon-Systemhaus-GmbH/javassist-maven-plugin
Source Code Management

Source Code Management

https://github.com/icon-Systemhaus-GmbH/javassist-maven-plugin.git

Download javassist-maven-plugin

How to add to project

<plugin>
    <groupId>de.icongmbh.oss.maven.plugins</groupId>
    <artifactId>javassist-maven-plugin</artifactId>
    <version>2.0.2</version>
</plugin>

Dependencies

compile (6)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 3.6.1
commons-io : commons-io jar 2.6
org.slf4j : slf4j-api jar 2.0.0-alpha1
org.slf4j : slf4j-simple jar 2.0.0-alpha1
org.javassist : javassist jar 3.26.0-GA
com.github.stephanenicolas.javassist : javassist-build-plugin-api jar 1.0.0

provided (6)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.6.0
org.apache.maven : maven-core jar 3.6.1
org.apache.maven : maven-model-builder jar 3.6.1
org.codehaus.plexus : plexus-utils jar 3.3.0
org.codehaus.plexus : plexus-archiver jar 4.2.0
org.codehaus.plexus : plexus-classworlds jar 2.6.0

test (6)

Group / Artifact Type Version
junit : junit jar 4.13-beta-3
org.easymock : easymock jar 4.0.2
org.apache.maven.plugin-testing : maven-plugin-testing-harness jar 3.3.0
org.apache.maven : maven-compat jar 3.6.1
org.codehaus.plexus : plexus-interpolation jar 1.26
org.apache.maven : maven-artifact jar 3.6.1

Project Modules

There are no modules declared in this project.

Javassist Maven Plugin

A simple Maven plugin that can apply Javassist tranformation on classes after compilation.

How to use it

Include the plugin on your pom.xml descriptor:


    <plugin>
        <groupId>de.icongmbh.oss.maven.plugins</groupId>
        <artifactId>javassist-maven-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <includeTestClasses>false</includeTestClasses>
            <transformerClasses>
                <transformerClass>
                    <className>com.domain.ToStringTransformer</className>
                    <properties>
                        <property>
                            <name>append.value</name>
                            <value> and ToStringTransformer</value>
                        </property>
                    </properties>
                </transformerClass>
            </transformerClasses>
        </configuration>
        <executions>
            <execution>
                <phase>process-classes</phase>
                <goals>
                    <goal>javassist</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

You must implement Class transformers, here’s one simple example (at least, not a “logger” one).


/**
 * Silly transformer, used to hack the toString method.
 */
public class ToStringTransformer extends ClassTransformer {

  private static final String APPEND_VALUE_PROPERTY_KEY = "append.value";

  private String appendValue;

  /**
   * We'll only transform subtypes of MyInterface.
   */
  @Override public boolean shouldTransform(final CtClass candidateClass)
          throws JavassistBuildException {
    CtClass myInterface = ClassPool.getDefault().get(MyInterface.class.getName());
    try {
      return !candidateClass.equals(myInterface) && candidateClass.subtypeOf(myInterface);
    } catch (NotFoundException e) {
      throw new JavassistBuildException(e);
    }
  }

  /**
   * Hack the toString() method.
   */
  @Override public void applyTransformations(CtClass classToTransform)
          throws JavassistBuildException {
    try {
      // Actually you must test if it exists, but it's just an example...
      CtMethod toStringMethod = classToTransform.getDeclaredMethod("toString");
      classToTransform.removeMethod(toStringMethod);

      CtMethod hackedToStringMethod = CtNewMethod
              .make("public String toString() { return \"toString() hacked by Javassist" + (
                            this.appendValue != null ? this.appendValue : "") + "\"; }",
                    classToTransform);
      classToTransform.addMethod(hackedToStringMethod);
    } catch (CannotCompileException | NotFoundException e) {
      throw new JavassistBuildException(e);
    }
  }

  @Override public void configure(final Properties properties) {
    if (null == properties) {
      return;
    }
    this.appendValue = properties.getProperty(APPEND_VALUE_PROPERTY_KEY);
  }
}

Known limitations

  • Don’t instrument classes inside .jar files, only classes on your project;
  • Lack of unit tests and sample app;
  • Further implementations of de.icongmbh.oss.maven.plugin.javassist.ClassTransformer can enable easier interactions with the Javassist API (provide some utilities).

How to contribute?

If you think this project is useful for you, then there’s a huge chance it’s useful to others, so please feel free
to fork, fix it, improve it and test it (the “Known limitations” above is a great way to start).

Documentation

de.icongmbh.oss.maven.plugins

icon Systemhaus GmbH

Versions

Version
2.0.2
2.0.0
1.1.0