Skip to content

Commit

Permalink
Use return type value when 'type' matcher is used.
Browse files Browse the repository at this point in the history
Fixes #517
  • Loading branch information
jbachorik committed Dec 26, 2021
1 parent f20e15a commit cd650f9
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1807,9 +1807,12 @@ private boolean typeMatches(String decl, String desc, boolean exactTypeMatch) {
return true;
} else {
String d = TypeUtils.declarationToDescriptor(decl);
Type[] args1 = Type.getArgumentTypes(d);
Type[] args2 = Type.getArgumentTypes(desc);
return InstrumentUtils.isAssignable(args1, args2, cl, exactTypeMatch);
Type[] argTypesLeft = Type.getArgumentTypes(d);
Type[] argTypesRight = Type.getArgumentTypes(desc);
Type retTypeLeft = Type.getReturnType(d);
Type retTypeRight = Type.getReturnType(desc);
return InstrumentUtils.isAssignable(retTypeLeft, retTypeRight, cl, exactTypeMatch)
&& InstrumentUtils.isAssignable(argTypesLeft, argTypesRight, cl, exactTypeMatch);
}
}

Expand Down
49 changes: 49 additions & 0 deletions btrace-instr/src/test/btrace/onmethod/ArgsReturnTypeMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package traces.onmethod;

import static org.openjdk.btrace.core.BTraceUtils.*;

import org.openjdk.btrace.core.annotations.BTrace;
import org.openjdk.btrace.core.annotations.Kind;
import org.openjdk.btrace.core.annotations.Location;
import org.openjdk.btrace.core.annotations.OnMethod;
import org.openjdk.btrace.core.annotations.Return;
import org.openjdk.btrace.core.annotations.Self;

/** @author Jaroslav Bachorik */
@BTrace
public class ArgsReturnTypeMatch {
@OnMethod(
clazz = "/.*\\.OnMethodTest/",
method = "args",
type = "long (java.lang.String, long, java.lang.String[], int[])",
location = @Location(value = Kind.RETURN))
public static void args(
@Self Object self, @Return long retVal, String a, long b, String[] c, int[] d) {
println("args");
}
}
49 changes: 49 additions & 0 deletions btrace-instr/src/test/btrace/onmethod/ArgsReturnTypeNoMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package traces.onmethod;

import static org.openjdk.btrace.core.BTraceUtils.*;

import org.openjdk.btrace.core.annotations.BTrace;
import org.openjdk.btrace.core.annotations.Kind;
import org.openjdk.btrace.core.annotations.Location;
import org.openjdk.btrace.core.annotations.OnMethod;
import org.openjdk.btrace.core.annotations.Return;
import org.openjdk.btrace.core.annotations.Self;

/** @author Jaroslav Bachorik */
@BTrace
public class ArgsReturnTypeNoMatch {
@OnMethod(
clazz = "/.*\\.OnMethodTest/",
method = "args",
type = "int (java.lang.String, long, java.lang.String[], int[])",
location = @Location(value = Kind.RETURN))
public static void args(
@Self Object self, @Return long retVal, String a, long b, String[] c, int[] d) {
println("args");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3135,6 +3135,21 @@ public void methodEntryArgsDuration() throws Exception {
+ "MAXLOCALS = 12");
}

@Test
public void methodEntryArgsReturnTypeMatch() throws Exception {
loadTargetClass("OnMethodTest");
transform("onmethod/ArgsReturnTypeMatch");
checkTransformation(
"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");
}

@Test
public void methodEntryArgsReturnTypeNoMatch() throws Exception {
loadTargetClass("OnMethodTest");
transform("onmethod/ArgsReturnTypeNoMatch");
checkTransformation("");
}

@Test
public void methodEntryArgsDurationLevel() throws Exception {
loadTargetClass("OnMethodTest");
Expand Down

0 comments on commit cd650f9

Please sign in to comment.