diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApiUtils.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApiUtils.java index 2f3f070b82b0..91d68fd839cc 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApiUtils.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ApiUtils.java @@ -20,6 +20,7 @@ /** * @author emeroad + * @author jaehong.kim */ public final class ApiUtils { @@ -64,4 +65,59 @@ public static String mergeApiDescriptor(String className, String methodName, Str buffer.append(parameterDescriptor); return buffer.toString(); } -} + + public static String toMethodDescriptor(String className, String methodName, String[] parameterType) { + StringBuilder buffer = new StringBuilder(256); + buffer.append(className); + buffer.append("."); + buffer.append(methodName); + + if (parameterType == null || parameterType.length == 0) { + buffer.append(EMPTY_ARRAY); + } else { + buffer.append('('); + int end = parameterType.length - 1; + for (int i = 0; i < parameterType.length; i++) { + buffer.append(parameterType[i]); + if (i < end) { + buffer.append(", "); + } + } + buffer.append(')'); + } + + return buffer.toString(); + } + + public static String toMethodDescriptor(String apiDescriptor) { + if (apiDescriptor == null) { + return ""; + } + + final int methodDescBegin = apiDescriptor.indexOf("("); + if (methodDescBegin == -1) { + throw new IllegalArgumentException("invalid api descriptor=" + apiDescriptor); + } + final int methodDescEnd = apiDescriptor.indexOf(")", methodDescBegin); + if (methodDescEnd == -1) { + throw new IllegalArgumentException("invalid api descriptor=" + apiDescriptor); + } + final int classNameEnd = apiDescriptor.lastIndexOf(".", methodDescBegin); + if (classNameEnd == -1) { + throw new IllegalArgumentException("invalid api descriptor=" + apiDescriptor); + } + + final String className = apiDescriptor.substring(0, classNameEnd); + final String methodName = apiDescriptor.substring(classNameEnd + 1, methodDescBegin); + final String methodDesc = apiDescriptor.substring(methodDescBegin + 1, methodDescEnd); + final String[] parameterTypes = methodDesc.split(","); + for (int i = 0; i < parameterTypes.length; i++) { + final int end = parameterTypes[i].indexOf(" "); + if (end != -1) { + parameterTypes[i] = parameterTypes[i].substring(0, end); + } + } + + return toMethodDescriptor(className, methodName, parameterTypes); + } +} \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java b/profiler/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java index 8181731f032f..34e8e0fcb471 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/test/PluginTestAgent.java @@ -28,6 +28,7 @@ import java.util.NoSuchElementException; import com.navercorp.pinpoint.common.util.AnnotationKeyUtils; +import com.navercorp.pinpoint.profiler.util.ApiUtils; import org.apache.thrift.TBase; import com.google.common.base.Objects; @@ -67,6 +68,7 @@ * @author emeroad * @author koo.taejin * @author hyungil.jeong + * @author jaehong.kim */ public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier { @@ -679,45 +681,30 @@ private void verifySql(ExpectedSql expected, TAnnotation actual) { } private int findApiId(Member method) throws AssertionError { - Class clazz = method.getDeclaringClass(); - - InstrumentClass ic; - try { - ic = getClassPool().getClass(null, clazz.getClassLoader(), clazz.getName(), null); - } catch (InstrumentException e) { - throw new RuntimeException("Cannot get instrumentClass " + clazz.getName(), e); - } - - InstrumentMethod methodInfo; - + String desc; if (method instanceof Method) { - methodInfo = getMethodInfo(ic, (Method) method); + desc = getMethodInfo((Method) method); + } else if (method instanceof Constructor) { - methodInfo = getMethodInfo(ic, (Constructor) method); + desc = getMethodInfo((Constructor) method); } else { throw new IllegalArgumentException("method: " + method); } - - String desc = methodInfo.getDescriptor().getFullName(); - return findApiId(desc); } - private InstrumentMethod getMethodInfo(InstrumentClass ic, Method method) { + private String getMethodInfo(Method method) { Class[] parameterTypes = method.getParameterTypes(); String[] parameterTypeNames = JavaAssistUtils.toPinpointParameterType(parameterTypes); - - return ic.getDeclaredMethod(method.getName(), parameterTypeNames); + return ApiUtils.toMethodDescriptor(method.getDeclaringClass().getName(), method.getName(), parameterTypeNames); } - private InstrumentMethod getMethodInfo(InstrumentClass ic, Constructor constructor) { + private String getMethodInfo(Constructor constructor) { Class[] parameterTypes = constructor.getParameterTypes(); String[] parameterTypeNames = JavaAssistUtils.getParameterType(parameterTypes); - - return ic.getConstructor(parameterTypeNames); + return ApiUtils.toMethodDescriptor(constructor.getDeclaringClass().getName(), constructor.getName(), parameterTypeNames); } - private int findApiId(String desc) throws AssertionError { try { return getTestTcpDataSender().getApiId(desc); diff --git a/profiler/src/main/java/com/navercorp/pinpoint/test/TestTcpDataSender.java b/profiler/src/main/java/com/navercorp/pinpoint/test/TestTcpDataSender.java index 30d6dec3f918..675f80ebbc77 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/test/TestTcpDataSender.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/test/TestTcpDataSender.java @@ -15,6 +15,7 @@ package com.navercorp.pinpoint.test; import com.navercorp.pinpoint.profiler.sender.EnhancedDataSender; +import com.navercorp.pinpoint.profiler.util.ApiUtils; import com.navercorp.pinpoint.rpc.FutureListener; import com.navercorp.pinpoint.rpc.ResponseMessage; import com.navercorp.pinpoint.rpc.client.PinpointClientReconnectEventListener; @@ -35,7 +36,7 @@ /** * @author Jongho Moon - * + * @author jaehong.kim */ public class TestTcpDataSender implements EnhancedDataSender { private final List> datas = new ArrayList>(); @@ -74,7 +75,8 @@ private void addData(TBase data) { } apiIdMap.put(md.getApiId(), api); - apiDescriptionMap.put(api, md.getApiId()); + final String key = ApiUtils.toMethodDescriptor(api); + apiDescriptionMap.put(key, md.getApiId()); } else if (data instanceof TSqlMetaData) { TSqlMetaData md = (TSqlMetaData)data; diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ApiUtilsTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ApiUtilsTest.java new file mode 100644 index 000000000000..628f2bc825ec --- /dev/null +++ b/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ApiUtilsTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.profiler.util; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author jaehong.kim + */ +public class ApiUtilsTest { + + @Test + public void toMethodDescriptor() { + String result = ApiUtils.toMethodDescriptor("com.navercorp.test.pinpoint.plugin.spring.beans.Outer.setInner(com.navercorp.test.pinpoint.plugin.spring.beans.Inner Inner):23"); + assertEquals("com.navercorp.test.pinpoint.plugin.spring.beans.Outer.setInner(com.navercorp.test.pinpoint.plugin.spring.beans.Inner)", result); + + String desc = ApiUtils.toMethodDescriptor("com.navercorp.test.pinpoint.plugin.spring.beans.Outer", "setInner", new String[]{"com.navercorp.test.pinpoint.plugin.spring.beans.Inner"}); + assertEquals(result, desc); + } +} \ No newline at end of file