Search code examples
javacoding-stylejvmbytecodejava-bytecode-asm

grep for specific jvm bytecode pattern


I am working on a legacy java project which has a number of design issues. As a result, some parts of the code don't behave as expected. Consider the following piece of code:

public enum Parent{
    PARENT1(CHILD1, CHILD2), PARENT2(CHILD3, CHILD1), 
    PARENT3(CHILD4, CHILD2)

    private Child [] children;
    Parent(Child ...children) { this.children = children; } 

    public Child [] getChildren() { return this.children; }
}

public enum Child{ CHILD1, CHILD2, CHILD3, CHILD4  }

Now from the above code, you can clearly see that getChildren() returns reference to the array. Hence, it is possible to change the elements of the array like this:

   Child [] childrenOfParent3 = Parent3.getChildren();
   childrenOfParent3[0] = null;  //badcode

Some piece of code is doing something like the above mentioned and, I am having a hard time trying to locate this(grep does not help).

Is there any tool out there which can help me locate this pattern(badcode) using jvm bytecode analysis or how would you do it?

NOTE: Due to some constraints, I cannot change Parent implementation.

Thanks!


Solution

  • Findbugs is capable of detecting this.