简体   繁体   中英

Change Java default classes (no override)

Is there a proper way to change a Java default method or class? I'm using a library that communicate via TCP socket and I want to catch the system socket call and change it with a custom one (without changing the library code).

没有非常darc的JVM魔术,这是不可能的。

No. It is not possible. Classes required for the execution are loaded when JVM initialized so it is not possible to change the default classes or method until OOPS paradigm used.

Not simple, but possible!
You need to overload the JVM default classLoader so any time new class will be initialized (eg like System.out for static or new MyObject for nonstatic classes).
Any time you load new class the JVM will first check if you would like to initialize the class in a different way.

It not that simple to understand how to do it so I attached good guides on the subject:
1. Sun's official guide.
2. JavaWorld - The basics of Java class loaders
3. JournalDev Code example
4. Jenkov's great guide on the subject from reflection point of view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM