ApacheHttpClientClassHandler extends SubTypeInstrumentationHandler { protected boolean transform(CtClass cc, ClassPool pool) throws NotFoundException, CannotCompileException, IOException { boolean transformedOneMethod = false; for (CtMethod method : cc.getDeclaredMethods()) { if ("execute".equals(method.getName()) && !Modifier.isAbstract(method.getModifiers())) { String hostParameter = "null"; String requestParameter = "null"; String requestUriParameter = "null"; String handlerParameter = "null"; CtClass[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { if (parameterTypes[i].subtypeOf(pool.get("org.apache.http.HttpHost"))) { hostParameter = "$" + (i + 1); } else if (parameterTypes[i].subtypeOf(pool.get("org.apache.http.client.methods.HttpUriRequest"))) { requestParameter = "$" + (i + 1); requestUriParameter = "$" + (i + 1); } else if (parameterTypes[i].subtypeOf(pool.get("org.apache.http.HttpRequest"))) { requestParameter = "$" + (i + 1); } } CtMethod copiedMethod = CtNewMethod.copy(method, cc.makeUniqueName("execute"), cc, null); copiedMethod.setModifiers(javassist.Modifier.PRIVATE); cc.addMethod(copiedMethod); final String source = Resources.toString(Resources.getResource(getClass(), "HttpClient_execute.javassist"), Charset.forName("utf-8")); pool.importPackage("java.net"); pool.importPackage("org.apache.http"); pool.importPackage("org.apache.http.client.methods"); pool.importPackage("com.yammer.telemetry.tracing"); pool.importPackage("com.yammer.telemetry.agent.handlers"); String body = source.replace("%HTTP_HOST_PARAM%", hostParameter) .replace("%HTTP_REQUEST_PARAM%", requestParameter) .replace("%HTTP_REQUEST_URI_PARAM%", requestUriParameter) .replace("%RESPONSE_HANDLER_PARAM%", handlerParameter); method.setBody(body, "this", copiedMethod.getName()); transformedOneMethod = true; } } return transformedOneMethod; } ApacheHttpClientClassHandler();   }
@Test public void testHttpClientDoesNotGetTransformedBecauseItIsAbstract() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get(HttpClient.class.getName()); assertFalse(handler.transform(ctClass, cp)); }
@Test public void testHttpClientSubClassDoesGetTransformedIfNonAbstractExecuteMethodExists() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get(HttpClientSubClass.class.getName()); assertTrue(handler.transform(ctClass, cp)); }
@Test public void testHttpClientAbstractSubClassDoesNotGetTransformed() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get(HttpClientAbstractSubClass.class.getName()); assertFalse(handler.transform(ctClass, cp)); }
@Test public void testHttpClientSubClassGetsTransformed() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get(AbstractHttpClient.class.getName()); assertTrue(handler.transform(ctClass, cp)); }
@Test public void testNonHttpClientClassDoesNotGetTransformed() throws Exception { ClassPool cp = ClassPool.getDefault(); CtClass ctClass = cp.get(String.class.getName()); assertFalse(handler.transform(ctClass, cp)); }