Alexander Belenov
Alexander Belenov

Reputation: 412

Why aren't my Javassist classes executed at runtime?

I have been writting a java agent which modifies bytecode of loaded classes. My code consists of 4 classes:

  1. StackTraceAgent.java - the class of java agent with method premain.
  2. StackTraceClassTransformer.java - implementation of java.lang.instrument.ClassFileTransformer.
  3. Student.java - just custom class for testing.
  4. AgentTest - simple class with a method main.

Utilized libraries:

  1. javassist-3.20.0-GA.jar

StackTraceAgent.java, ClassFileTransformer.java and javassist-3.20.0-GA.jar are packed in a jar-file. And it is invoked via "-javaagent:MyJar.jar AgentTest". My issue is that code has been written using javassist classes is not invoked at runtume. It is just passed up by a runtime without any error, and at the end program continue to execute non-javassist code till the end.

How I have tried to solve the issue:

  1. I have reviewed compiled classes. All classes contain exact code.
  2. I sense that it is something wrong with packaging into jar, but after hours of Internet surfing I did not find any interesting stuff.

MANIFEST.MF:

Manifest-Version: 1.0
Premain-Class: agent.StackTraceAgent

My pom.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.belenov</groupId>
    <artifactId>TraceAgent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestFile>
                            src/main/resources/META-INF/MANIFEST.MF
                        </manifestFile>
                        <manifest>
                            <addClasspath>false</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

StackTraceAgent.java:

import java.lang.instrument.Instrumentation;
public class StackTraceAgent  {

    public static void premain(String args, Instrumentation instrumentation) {
        System.out.println("instrument agent");
        StackTraceClassTransformer transformer = new StackTraceClassTransformer();
        instrumentation.addTransformer(transformer);
    }
}

StackTraceClassTransformer.java:

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

public class StackTraceClassTransformer implements ClassFileTransformer {

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
                            ProtectionDomain protectionDomain,
                            byte[] classfileBuffer) throws IllegalClassFormatException {

        try {
            //the code below is passed up
            ClassPool classPool = ClassPool.getDefault();
            CtClass ctClass = classPool.get(className);
            CtMethod[] methods = ctClass.getMethods();

            for (CtMethod method : methods) {
                method.insertAfter("System.out.println(\"Method name: \"+" + method.getName() + "+\" \"+" + method.getSignature() + ");");
                method.insertAfter("System.out.println(\"Object: \" + this.toString());");
            }

            byte[] byteCode = ctClass.toBytecode();
            ctClass.detach();
            return byteCode;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

AgentTest.java

public class AgentTest {
    public static void main(String[] args) {
        System.out.println("Agent Tester");
        Student student = new Student("vasia", "poopkin");
        student.toString();
    }
}

Student.java

public class Student {
    private String name;
    private String surname;

    public Student(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", surname='" + surname + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
}

Upvotes: 1

Views: 1224

Answers (1)

Adonis
Adonis

Reputation: 4818

Basically in your manifest file, you need to include the classpath of every Library and resources your program used, you can add a line like this in the MANIFEST.MF:

Class-Path: path/to/my/libraries

One way to do it would simply to update your pom.xml, the manifest section of the maven jar plugin with:

<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.directory}/classes/lib</classpathPrefix>

Upvotes: 2

Related Questions