/* (non-Javadoc)
   * @see com.navercorp.pinpoint.bootstrap.plugin.transformer.PinpointClassFileTransformer#transform(com.navercorp.pinpoint.bootstrap.plugin.PinpointInstrument, java.lang.ClassLoader, java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[])
   */
  @Override
  public byte[] transform(
      Instrumentor instrumentContext,
      ClassLoader loader,
      String className,
      Class<?> classBeingRedefined,
      ProtectionDomain protectionDomain,
      byte[] classfileBuffer)
      throws InstrumentException {
    if (logger.isInfoEnabled()) {
      logger.info("Modify {}", className);
    }

    try {
      InstrumentClass target =
          instrumentContext.getInstrumentClass(loader, className, classfileBuffer);

      if (!target.isInterceptable()) {
        return null;
      }

      List<InstrumentMethod> methodList = target.getDeclaredMethods(METHOD_FILTER);
      for (InstrumentMethod method : methodList) {
        if (logger.isTraceEnabled()) {
          logger.trace(
              "### c={}, m={}, params={}",
              new Object[] {
                className, method.getName(), Arrays.toString(method.getParameterTypes())
              });
        }

        method.addInterceptor(
            BasicMethodInterceptor.class.getName(), va(SpringBeansConstants.SERVICE_TYPE));
      }

      return target.toBytecode();
    } catch (Exception e) {
      logger.warn("modify fail. Cause:{}", e.getMessage(), e);
      return null;
    }
  }
  @Test
  public void testDeclaredMethods() throws InstrumentException {

    JavassistClassPool pool = new JavassistClassPool(new GlobalInterceptorRegistryBinder(), null);

    String testObjectName = "com.navercorp.pinpoint.profiler.interceptor.bci.TestObject";

    InstrumentClass testObject = pool.getClass(null, testObjectName, null);
    Assert.assertEquals(testObject.getName(), testObjectName);

    int findMethodCount = 0;
    for (InstrumentMethod methodInfo : testObject.getDeclaredMethods()) {
      if (!methodInfo.getName().equals("callA")) {
        continue;
      }
      String[] parameterTypes = methodInfo.getParameterTypes();
      if (parameterTypes == null || parameterTypes.length == 0) {
        findMethodCount++;
      }
    }
    Assert.assertEquals(findMethodCount, 1);
  }
 @Override
 public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
   for (InstrumentMethod targetMethod : target.getDeclaredMethods(filter)) {
     for (MethodRecipe recipe : recipes) {
       try {
         recipe.edit(classLoader, target, targetMethod);
       } catch (Throwable t) {
         if (exceptionHandler != null) {
           exceptionHandler.handle(
               target.getName(), targetMethod.getName(), targetMethod.getParameterTypes(), t);
           logger.info(
               "Exception thrown while editing"
                   + targetMethod.getDescriptor().getApiDescriptor()
                   + " but MethodTransformerExceptionHandler handled it.",
               t);
         } else {
           throw new InstrumentException(
               "Fail to edit method " + targetMethod.getDescriptor().getApiDescriptor(), t);
         }
       }
     }
   }
 }