Skip to content

Commit

Permalink
[#674] Add Logic to generate delegator method in JavaAssistClass
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoo-jung committed Jun 30, 2015
1 parent b77dabc commit 7553c5c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
Expand Up @@ -121,5 +121,9 @@ public interface InstrumentClass {

void addGetter(Class<?> interfaceType, String fieldName) throws InstrumentException;

/**
* You should check that class already have Declared method.
* If class already have method, this method throw exception.
*/
void addDelegatorMethod(String methodName, String[] args) throws InstrumentException;
}
Expand Up @@ -64,6 +64,7 @@
/**
* @author emeroad
* @author netspider
* @author minwoo.jung
*/
public class JavaAssistClass implements InstrumentClass {

Expand Down Expand Up @@ -1034,7 +1035,7 @@ private String getWrapperClassName(String primitiveType) {
}


private CtMethod getMethod(CtClass ctClass, String methodName, String[] args) {
private static CtMethod getMethod(CtClass ctClass, String methodName, String[] args) {
final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(args);

for (CtMethod method : ctClass.getDeclaredMethods()) {
Expand All @@ -1049,40 +1050,17 @@ private CtMethod getMethod(CtClass ctClass, String methodName, String[] args) {

return null;
}
/**
* You should check that class already have method.
* If class already have method, this method throw exception.
*/

@Override
public void addDelegatorMethod(String methodName, String[] args) throws InstrumentException {
final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(args);

for (CtMethod method : ctClass.getDeclaredMethods()) {
if (!method.getName().equals(methodName)) {
continue;
}
final String descriptor = method.getMethodInfo2().getDescriptor();
if (descriptor.startsWith(jvmSignature)) {
// skip return type check
throw new InstrumentException(getName() + "already have method(" + methodName +").");
}
if (getMethod(ctClass, methodName, args) != null) {
throw new InstrumentException(getName() + "already have method(" + methodName +").");
}

try {
final CtClass superClass = ctClass.getSuperclass();
CtMethod superMethod = null;
CtMethod superMethod = getMethod(superClass, methodName, args);

for (CtMethod method : superClass.getDeclaredMethods()) {
if (!method.getName().equals(methodName)) {
continue;
}
final String descriptor = method.getMethodInfo2().getDescriptor();
// skip return type check
if (descriptor.startsWith(jvmSignature)) {
superMethod = method;
break;
}
}
if (superMethod == null) {
throw new NotFoundInstrumentException(methodName + Arrays.toString(args) + " is not found in " + superClass.getName());
}
Expand Down
Expand Up @@ -30,12 +30,16 @@ public byte[] modify(ClassLoader classLoader, String javassistClassName, Protect
}

try {
InstrumentClass loggingEvent = byteCodeInstrumentor.getClass(classLoader, javassistClassName, classFileBuffer);
loggingEvent.addDelegatorMethod("append", new String[]{"org.apache.log4j.spi.LoggingEvent"});
InstrumentClass nelo2AsyncAppender = byteCodeInstrumentor.getClass(classLoader, javassistClassName, classFileBuffer);

if (!nelo2AsyncAppender.hasDeclaredMethod("append", new String[]{"org.apache.log4j.spi.LoggingEvent"})) {
nelo2AsyncAppender.addDelegatorMethod("append", new String[]{"org.apache.log4j.spi.LoggingEvent"});
}

Interceptor interceptor = byteCodeInstrumentor.newInterceptor(classLoader, protectedDomain, "com.navercorp.pinpoint.profiler.modifier.log.nelo.interceptor.AppenderInterceptor");
loggingEvent.addInterceptor("append", new String[]{"org.apache.log4j.spi.LoggingEvent"}, interceptor);
nelo2AsyncAppender.addInterceptor("append", new String[]{"org.apache.log4j.spi.LoggingEvent"}, interceptor);

return loggingEvent.toBytecode();
return nelo2AsyncAppender.toBytecode();
} catch (InstrumentException e) {
logger.warn("modify fail. Cause:" + e.getMessage(), e);
return null;
Expand Down
Expand Up @@ -30,12 +30,12 @@ public byte[] modify(ClassLoader classLoader, String javassistClassName, Protect
}

try {
InstrumentClass loggingEvent = byteCodeInstrumentor.getClass(classLoader, javassistClassName, classFileBuffer);
InstrumentClass neloAppender = byteCodeInstrumentor.getClass(classLoader, javassistClassName, classFileBuffer);

Interceptor interceptor = byteCodeInstrumentor.newInterceptor(classLoader, protectedDomain, "com.navercorp.pinpoint.profiler.modifier.log.nelo.interceptor.AppenderInterceptor");
loggingEvent.addInterceptor("append", new String[]{"org.apache.log4j.spi.LoggingEvent"}, interceptor);
neloAppender.addInterceptor("append", new String[]{"org.apache.log4j.spi.LoggingEvent"}, interceptor);

return loggingEvent.toBytecode();
return neloAppender.toBytecode();
} catch (InstrumentException e) {
logger.warn("modify fail. Cause:" + e.getMessage(), e);
return null;
Expand Down

0 comments on commit 7553c5c

Please sign in to comment.