Skip to content

Commit cd650f9

Browse files
committed
Use return type value when 'type' matcher is used.
Fixes btraceio#517
1 parent f20e15a commit cd650f9

4 files changed

Lines changed: 119 additions & 3 deletions

File tree

btrace-instr/src/main/java/org/openjdk/btrace/instr/Instrumentor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,9 +1807,12 @@ private boolean typeMatches(String decl, String desc, boolean exactTypeMatch) {
18071807
return true;
18081808
} else {
18091809
String d = TypeUtils.declarationToDescriptor(decl);
1810-
Type[] args1 = Type.getArgumentTypes(d);
1811-
Type[] args2 = Type.getArgumentTypes(desc);
1812-
return InstrumentUtils.isAssignable(args1, args2, cl, exactTypeMatch);
1810+
Type[] argTypesLeft = Type.getArgumentTypes(d);
1811+
Type[] argTypesRight = Type.getArgumentTypes(desc);
1812+
Type retTypeLeft = Type.getReturnType(d);
1813+
Type retTypeRight = Type.getReturnType(desc);
1814+
return InstrumentUtils.isAssignable(retTypeLeft, retTypeRight, cl, exactTypeMatch)
1815+
&& InstrumentUtils.isAssignable(argTypesLeft, argTypesRight, cl, exactTypeMatch);
18131816
}
18141817
}
18151818

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package traces.onmethod;
27+
28+
import static org.openjdk.btrace.core.BTraceUtils.*;
29+
30+
import org.openjdk.btrace.core.annotations.BTrace;
31+
import org.openjdk.btrace.core.annotations.Kind;
32+
import org.openjdk.btrace.core.annotations.Location;
33+
import org.openjdk.btrace.core.annotations.OnMethod;
34+
import org.openjdk.btrace.core.annotations.Return;
35+
import org.openjdk.btrace.core.annotations.Self;
36+
37+
/** @author Jaroslav Bachorik */
38+
@BTrace
39+
public class ArgsReturnTypeMatch {
40+
@OnMethod(
41+
clazz = "/.*\\.OnMethodTest/",
42+
method = "args",
43+
type = "long (java.lang.String, long, java.lang.String[], int[])",
44+
location = @Location(value = Kind.RETURN))
45+
public static void args(
46+
@Self Object self, @Return long retVal, String a, long b, String[] c, int[] d) {
47+
println("args");
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package traces.onmethod;
27+
28+
import static org.openjdk.btrace.core.BTraceUtils.*;
29+
30+
import org.openjdk.btrace.core.annotations.BTrace;
31+
import org.openjdk.btrace.core.annotations.Kind;
32+
import org.openjdk.btrace.core.annotations.Location;
33+
import org.openjdk.btrace.core.annotations.OnMethod;
34+
import org.openjdk.btrace.core.annotations.Return;
35+
import org.openjdk.btrace.core.annotations.Self;
36+
37+
/** @author Jaroslav Bachorik */
38+
@BTrace
39+
public class ArgsReturnTypeNoMatch {
40+
@OnMethod(
41+
clazz = "/.*\\.OnMethodTest/",
42+
method = "args",
43+
type = "int (java.lang.String, long, java.lang.String[], int[])",
44+
location = @Location(value = Kind.RETURN))
45+
public static void args(
46+
@Self Object self, @Return long retVal, String a, long b, String[] c, int[] d) {
47+
println("args");
48+
}
49+
}

btrace-instr/src/test/java/org/openjdk/btrace/instr/InstrumentorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,21 @@ public void methodEntryArgsDuration() throws Exception {
31353135
+ "MAXLOCALS = 12");
31363136
}
31373137

3138+
@Test
3139+
public void methodEntryArgsReturnTypeMatch() throws Exception {
3140+
loadTargetClass("OnMethodTest");
3141+
transform("onmethod/ArgsReturnTypeMatch");
3142+
checkTransformation(
3143+
"DUP2\nLSTORE 6\nALOAD 0\nLLOAD 6\nALOAD 1\nLLOAD 2\nALOAD 4\nALOAD 5\nINVOKESTATIC resources/OnMethodTest.$btrace$org$openjdk$btrace$runtime$auxiliary$ArgsReturnTypeMatch$args (Ljava/lang/Object;JLjava/lang/String;J[Ljava/lang/String;[I)V");
3144+
}
3145+
3146+
@Test
3147+
public void methodEntryArgsReturnTypeNoMatch() throws Exception {
3148+
loadTargetClass("OnMethodTest");
3149+
transform("onmethod/ArgsReturnTypeNoMatch");
3150+
checkTransformation("");
3151+
}
3152+
31383153
@Test
31393154
public void methodEntryArgsDurationLevel() throws Exception {
31403155
loadTargetClass("OnMethodTest");

0 commit comments

Comments
 (0)