Giter Site home page Giter Site logo

nikelin / javassist-maven-plugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from icon-systemhaus-gmbh/javassist-maven-plugin

0.0 2.0 0.0 283 KB

Maven plugin that will apply Javassist bytecode transformations during build time.

License: Apache License 2.0

javassist-maven-plugin's Introduction

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>com.github.drochetti</groupId>
		<artifactId>javassist-maven-plugin</artifactId>
		<version>1.0.0-SNAPSHOT</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
	protected boolean filter(CtClass candidateClass) throws Exception {
		CtClass myInterface = ClassPool.getDefault().get(MyInterface.class.getName());
		return !candidateClass.equals(myInterface) && candidateClass.subtypeOf(myInterface);
	}

	/**
	 * Hack the toString() method.
	 */
	@Override
	protected void applyTransformations(CtClass classToTransform) throws Exception {
		// 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);
	}

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

Known limitations

  • Not published on Maven Central or Sonatype OSS repos yet (Critical);
  • Don’t instrument classes inside .jar files, only classes on your project;
  • Lack of unit tests and sample app;
  • Further implementations of com.github.drochetti.javassist.maven.ClassTransformer can enable easier interactions with the Javassist API (provide some utilities).

How to contribute?

If you think this project is useful for you, them 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).

javassist-maven-plugin's People

Contributors

barthel avatar drochetti avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.