1

I'm looking at a way to add some tracing diagnostics to some legacy Java code. By legacy in this sense I mean I cannot change any of the methods. If we consider the following objects:

public class ClassIOwnAndCanChange {

    public static void main(String[] args) {
        // store my object here that I want to make
        // available in LegacyClassA and B
        myMethod("foo", 100);
    }    

}

public class LegacyClassA {

    // method I cannot change
    public void myMethod(String name, int timer) {

        //want to get my 'injected' object here so that I can, 
        //for instance, time this method
        methodCallThatTakesSomeTime("some param");

    }  

}

public class LegacyClassB {

    // method I cannot change
    public void methodCallThatTakesSomeTime(String name) {
        // want to get my 'injected' object here for
        // diagnostic reasons

    }

}

I own A and I would like to make an object available in B and C. What is the right way to do this in Java without changing the method that A calls on B and the method that B calls on C?

2
  • You will have to improve your question to get answers. Maybe provide example using some names for instance methodOfA, methodOfB etc. Seems to me from the title a static member may do the job, but it is possible I misunderstand you Commented Jun 21, 2013 at 13:12
  • 2
    sort of global reference... Commented Jun 21, 2013 at 13:13

1 Answer 1

4

You can play all sorts of games with AOP (Aspect Oriented Programming) and/or other forms of bytecode manipulation.

Whether or not you should varies wildly.

It depends on your precise needs, and how you need to use the "injected" object. I'd consider AOP first since tools like AspectJ are mature. AOP and a ThreadLocal might be all you need.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer and also the ThreadLocal suggestion. I'll investigate those two in detail

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.