Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watch/monitor/trace 等判断重载函数/同名函数 #434

Closed
hengyunabc opened this issue Jan 7, 2019 · 0 comments
Labels

Comments

@hengyunabc
Copy link
Collaborator

@hengyunabc hengyunabc commented Jan 7, 2019

比如下面的一个Demo:

public class Test {
	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 1000; ++i) {
			hello("world");
			hello(i, "world");
			Thread.sleep(1000);
		}
	}

	public static void hello(String string) {
		System.out.println(string);
	}

	public static void hello(int i, String string) {
		System.out.println(string + " " + i);
	}
}

Test类有两个 hello函数,它们的参数不一样,如果直接watch Test hello params,则会匹配到两个hello函数。

那么怎么准确watch第二个hello函数呢?

下面给出两种方式,ognl表达式是很灵活的,大家可以多尝试下。

第一种方式,判断params的length:

$ watch Test hello params params.length==2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 21 ms.
ts=2019-01-07 19:50:17; [cost=0.103617ms] result=@Object[][
    @Integer[109],
    @String[world],
]
ts=2019-01-07 19:50:18; [cost=0.185045ms] result=@Object[][
    @Integer[110],
    @String[world],
]

第二种方式,判断params的类型(注意,这里因为int会被包装为Object,所以params[0]的类型是java.lang.Integer):

$ watch Test hello params 'params[0].class.name=="java.lang.Integer"'
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 25 ms.
ts=2019-01-07 19:54:59; [cost=0.358485ms] result=@Object[][
    @Integer[390],
    @String[world],
]
ts=2019-01-07 19:55:00; [cost=0.211562ms] result=@Object[][
    @Integer[391],
    @String[world],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.