diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java index 6d8c8c6af5d4418e19b7f4f474fae8fb65c7646e..018de2136ddcf26f0632329edfcc1018b2c14304 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatch.java @@ -1,6 +1,7 @@ package com.a.eye.skywalking.api.plugin.bytebuddy; import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.method.ParameterList; import net.bytebuddy.matcher.ElementMatcher; /** @@ -39,8 +40,9 @@ public class ArgumentTypeNameMatch implements ElementMatcher */ @Override public boolean matches(MethodDescription target) { - if (target.getParameters().size() > index) { - return target.getParameters().get(index).getType().asErasure().getName().equals(argumentTypeName); + ParameterList parameters = target.getParameters(); + if (parameters.size() > index) { + return parameters.get(index).getType().asErasure().getName().equals(argumentTypeName); } return false; diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8721ae449bde620fa055b7bb09af0d93c1bf8522 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/bytebuddy/ArgumentTypeNameMatchTest.java @@ -0,0 +1,39 @@ +package com.a.eye.skywalking.api.plugin.bytebuddy; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.method.ParameterDescription; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.when; + +/** + * @author wusheng + */ +public class ArgumentTypeNameMatchTest { + @Test + public void testMatches() throws IllegalAccessException { + MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS); + ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS); + when(methodDescription.getParameters().get(0)).thenReturn(parameterDescription); + when(methodDescription.getParameters().size()).thenReturn(1); + when(parameterDescription.getType().asErasure().getName()).thenReturn("com.a.eye.TestClass"); + + ArgumentTypeNameMatch matcher = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass")); + Assert.assertTrue(matcher.matches(methodDescription)); + + ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass2")); + Assert.assertFalse(matcher2.matches(methodDescription)); + } + + @Test + public void testMatchesWithNoParameters(){ + MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS); + ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS); + when(methodDescription.getParameters().size()).thenReturn(0); + + ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch)ArgumentTypeNameMatch.takesArgumentWithType(0, "com.a.eye.TestClass")); + Assert.assertFalse(matcher2.matches(methodDescription)); + } +}