Skip to content

Commit

Permalink
fix key of api descriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim authored and emeroad committed Nov 8, 2016
1 parent ba08a24 commit 16e0982
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 26 deletions.
Expand Up @@ -20,6 +20,7 @@

/**
* @author emeroad
* @author jaehong.kim
*/
public final class ApiUtils {

Expand Down Expand Up @@ -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);
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,7 @@
* @author emeroad
* @author koo.taejin
* @author hyungil.jeong
* @author jaehong.kim
*/
public class PluginTestAgent extends DefaultAgent implements PluginTestVerifier {

Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand All @@ -35,7 +36,7 @@

/**
* @author Jongho Moon
*
* @author jaehong.kim
*/
public class TestTcpDataSender implements EnhancedDataSender {
private final List<TBase<?, ?>> datas = new ArrayList<TBase<?, ?>>();
Expand Down Expand Up @@ -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;

Expand Down
@@ -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);
}
}

0 comments on commit 16e0982

Please sign in to comment.