Skip to content

Commit

Permalink
Removed return type parameter from get/hasMethod()
Browse files Browse the repository at this point in the history
google-httpclient plugin uses new plugin api
  • Loading branch information
lioolli committed Aug 11, 2015
1 parent 44de5c5 commit 0425c5c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 112 deletions.
Expand Up @@ -39,28 +39,24 @@ public interface InstrumentClass {

String[] getInterfaces();

InstrumentMethod getConstructor(String[] parameterTypes);
InstrumentMethod getConstructor(String... parameterTypes);

List<InstrumentMethod> getDeclaredMethods();

List<InstrumentMethod> getDeclaredMethods(MethodFilter filter);

InstrumentMethod getDeclaredMethod(String name, String[] parameterTypes);
InstrumentMethod getDeclaredMethod(String name, String... parameterTypes);

ClassLoader getClassLoader();


public boolean isInterceptable();

boolean hasConstructor(String[] parameterTypes);
boolean hasConstructor(String... parameterTypes);

boolean hasDeclaredMethod(String methodName, String[] parameterTypes, String returnType);
boolean hasDeclaredMethod(String methodName, String... parameterTypes);

boolean hasDeclaredMethod(String methodName, String[] parameterTypes);

boolean hasMethod(String methodName, String[] parameterTypes, String returnType);

boolean hasMethod(String methodName, String[] parameterTypes);
boolean hasMethod(String methodName, String... parameterTypes);

boolean hasField(String name, String type);

Expand Down
Expand Up @@ -28,7 +28,7 @@
public interface ProfilerPluginInstrumentContext {
public TraceContext getTraceContext();

public InstrumentClass getInstrumentClass(ClassLoader classLoader, String className, byte[] classFileBuffer);
public InstrumentClass getInstrumentClass(ClassLoader classLoader, String className, byte[] classfileBuffer);

public InterceptorGroup getInterceptorGroup(String name);

Expand Down
Expand Up @@ -32,8 +32,8 @@ public static ClassCondition hasField(String name, String type) {
return new HasField(name, type);
}

public static ClassCondition hasMethod(String name, String returnType, String... paramTypes) {
return new HasMethod(name, returnType, paramTypes);
public static ClassCondition hasMethod(String name, String... paramTypes) {
return new HasMethod(name, paramTypes);
}

public static ClassCondition hasDeclaredMethod(String name, String... paramTypes) {
Expand Down Expand Up @@ -78,26 +78,22 @@ public String toString() {

private static class HasMethod implements ClassCondition {
private final String name;
private final String returnType;
private final String[] paramTypes;

public HasMethod(String name, String returnType, String... paramTypes) {
public HasMethod(String name, String... paramTypes) {
this.name = name;
this.returnType = returnType;
this.paramTypes = paramTypes;
}

@Override
public boolean check(ProfilerPluginSetupContext context, ClassLoader classLoader, InstrumentClass target) {
return target.hasMethod(name, paramTypes, returnType);
return target.hasMethod(name, paramTypes);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("HasMethod[");
builder.append(returnType);
builder.append(' ');
builder.append(name);
builder.append('(');

Expand Down
Expand Up @@ -23,5 +23,5 @@


public interface PinpointClassFileTransformer {
public byte[] transform(ProfilerPluginInstrumentContext context, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException;
public byte[] transform(ProfilerPluginInstrumentContext instrumentContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException;
}
Expand Up @@ -82,7 +82,7 @@ private void addArcusClientEditor(ProfilerPluginSetupContext context, final Arcu
public byte[] transform(ProfilerPluginInstrumentContext context, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = context.getInstrumentClass(loader, className, classfileBuffer);

if (target.hasMethod("addOp", new String[] { "java.lang.String", "net.spy.memcached.ops.Operation" }, "net.spy.memcached.ops.Operation")) {
if (target.hasMethod("addOp", "java.lang.String", "net.spy.memcached.ops.Operation")) {
boolean traceKey = config.isArcusKeyTrace();

target.addInterceptor("com.navercorp.pinpoint.plugin.arcus.interceptor.SetCacheManagerInterceptor");
Expand Down
Expand Up @@ -15,19 +15,18 @@
*/
package com.navercorp.pinpoint.plugin.google.httpclient;

import java.security.ProtectionDomain;

import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod;
import com.navercorp.pinpoint.bootstrap.interceptor.AsyncTraceIdAccessor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginInstrumentContext;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.ClassCondition;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.ClassFileTransformerBuilder;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.ConditionalClassFileTransformerBuilder;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.ConditionalClassFileTransformerSetup;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.ConstructorTransformerBuilder;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.MethodTransformerBuilder;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.MethodTransformerExceptionHandler;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.MethodTransformerProperty;
import com.navercorp.pinpoint.bootstrap.plugin.transformer.PinpointClassFileTransformer;

/**
* @author jaehong.kim
Expand Down Expand Up @@ -58,56 +57,60 @@ public void setup(ProfilerPluginSetupContext context) {
}
}

private void addHttpRequestClass(ProfilerPluginSetupContext context, HttpClientPluginConfig config) {
final ClassFileTransformerBuilder classBuilder = context.getClassFileTransformerBuilder("com.google.api.client.http.HttpRequest");

MethodTransformerBuilder executeMethodBuilder = classBuilder.editMethod("execute");
executeMethodBuilder.property(MethodTransformerProperty.IGNORE_IF_NOT_EXIST);
executeMethodBuilder.injectInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteMethodInterceptor");

if (config.isAsync()) {
MethodTransformerBuilder executeAsyncMethodBuilder = classBuilder.editMethod("executeAsync", "java.util.concurrent.Executor");
executeAsyncMethodBuilder.property(MethodTransformerProperty.IGNORE_IF_NOT_EXIST);
executeAsyncMethodBuilder.injectInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInterceptor");
}

context.addClassFileTransformer(classBuilder.build());
}

private void addHttpRequestExecuteAsyncMethodInnerClass(ProfilerPluginSetupContext context, String targetClassName) {
final ClassFileTransformerBuilder classBuilder = context.getClassFileTransformerBuilder(targetClassName);
classBuilder.injectMetadata(METADATA_ASYNC_TRACE_ID);

classBuilder.conditional(new ClassCondition() {
private void addHttpRequestClass(ProfilerPluginSetupContext context, final HttpClientPluginConfig config) {
context.addClassFileTransformer("com.google.api.client.http.HttpRequest", new PinpointClassFileTransformer() {

@Override
public boolean check(ProfilerPluginSetupContext context, ClassLoader classLoader, InstrumentClass target) {
if (!target.hasConstructor(new String[] { "com.google.api.client.http.HttpRequest" })) {
return false;
}

if (!target.hasMethod("call", null, "com.google.api.client.http.HttpResponse")) {
return false;
public byte[] transform(ProfilerPluginInstrumentContext instrumentContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentContext.getInstrumentClass(loader, className, classfileBuffer);


InstrumentMethod execute = target.getDeclaredMethod("execute", new String[] {});

if (execute != null) {
execute.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteMethodInterceptor");
}

return true;

if (config.isAsync()) {
InstrumentMethod executeAsync = target.getDeclaredMethod("executeAsync", "java.util.concurrent.Executor");

if (executeAsync != null) {
executeAsync.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInterceptor");
}
}

return target.toBytecode();
}
}, new ConditionalClassFileTransformerSetup() {
});
}

private void addHttpRequestExecuteAsyncMethodInnerClass(ProfilerPluginSetupContext context, String targetClassName) {
context.addClassFileTransformer(targetClassName, new PinpointClassFileTransformer() {

@Override
public void setup(ConditionalClassFileTransformerBuilder conditional) {
ConstructorTransformerBuilder constructorBuilder = conditional.editConstructor("com.google.api.client.http.HttpRequest");
constructorBuilder.injectInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassConstructorInterceptor");

MethodTransformerBuilder methodBuilder = conditional.editMethods(new HttpRequestExceuteAsyncMethodInnerClassMethodFilter());
methodBuilder.exceptionHandler(new MethodTransformerExceptionHandler() {
public void handle(String targetClassName, String targetMethodName, String[] targetMethodParameterTypes, Throwable exception) throws Throwable {
if (logger.isWarnEnabled()) {
logger.warn("[GoogleHttpClient] Unsupported method " + targetClassName + "." + targetMethodName, exception);
public byte[] transform(ProfilerPluginInstrumentContext instrumentContext, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
InstrumentClass target = instrumentContext.getInstrumentClass(loader, className, classfileBuffer);
target.addField(AsyncTraceIdAccessor.class.getName());

if (target.hasConstructor("com.google.api.client.http.HttpRequest") && target.hasMethod("call", "com.google.api.client.http.HttpResponse")) {
InstrumentMethod constructor = target.getConstructor("com.google.api.client.http.HttpRequest");
constructor.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassConstructorInterceptor");

for (InstrumentMethod m : target.getDeclaredMethods(new HttpRequestExceuteAsyncMethodInnerClassMethodFilter())) {
try {
m.addInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassCallMethodInterceptor");
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("[GoogleHttpClient] Unsupported method " + className + "." + m.getName(), t);
}
}

}
});
methodBuilder.injectInterceptor("com.navercorp.pinpoint.plugin.google.httpclient.interceptor.HttpRequestExecuteAsyncMethodInnerClassCallMethodInterceptor");
}

return target.toBytecode();
}
});
context.addClassFileTransformer(classBuilder.build());
}
}
}
Expand Up @@ -36,8 +36,8 @@
import com.navercorp.pinpoint.bootstrap.FieldAccessor;
import com.navercorp.pinpoint.bootstrap.MetadataAccessor;
import com.navercorp.pinpoint.bootstrap.instrument.DefaultInterceptorGroupDefinition;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod;
import com.navercorp.pinpoint.bootstrap.instrument.InterceptorGroupDefinition;
import com.navercorp.pinpoint.bootstrap.instrument.MethodFilter;
Expand Down Expand Up @@ -146,15 +146,18 @@ private CtMethod getCtMethod(String methodName, String[] parameterTypes) throws
return method;
}

public InstrumentMethod getDeclaredMethod(String name, String[] parameterTypes) {
@Override
public InstrumentMethod getDeclaredMethod(String name, String... parameterTypes) {
CtMethod method = getCtMethod0(ctClass, name, parameterTypes);
return method == null ? null : new JavassistMethod(pluginContext, interceptorRegistryBinder, this, method);
}

@Override
public List<InstrumentMethod> getDeclaredMethods() {
return getDeclaredMethods(MethodFilters.ACCEPT_ALL);
}

@Override
public List<InstrumentMethod> getDeclaredMethods(MethodFilter methodFilter) {
if (methodFilter == null) {
throw new NullPointerException("methodFilter must not be null");
Expand Down Expand Up @@ -195,48 +198,20 @@ private CtConstructor getCtConstructor0(String[] parameterTypes) {
return null;
}

public InstrumentMethod getConstructor(String[] parameterTypes) {
@Override
public InstrumentMethod getConstructor(String... parameterTypes) {
CtConstructor constructor = getCtConstructor0(parameterTypes);
return constructor == null ? null : new JavassistMethod(pluginContext, interceptorRegistryBinder, this, constructor);
}


@Override
public boolean hasDeclaredMethod(String methodName, String[] args) {
public boolean hasDeclaredMethod(String methodName, String... args) {
return getCtMethod0(ctClass, methodName, args) != null;
}

@Override
public boolean hasDeclaredMethod(String methodName, String[] parameterTypes, String returnType) {
final String signature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes, returnType);

try {
for (CtMethod method : ctClass.getDeclaredMethods(methodName)) {
final String descriptor = method.getMethodInfo2().getDescriptor();
if (descriptor.startsWith(signature)) {
return true;
}
}
} catch (NotFoundException e) {
return false;
}

return false;
}

@Override
public boolean hasMethod(String methodName, String[] parameterTypeArray, String returnType) {
final String signature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypeArray, returnType);
try {
CtMethod m = ctClass.getMethod(methodName, signature);
return m != null;
} catch (NotFoundException e) {
return false;
}
}

@Override
public boolean hasMethod(String methodName, String[] parameterTypes) {
public boolean hasMethod(String methodName, String... parameterTypes) {
final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes);

for (CtMethod method : ctClass.getMethods()) {
Expand All @@ -254,7 +229,7 @@ public boolean hasMethod(String methodName, String[] parameterTypes) {
}

@Override
public boolean hasConstructor(String[] parameterTypeArray) {
public boolean hasConstructor(String... parameterTypeArray) {
final String signature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypeArray, "void");
try {
CtConstructor c = ctClass.getConstructor(signature);
Expand Down
Expand Up @@ -17,15 +17,15 @@

import java.security.ProtectionDomain;

import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher;
import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matchers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.navercorp.pinpoint.bootstrap.Agent;
import com.navercorp.pinpoint.bootstrap.instrument.ByteCodeInstrumentor;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher;
import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matchers;
import com.navercorp.pinpoint.bootstrap.interceptor.Interceptor;
import com.navercorp.pinpoint.profiler.modifier.AbstractModifier;

Expand Down Expand Up @@ -56,11 +56,11 @@ public byte[] modify(ClassLoader classLoader, String javassistClassName, Protect
try {
InstrumentClass mdcClass = byteCodeInstrumentor.getClass(classLoader, "org.apache.log4j.MDC", classFileBuffer);

if (!mdcClass.hasMethod("put", new String[]{"java.lang.String", "java.lang.Object"}, "void")) {
if (!mdcClass.hasMethod("put", new String[]{"java.lang.String", "java.lang.Object"})) {
logger.warn("modify fail. Because put method does not existed org.apache.log4j.MDC class.");
return null;
}
if (!mdcClass.hasMethod("remove", new String[]{"java.lang.String"}, "void")) {
if (!mdcClass.hasMethod("remove", new String[]{"java.lang.String"})) {
logger.warn("modify fail. Because remove method does not existed org.apache.log4j.MDC class.");
return null;
}
Expand Down
Expand Up @@ -56,11 +56,11 @@ public byte[] modify(ClassLoader classLoader, String javassistClassName, Protect
try {
InstrumentClass mdcClass = byteCodeInstrumentor.getClass(classLoader, "org.slf4j.MDC", classFileBuffer);

if (!mdcClass.hasMethod("put", new String[]{"java.lang.String", "java.lang.String"}, "void")) {
if (!mdcClass.hasMethod("put", new String[]{"java.lang.String", "java.lang.String"})) {
logger.warn("modify fail. Because put method does not existed org.slf4j.MDC class.");
return null;
}
if (!mdcClass.hasMethod("remove", new String[]{"java.lang.String"}, "void")) {
if (!mdcClass.hasMethod("remove", new String[]{"java.lang.String"})) {
logger.warn("modify fail. Because remove method does not existed org.slf4j.MDC class.");
return null;
}
Expand Down

0 comments on commit 0425c5c

Please sign in to comment.