简体   繁体   中英

Agent to count objects inside methods using ByteBuddy

I'm looking for a way of count how many different objects are being called inside a method using ByteBuddy for byte code analysis. I tried this with string parsing, but that's absolutely impossible. Also, I've checked about AST, but I should build the code before doing that, what would be a lot of time. By this, it would be preferable if I could create an agent.

Given the following code:

@Test
public void myMethod(){
    Boolean myObj = false;
    assertTrue(myObj).isTrue();
    assertTrue(myObj2).isTrue();
}

The output for analysis for this method would be: (myObj, myObj2). As I'm new to JavaBuddy, my approach to this would be:

Create an element matcher that would find for methods annotated with @Test . Intercept it .... here's the problem: I don't know how should I count those objects or iterate through method statements.

Can anybody give me some links about it or samples?

In order to instrument code within a method, Byte Buddy allows you to use ASM to instrument the code instruction-wise. The reason for this is that ASM is a very good API for byte code processing already and there is no real good reason to replace it with something that would end up being rather similar.

If you want to learn about ASM, their webpage offers a great documentation.

Nevertheless, what you intend to is pretty complex to do. You basically have to follow the control flow of the method to see what object is assigned to what variable at any point in time. Also, some classes cannot be instrumented such as the Boolean class which is why I would recommend you to find another approach to your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM