From 9cf0ceef7ec818369d9e854994bdb825d52498d6 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Fri, 7 Dec 2018 15:54:32 +0800 Subject: [PATCH] update redefine doc --- site/src/site/sphinx/en/redefine.md | 33 ++++++++++++++++++++++++++++- site/src/site/sphinx/redefine.md | 33 ++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/site/src/site/sphinx/en/redefine.md b/site/src/site/sphinx/en/redefine.md index 146b8cb1a..7d2ca400a 100644 --- a/site/src/site/sphinx/en/redefine.md +++ b/site/src/site/sphinx/en/redefine.md @@ -17,7 +17,38 @@ Reference: [Instrumentation#redefineClasses](https://docs.oracle.com/javase/8/do ### Usage -``` +```bash redefine -p /tmp/Test.class redefine -c 327a647b -p /tmp/Test.class /tmp/Test$Inner.class ``` + +### Restrictions of the redefine command + +* New field/method is not allowed +* The function that is running, no exit can not take effect, such as the new `System.out.println` added below, only the `run()` function will take effect. + + ```java + public class MathGame { + public static void main(String[] args) throws InterruptedException { + MathGame game = new MathGame(); + while (true) { + game.run(); + TimeUnit.SECONDS.sleep(1); + // This doesn't work because the code keeps running in while + System.out.println("in loop"); + } + } + + public void run() throws InterruptedException { + // This works because the run() function ends completely every time + System.out.println("call run()"); + try { + int number = random.nextInt(); + List primeFactors = primeFactors(number); + print(number, primeFactors); + + } catch (Exception e) { + System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage()); + } + } +``` \ No newline at end of file diff --git a/site/src/site/sphinx/redefine.md b/site/src/site/sphinx/redefine.md index 1e4f8e0fc..bcae9252a 100644 --- a/site/src/site/sphinx/redefine.md +++ b/site/src/site/sphinx/redefine.md @@ -18,7 +18,38 @@ redefine ### 使用参考 -``` +```bash redefine -p /tmp/Test.class redefine -c 327a647b -p /tmp/Test.class /tmp/Test\$Inner.class +``` + +### redefine的限制 + +* 不允许新增加field/method +* 正在跑的函数,没有退出不能生效,比如下面新增加的`System.out.println`,只有`run()`函数里的会生效 + + ```java + public class MathGame { + public static void main(String[] args) throws InterruptedException { + MathGame game = new MathGame(); + while (true) { + game.run(); + TimeUnit.SECONDS.sleep(1); + // 这个不生效,因为代码一直跑在 while里 + System.out.println("in loop"); + } + } + + public void run() throws InterruptedException { + // 这个生效,因为run()函数每次都可以完整结束 + System.out.println("call run()"); + try { + int number = random.nextInt(); + List primeFactors = primeFactors(number); + print(number, primeFactors); + + } catch (Exception e) { + System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage()); + } + } ``` \ No newline at end of file