Question

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!

Was it helpful?

Solution

Findbugs is capable of detecting this.

OTHER TIPS

How many instances are we talking about here? Tools like the Eclipse debugger allow for data watchpoints (eg: object modified). That will cause the debugger to stop immediately on the exact line of code that is modifying the object in question. The only trick is you need to put a breakpoint in at object creation so that you can identify the object you want to watch.

You can use a Java Decompiler turn all the byte code into source, then you should be able to come up with a regex to match the code in question (or use FindBugs?).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top