Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:57:25+00:00 2026-06-16T10:57:25+00:00

I have a maven project which compiles with javac / aspectj compiler. I want

  • 0

I have a maven project which compiles with javac / aspectj compiler.
I want to run on classes which were compiled a javassist program which manipulate the compiled classes and add stuff to them.
I thought using the “process-classes” phase to run my tool.
My question is what is the best way to iterate with javassist over the classes files created in the “target/classes” so I can load, fix and save afterwards.
Another requirement is to run the tool on test classes as well.
If there is an open source project which does similar stuff it will be great to see a live example.
Thanks,
Avner

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:57:26+00:00Added an answer on June 16, 2026 at 10:57 am

    I stumbled upon the same problem recently and I wrote a small Maven Plugin to apply Javassist class transformations during build time. I’ve shared the code on https://github.com/drochetti/javassist-maven-plugin

    You’ve guessed right, you should use the process-classes phase and the tricky part is the classpath setup. After some trials and errors I’ve managed to guess the whole ClassPath problem among Project, Dependencies and Javassist (please, refer to com.github.drochetti.javassist.maven.JavassistMojo.execute() code if you want to check the solution).

    There’re some guidelines on GitHub link above, but basically you need to:

    1 – Configure the plugin on your pom.xml

    <plugin>
        <groupId>com.github.drochetti</groupId>
        <artifactId>javassist-maven-plugin</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <configuration>
            <includeTestClasses>false</includeTestClasses>
            <transformerClasses>
                <transformerClass>com.domain.ToStringTransformer</transformerClass>
            </transformerClasses>
        </configuration>
        <executions>
            <execution>
                <phase>process-classes</phase>
                <goals>
                    <goal>javassist</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    2 – Implement a ClassTransformer, here’s an example:

    /**
     * Silly transformer, used to hack the toString method.
     */
    public class ToStringTransformer extends ClassTransformer {
    
        /**
         * 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\"; }",
                    classToTransform);
            classToTransform.addMethod(hackedToStringMethod);
        }
    
    }
    

    Note: to implement a transformer you’ll need to add the plugin as a dependency of your project, but don’t worry as it’s only used during build time it can be provided scoped, this way it’ll not be a dependency of your final build.

    I hope that helps! Let me know if you need further help.

    Daniel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project in which I used maven fail-safe plugin to run the
I have a maven program, it compiles fine. When I run mvn test it
I have a Maven-based project, in which I trying to add some JAXB classes
I have a maven project which won't compile due to an unresolvable reference from
I have a Maven project which performs a number of time consuming tests as
Currently, I have a Maven project which inherits from a parent pom defining two
I have a maven project A which uses jar files of another project B
I have a maven Java project (Project A) which depends on a second project
For my Java project which uses Maven I have added Twitters Bootstrap lately to
I have a Jenkins build which builds a maven project with -PmyProfile clean package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.