3

I'm using a javaagent (with Javaassit) via the

premain(String agentArgs, Instrumentation inst)

method and I'm curios why a class is not considered by the ClassFileTransformer

Short description:

  • I have two classes in my project where the javaagent (premain) is applied to
  • MyMainClass is the class with the main method
  • MyLogicReference class is not references via import, ... in the MyMainClass
  • The ClassFileTransformer transform method is only called for MyMainClass but not for MyLogicReference
  • If I call java.lang.instrument.Instrumentation getAllLoadedClasses then I can see that the MyLogicReference class is loaded
  • ? Is this the way the agent works and if yes how can I for the agent to transform also the second (MyLogicReference) class ?

UPDATE

I think I've found some useful information in the Javadocs https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/ClassFileTransformer.html.

In java.lang.instrument.Instrumentation.retransformClasses I should be able to register the MyLogicReference class from the example. But I still wonder about the behaviour... Lets try it out...

for each transformer that was added with canRetransform true, the transform method is called in these transformers

Detailed:

I'm using the agent to change methods, ... via annotations (some sort of injection).

I've stripped down the classes to just the onces below and I wonder why only MyMainClass is put to the javaagent transformer (classfilebuffer) but not the MyLogicReference class.

public class MyMainClass {

  ... //Main method and call of myMethod();      

  @MyAnnotationToApplyLogic
  public void myMethod(){
    //Some code here
  }

The entry point for my process to change code is the annotation where I refer to another class (MyLogicReference) ...

@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.CLASS)
@Functional(value = MyLogicReference.class, type = ElementType.PARAMETER)
public @interface MyAnnotationToApplyLogic {

}

public class MyLogicReference {
  // @MyAnnotationToApplyLogic  in the MyMainClass method references to this class
  // The Javaagent Class file transformer adjust the MyMainClass.myMethod code based on the annotation
  public void mySecondMethod(){
  }
}

If I use the

java.lang.instrument.Instrumentation getAllLoadedClasses method

I can see the MyLogicReference class. But the ClassFileTransformer is never called for this class. Is this correct for javaagents ?

If I, for example import the MyLogiReference.class in the MyMainClass, I figured out that the transformer is called.

So my current assumption is that only classes/subclasses which are directly referenced over the main class are send to the ClassFileTransformer. If this is correct than how can I force the javaagent to transform a class which hasn't been transformed before ?

My javaaagent manifest entries (MVN):

<Premain-Class>com.MyTestAgent</Premain-Class>
<Agent-Class>com.MyTestAgent</Agent-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>

I stripped down the ClassFileTransformer to this and the second class is still not loaded:

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
  if (!className.startsWith("java/") && !className.startsWith("javax/") && !className.startsWith("sun/")) {
    log("NOW PROCESSING: " + className);
    return classfileBuffer;
  }
  return null;
}

//Output:
//NOW PROCESSING: MyMainClass

1 Answer 1

0

Based on what you said I'm guessing you are referring to MyLogicReference somewhere from your javaagent code. As a consequence JVM loads the class before the instrumentation has started. Two things to remember:

  1. Java agent is just a java program, and it requires classes to be loaded.
  2. Java agent transform classes when they are loaded. For any class loaded before java agent started you will need to use java.lang.instrument.Instrumentation#retransformClasses (as you noticed yourself).

Using java.lang.instrument.Instrumentation#retransformClasses doesn't seem necessary here too. In my opinion it is always better to avoid it (for the sake of simplicity). You can either:

  • separate the code which is used by javaagent, and the code that's being instrumented.
  • only refer to MyLogicReference as a string, eg. use foo.getClass().getName().equals("MyLogicReference") instead of foo instanceof MyLogicReference

Based on information provided, I'm also wondering if you considered using Annotation Processor instead (https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html).

1
  • Thanks for the answer. I've already had a look at annotation processors (not in detail). When it comes to modify code and dont build proxy classes I prefer the agent before the anno. processor with hacks (like loombok). Also the speration of the agent is for my usecases quit handy (e.g remote JMX, ...). On the other hand for annotation driven actions the procesor is nice (with compile messages, ...). For me as a newbie in these topics the annotation processor with a transform method would be great :D
    – Greasy Fox
    Jul 2, 2016 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.