• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

icon-Systemhaus-GmbH/javassist-maven-plugin: Maven plugin that will apply Javass ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

icon-Systemhaus-GmbH/javassist-maven-plugin

开源软件地址(OpenSource Url):

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

开源编程语言(OpenSource Language):

Java 100.0%

开源软件介绍(OpenSource 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>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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
gkdevops/maven-sample-code发布时间:2022-08-17
下一篇:
play2-maven-plugin/play2-maven-test-projects: Play! Framework 2.x Maven Plugin - ...发布时间:2022-08-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap