开发者

How to hijack the String.class

So basically I just want to debug a couple of simple scripts by trying a new method, I have been successful in the past by hooking the URL.class (using the source from the jdk and recompling), however I realized that how can I print out a string, before it is even a string?

I want to be able to write to file, or print out, any string that goes through the string class, but how can I create a pipe method when I am within the string class itself?

I.e. a char array is being passed int开发者_如何转开发o the string, how do I grab that, make it a string and send it to the print out command from within the string class? If I am too vague I will provide some examples.


IMO, it is a really bad idea to tweak the implementations of Java standard classes, even if you are only doing it to try and debug something. The problem is that you can break things really badly.

For instance, @Roflcoptr suggests that you add a System.out.println(...) to each String constructor. But what would happen if the PrintStream.println call tried to construct a String for some reason? And what would happen during JVM bootstrapping if something tried to construct a String ... before the System.* stream objects were initialized?

I'm not saying it won't work. I'm not saying you can't make it work. I'm am saying that hackery like this is liable to fail in spectacular and horrible ways.

My advice would be to find another way to debug / diagnose your problem. For example, set a breakpoint on the relevant String constructor.


An alternative to recompiling from the JDK source might be to use aspect-oriented programming, which allows you to add or replace behavior to previously compiled classes. AspectJ is one tool supporting aspect-oriented programming for Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜