0

I'm working with pre-existing classes, and I am not permitted to alter any of them. I am working within the same package as the classes, and am trying to implement new functionality. In one of the classes, there is a static void step(), which is called every time a significant action is completed. This method renders the view, and then calls Thread.sleep() for varying amounts of time. Part of the functionality I need to implement needs to be called every time a significant action is completed, before the view renders. This means that I need a way to execute code every time step is called, before anything else in step is allowed to execute.

I can't modify step, since it's in a class I can't touch, and I can't extend its enclosing class, since it's a static method, and so every call to it in those untouchable classes is a class-method call, rather than an object reference. That means that even if and when I override the method, it won't be called by any of the other classes. The way I see it, I have two options: Find a way to automatically insert a modified method into the class whenever it is loaded, or find a way to insert a separate method into the process whenever step is called. My research has been fruitless in both veins.

EDIT 1: As I've come to learn, there are extensions to complete these tasks. However, any solutions I use have to stay within the standard JDK.

Siris
  • 13
  • 4
  • Have you tried [How do I intercept a method invocation](https://stackoverflow.com/questions/576918/how-do-i-intercept-a-method-invocation-with-standard-java-features-no-aspectj-e)? – David Rawson May 23 '17 at 21:54
  • Hi, David. I took a look at that page, and there are lots of things there that look really great, but I have two qualms I forgot to mention in my question that, as best I can tell, rule out everything that I saw: 1) I can't use extensions. Everything I use has to stay in the JDK. 2) I need a clean solution - getting a little hacky is okay (i.e. reflection), but I'm not permitted to go to the level of things like exploiting the debugger. Is there anything I should go over again? (And I'll add those things to the question shortly). – Siris May 23 '17 at 23:28
  • are you not allowed to use AspectJ? – David Rawson May 23 '17 at 23:30
  • No, it's not part of the resources. As best I know, I only have the standard JDK to work with. – Siris May 23 '17 at 23:33
  • I'm not sure if it's a satisfying answer to you but your best option might be to have a look at how those suggested libraries accomplish method interception because, after all, these will eventually use JDK-only methods at some point. – David Rawson May 23 '17 at 23:37
  • 1
    Is there something that prevents you from decompiling the class, alter `step` method, recompile just that class (and inner classes if any), put the resulting `.class` file(s) in a jar, and put that jar at beginning of classpath? (If it's a class from JRE, there is still `-Xbootclasspath/p:hack.jar`) – Hugues M. May 24 '17 at 00:21

1 Answers1

1

Look at AspectJ static method weaving. It should allow you to modify your "un-modifiable" class byte code in order to add custom logic to "step()" methods before and after calls.

tsolakp
  • 5,858
  • 1
  • 22
  • 28