public Object invoke() throws Throwable {
   Object returnValue = null;
   if (index < ans.length) {
     Annotation annotation = ans[index++];
     if (pluginMap.containsKey(annotation.annotationType())) {
       returnValue =
           pluginMap
               .get(annotation.annotationType())
               .intercept(annotation, this, args, target, method, isHandled);
     }
     if (isHandled[0]) {
       return returnValue;
     }
   } else if (index++ == ans.length) {
     try {
       if (useInjectTarget) returnValue = methodProxy.invoke(target, args);
       else returnValue = methodProxy.invokeSuper(target, args);
     } catch (InvocationTargetException e) {
       Throwable t = e.getTargetException();
       throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(e);
     } catch (RuntimeException e) {
       throw e;
     } catch (Throwable t) {
       throw new RuntimeException(t);
     }
     return returnValue;
   }
   return returnValue;
 }
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   Object returnObj = null;
   if (openConnection) {
     ConnectionHandler.getConnection();
     if (CacheMark.getTran(method)) {
       try {
         ConnectionHandler.beginTransaction();
         returnObj = proxy.invokeSuper(obj, args);
         ConnectionHandler.commit();
       } catch (Exception e) {
         ConnectionHandler.rollback();
         MethodParamHandler.getMethodParam(e, method, args);
       }
     } else {
       try {
         returnObj = proxy.invokeSuper(obj, args);
       } catch (Exception e) {
         MethodParamHandler.getMethodParam(e, method, args);
       }
     }
     ConnectionHandler.close();
   } else {
     returnObj = proxy.invokeSuper(obj, args);
   }
   return returnObj;
 }
Пример #3
0
  public Object intercept(
      Object obj, java.lang.reflect.Method method, Object[] args, MethodProxy proxy)
      throws Throwable {
    printIdent(ident);
    System.out.println(method);
    for (int i = 0; i < args.length; i++) {
      printIdent(ident);
      System.out.print("arg" + (i + 1) + ": ");
      if (obj == args[i]) System.out.println("this");
      else System.out.println(args[i]);
    }
    ident++;

    Object retValFromSuper = null;
    try {
      retValFromSuper = proxy.invokeSuper(obj, args);
      ident--;
    } catch (Throwable t) {
      ident--;
      printIdent(ident);
      System.out.println("throw " + t);
      System.out.println();
      throw t.fillInStackTrace();
    }

    printIdent(ident);
    System.out.print("return ");
    if (obj == retValFromSuper) System.out.println("this");
    else System.out.println(retValFromSuper);

    if (ident == 1) System.out.println();

    return retValFromSuper;
  }
Пример #4
0
 public Object intercept(Object obj, Method m, Object[] args, MethodProxy proxy) throws Throwable {
   // TODO Auto-generated method stub
   System.out.println("实际类方法调用前,cglib 代理类逻辑");
   proxy.invokeSuper(obj, args);
   System.out.println("实际类方法调用后,cglib 代理类逻辑");
   return null;
 }
 public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   if (Modifier.isAbstract(method.getModifiers())) {
     return invoker.invoke(o, method, args);
   } else {
     return methodProxy.invokeSuper(o, args);
   }
 }
Пример #6
0
 @Override
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   System.out.println("===== before method : " + method);
   Object ret = proxy.invokeSuper(obj, args);
   System.out.println("===== before method : " + method);
   return ret;
 }
Пример #7
0
 @Override
 public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   System.out.println("事物开始");
   methodProxy.invokeSuper(o, args);
   System.out.println("事物结束");
   return null;
 }
Пример #8
0
 @Override
 public Object intercept(Object object, Method method, Object[] params, MethodProxy proxy)
     throws Throwable {
   before();
   Object result = proxy.invokeSuper(object, params);
   after();
   return result;
 }
Пример #9
0
 @Override
 // 回调方法
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   System.out.println("事务开始");
   proxy.invokeSuper(obj, args);
   System.out.println("事务结束");
   return null;
 }
  /*
   * (non-Javadoc)
   * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object,
   * java.lang.reflect.Method, java.lang.Object[],
   * net.sf.cglib.proxy.MethodProxy)
   */
  @Override
  public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
      throws Throwable {

    if (Modifier.isAbstract(method.getModifiers())) {
      if (method.isAnnotationPresent(Block.class)) {
        Block anno = method.getAnnotation(Block.class);
        Class<?>[] clazzList = anno.value();
        ObjectBlock objBlock = new ObjectBlock(method, clazzList);
        this.objBlockList.add(objBlock);
      }
      // thực thi hàm getOptionsList trên ConfigModule interface
      if (method.getName().equals("getOptionsList") && method.getParameterTypes().length == 0) {
        checkBlock(method);
        return this.optionsList;
      } else if (method.getName().equals("getTargetClass")
          && method.getParameterTypes().length == 0) {
        checkBlock(method);
        return this.targetClass;
      } else if (method.getName().equals("getConfigInstance")
          && method.getParameterTypes().length == 1) {
        checkBlock(method);
        return this.configObjList.get(args[0]);
      } else {
        if (!this.objBlockList.isEmpty()) {
          ObjectBlock objb = this.objBlockList.get(this.configObjList.size() - 1);
          List<Class<?>> clazzList = null;
          clazzList = objb.getBlockList();
          for (Class<?> clazz : clazzList) {
            Object objConfig = this.configObjList.get(clazz);
            List<Method> methodList = Arrays.asList(objConfig.getClass().getMethods());
            if (methodList.contains(method)) {
              checkBlock(method);
              return method.invoke(objConfig, args);
            } else {
              continue;
            }
          }
        }
        // Nếu không có
        for (Class<?> clazz : this.configObjList.keySet()) {
          Object objConfig = this.configObjList.get(clazz);
          List<Method> methodList = Arrays.asList(ReflectUtils.getAllDeclaredMethods(clazz));
          if (methodList.contains(method)) {
            checkBlock(method);
            return method.invoke(objConfig, args);
          } else {
            continue;
          }
        }
        throw new NoSuchMethodException("Could not found " + method + " method!");
      }
    } else {
      checkBlock(method);
      return proxy.invokeSuper(obj, args);
    }
  }
  /**
   * @param o 代理对象
   * @param method 目标对象方法
   * @param objects 对象方法参数
   * @param methodProxy 目标对象方法代理方法
   * @return
   * @throws Throwable
   */
  @Override
  public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
      throws Throwable {

    before();
    Object result = methodProxy.invokeSuper(o, objects);
    after();
    return result;
  }
Пример #12
0
  public Object doProxyChain() throws Throwable {
    Object methodResult;
    if (proxyIndex < proxyList.size()) {
      methodResult = proxyList.get(proxyIndex++).doProxy(this);
    } else {
      methodResult = methodProxy.invokeSuper(targetBean, methodParams);
    }

    return methodResult;
  }
Пример #13
0
 @Override
 public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   Transaction txn = interceptStart(method);
   try {
     return methodProxy.invokeSuper(object, args);
   } finally {
     interceptComplete(method, txn);
   }
 }
 public Object proceed() throws Throwable {
   try {
     index++;
     return index == interceptors.length
         ? methodProxy.invokeSuper(proxy, arguments)
         : interceptors[index].invoke(this);
   } finally {
     index--;
   }
 }
Пример #15
0
 private Object executeTestStepMethod(
     Object obj, Method method, Object[] args, MethodProxy proxy, Object result) throws Throwable {
   try {
     result = proxy.invokeSuper(obj, args);
     notifyStepFinishedFor(method, args);
   } catch (PendingStepException pendingStep) {
     notifyStepPending(pendingStep.getMessage());
   } catch (IgnoredStepException ignoredStep) {
     notifyStepIgnored(ignoredStep.getMessage());
   }
   return result;
 }
Пример #16
0
 @Override
 public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   String name = method.getName();
   if (name.startsWith("set")) {
     String field = methodToField(name, 3);
     makeChange(field, args[0]);
   } else if (name.startsWith("incr")) {
     makeIncrChange(name, args);
   } else if (name.startsWith("decr")) {
     makeDecrChange(name, args);
   }
   return methodProxy.invokeSuper(object, args);
 }
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   for (int ii = 0; ii < methodsToIntercept.length; ii++) {
     if (MethodUtil.areMethodsEqual(method, methodsToIntercept[ii])) {
       return methodsToIntercept[ii].invoke(delegate, args);
     }
   }
   for (int ii = 0; ii < methodsToDuplicate.length; ii++) {
     if (MethodUtil.areMethodsEqual(method, methodsToDuplicate[ii])) {
       methodsToDuplicate[ii].invoke(delegate, args);
     }
   }
   return proxy.invokeSuper(obj, args);
 }
Пример #18
0
  /*
   * (non-Javadoc)
   *
   * @see com.absir.aop.AopProxyHandler#invoke(java.lang.Object,
   * java.lang.reflect.Method, java.lang.Object[],
   * net.sf.cglib.proxy.MethodProxy)
   */
  @Override
  public Object invoke(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
      throws Throwable {
    if (beanObject == null) {
      return methodProxy.invokeSuper(proxy, args);

    } else {
      if (!method.isAccessible()) {
        method.setAccessible(true);
      }

      return method.invoke(beanObject, args);
    }
  }
 @Override
 public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   EventVO event = interceptStart(method);
   boolean success = true;
   try {
     return methodProxy.invokeSuper(object, args);
   } catch (Exception e) {
     success = false;
     interceptException(method, event);
     throw e;
   } finally {
     if (success) {
       interceptComplete(method, event);
     }
   }
 }
Пример #20
0
 public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   final String methodName = method.getName();
   try {
     synchronized (lazyLoader) {
       if (WRITE_REPLACE_METHOD.equals(methodName)) {
         Object original = null;
         if (constructorArgTypes.isEmpty()) {
           original = objectFactory.create(type);
         } else {
           original = objectFactory.create(type, constructorArgTypes, constructorArgs);
         }
         PropertyCopier.copyBeanProperties(type, enhanced, original);
         if (lazyLoader.size() > 0) {
           return new CglibSerialStateHolder(
               original,
               lazyLoader.getProperties(),
               objectFactory,
               constructorArgTypes,
               constructorArgs);
         } else {
           return original;
         }
       } else {
         if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
           if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
             lazyLoader.loadAll();
           } else if (PropertyNamer.isProperty(methodName)) {
             final String property = PropertyNamer.methodToProperty(methodName);
             if (lazyLoader.hasLoader(property)) {
               lazyLoader.load(property);
             }
           }
         }
       }
     }
     return methodProxy.invokeSuper(enhanced, args);
   } catch (Throwable t) {
     throw ExceptionUtil.unwrapThrowable(t);
   }
 }
Пример #21
0
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
        throws Throwable {

      // We conveniently mock abstract methods be default
      if (Modifier.isAbstract(method.getModifiers())) {
        return handler.invoke(obj, method, args);
      }

      // Here I need to check if the fillInStackTrace was called by EasyMock inner code
      // If it's the case, just ignore the call. We ignore it for two reasons
      // 1- In Java 7, the fillInStackTrace won't work because, since no constructor was called, the
      // stackTrace attribute is null
      // 2- There might be some unexpected side effect in the original fillInStackTrace. So it seems
      // more logical to ignore the call
      if (obj instanceof Throwable && method.getName().equals("fillInStackTrace")) {
        if (isCallerMockInvocationHandlerInvoke(new Throwable())) {
          return obj;
        }
      }

      // Bridges should delegate to their bridged method. It should be done before
      // checking for mocked methods because only unbridged method are mocked
      // It also make sure the method passed to the handler is not the bridge. Normally it
      // shouldn't be necessary because bridges are never mocked so are never in the mockedMethods
      // map. So the normal case is that is will call invokeSuper which will call the interceptor
      // for
      // the bridged method. The problem is that it doesn't happen. It looks like a cglib bug. For
      // package scoped bridges (see GenericTest), the interceptor is not called for the bridged
      // method. Not normal from my point of view.
      if (method.isBridge()) {
        method = BridgeMethodResolver.findBridgedMethod(method);
      }

      if (mockedMethods != null && !mockedMethods.contains(method)) {
        return proxy.invokeSuper(obj, args);
      }

      return handler.invoke(obj, method, args);
    }
Пример #22
0
  @Override
  public Object intercept(
      Object target, Method method, Object[] methodParams, MethodProxy methodProxy)
      throws Throwable {
    // 用户进行判断
    // TODO 再完善一下打印的信息 最好能过滤出类名和方法名出来
    Object result = null;

    //        class com.liusoft.tools.test.AOPSupportBean$$EnhancerByCGLIB$$ef560001test3
    String proxyCalss = target.getClass().getName();

    TimeUnit timeUnit = null;
    try {
      timeUnit = TimeUnit.valueOf(timeUnitName);
    } catch (Exception e) {
    }

    Profiler.enter(proxyCalss.split("\\$\\$")[0] + "." + method.getName(), timeUnit);

    result = methodProxy.invokeSuper(target, methodParams);
    Profiler.release();
    return result;
  }
Пример #23
0
 @Override
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   return proxy.invokeSuper(obj, args);
 }
Пример #24
0
 @Override
 public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy)
     throws Throwable {
   final Object o = super.invoke(enhanced, method, args);
   return (o instanceof AbstractSerialStateHolder) ? o : methodProxy.invokeSuper(o, args);
 }
Пример #25
0
 private Object invokeMethod(
     final Object obj, final Method method, final Object[] args, final MethodProxy proxy)
     throws Throwable {
   return proxy.invokeSuper(obj, args);
 }