예제 #1
0
  public void testInvokeExactMethod() throws Exception {
    assertEquals(
        "foo()",
        MethodUtils.invokeMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
    assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) null));
    assertEquals("foo(String)", MethodUtils.invokeExactMethod(testBean, "foo", ""));
    assertEquals("foo(Object)", MethodUtils.invokeExactMethod(testBean, "foo", new Object()));
    assertEquals(
        "foo(Integer)", MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.INTEGER_ONE));
    assertEquals(
        "foo(double)",
        MethodUtils.invokeExactMethod(
            testBean, "foo", new Object[] {NumberUtils.DOUBLE_ONE}, new Class[] {Double.TYPE}));

    try {
      MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.BYTE_ONE);
      fail("should throw NoSuchMethodException");
    } catch (NoSuchMethodException e) {
    }
    try {
      MethodUtils.invokeExactMethod(testBean, "foo", NumberUtils.LONG_ONE);
      fail("should throw NoSuchMethodException");
    } catch (NoSuchMethodException e) {
    }
    try {
      MethodUtils.invokeExactMethod(testBean, "foo", Boolean.TRUE);
      fail("should throw NoSuchMethodException");
    } catch (NoSuchMethodException e) {
    }
  }
예제 #2
0
 private void expectMatchingAccessibleMethodParameterTypes(
     Class<?> cls, String methodName, Class<?>[] requestTypes, Class<?>[] actualTypes) {
   Method m = MethodUtils.getMatchingAccessibleMethod(cls, methodName, requestTypes);
   assertTrue(
       toString(m.getParameterTypes()) + " not equals " + toString(actualTypes),
       Arrays.equals(actualTypes, m.getParameterTypes()));
 }
예제 #3
0
 public void testGetAccessiblePublicMethodFromDescription() throws Exception {
   assertSame(
       MutableObject.class,
       MethodUtils.getAccessibleMethod(
               MutableObject.class, "getValue", ArrayUtils.EMPTY_CLASS_ARRAY)
           .getDeclaringClass());
 }
예제 #4
0
 public void testGetAccessibleInterfaceMethodFromDescription() throws Exception {
   Class<?>[][] p = {ArrayUtils.EMPTY_CLASS_ARRAY, null};
   for (int i = 0; i < p.length; i++) {
     Method accessibleMethod =
         MethodUtils.getAccessibleMethod(TestMutable.class, "getValue", p[i]);
     assertSame(Mutable.class, accessibleMethod.getDeclaringClass());
   }
 }
예제 #5
0
 public void testInvokeMethod() throws Exception {
   assertEquals(
       "foo()",
       MethodUtils.invokeMethod(testBean, "foo", (Object[]) ArrayUtils.EMPTY_CLASS_ARRAY));
   assertEquals("foo()", MethodUtils.invokeMethod(testBean, "foo", (Object[]) null));
   assertEquals("foo(String)", MethodUtils.invokeMethod(testBean, "foo", ""));
   assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", new Object()));
   assertEquals("foo(Object)", MethodUtils.invokeMethod(testBean, "foo", Boolean.TRUE));
   assertEquals(
       "foo(Integer)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.INTEGER_ONE));
   assertEquals("foo(int)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.BYTE_ONE));
   assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.LONG_ONE));
   assertEquals("foo(double)", MethodUtils.invokeMethod(testBean, "foo", NumberUtils.DOUBLE_ONE));
 }
  public Object invoke(Method method, Object[] args) throws Throwable {

    final String methodName = method.getName();

    if (!METHODS_TO_INTERCEPT.contains(methodName)) {
      return MethodUtils.proceedExecution(method, stmt, args);
    }

    // special treat for toString method
    if ("toString".equals(methodName)) {
      final StringBuilder sb = new StringBuilder();
      sb.append(stmt.getClass().getSimpleName());
      sb.append(" [");
      sb.append(stmt.toString());
      sb.append("]");
      return sb.toString(); // differentiate toString message.
    } else if ("getDataSourceName".equals(methodName)) {
      return dataSourceName;
    } else if ("getTarget".equals(methodName)) {
      // ProxyJdbcObject interface has method to return original object.
      return stmt;
    }

    if (StatementMethodNames.JDBC4_METHODS.contains(methodName)) {
      final Class<?> clazz = (Class<?>) args[0];
      if ("unwrap".equals(methodName)) {
        return stmt.unwrap(clazz);
      } else if ("isWrapperFor".equals(methodName)) {
        return stmt.isWrapperFor(clazz);
      }
    }

    if (StatementMethodNames.GET_CONNECTION_METHOD.contains(methodName)) {
      return connectionProxy;
    }

    if ("addBatch".equals(methodName) || "clearBatch".equals(methodName)) {
      if ("addBatch".equals(methodName) && ObjectArrayUtils.isFirstArgString(args)) {
        final QueryTransformer queryTransformer = interceptorHolder.getQueryTransformer();
        final String query = (String) args[0];
        final Class<? extends Statement> clazz = Statement.class;
        final int batchCount = batchQueries.size();
        final TransformInfo transformInfo =
            new TransformInfo(clazz, dataSourceName, query, true, batchCount);
        final String transformedQuery = queryTransformer.transformQuery(transformInfo);
        args[0] = transformedQuery; // replace to the new query
        batchQueries.add(transformedQuery);
      } else if ("clearBatch".equals(methodName)) {
        batchQueries.clear();
      }

      // proceed execution, no need to call listener
      try {
        return method.invoke(stmt, args);
      } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
      }
    }

    List<QueryInfo> queries;
    boolean isBatchExecute = false;
    int batchSize = 0;

    if (StatementMethodNames.BATCH_EXEC_METHODS.contains(methodName)) {

      queries = new ArrayList<QueryInfo>(batchQueries.size());
      for (String batchQuery : batchQueries) {
        queries.add(new QueryInfo(batchQuery));
      }
      batchSize = batchQueries.size();
      batchQueries.clear();
      isBatchExecute = true;
      queries = Collections.unmodifiableList(queries);
    } else if (StatementMethodNames.QUERY_EXEC_METHODS.contains(methodName)
        && ObjectArrayUtils.isFirstArgString(args)) {
      final QueryTransformer queryTransformer = interceptorHolder.getQueryTransformer();
      final String query = (String) args[0];
      final TransformInfo transformInfo =
          new TransformInfo(Statement.class, dataSourceName, query, false, 0);
      final String transformedQuery = queryTransformer.transformQuery(transformInfo);
      args[0] = transformedQuery; // replace to the new query
      queries = Collections.singletonList(new QueryInfo(transformedQuery, null));
    } else {
      queries = Collections.emptyList();
    }

    final QueryExecutionListener listener = interceptorHolder.getListener();
    final ExecutionInfoBuilder execInfoBuilder =
        ExecutionInfoBuilder.create()
            .dataSourceName(dataSourceName)
            .batch(isBatchExecute)
            .batchSize(batchSize)
            .method(method)
            .methodArgs(args)
            .statement(stmt);
    listener.beforeQuery(execInfoBuilder.build(), queries);

    // Invoke method on original Statement.
    try {
      final TimeProvider timeProvider = connectionProxy.getTimeProvider();
      final long beforeTime = timeProvider.getCurrentTime();
      Object retVal;
      try {
        retVal = method.invoke(stmt, args);
      } finally {
        final long afterTime = timeProvider.getCurrentTime();
        execInfoBuilder.elapsedTime(afterTime - beforeTime, timeProvider.getTimeUnit());
      }

      execInfoBuilder.result(retVal);
      execInfoBuilder.success(true);

      return retVal;
    } catch (InvocationTargetException ex) {
      execInfoBuilder.throwable(ex.getTargetException());
      execInfoBuilder.success(false);
      throw ex.getTargetException();
    } finally {
      listener.afterQuery(execInfoBuilder.build(), queries);
    }
  }
예제 #7
0
 /*
  * (non-Javadoc)
  *
  * @see
  * org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom
  * .MethodDeclaration)
  */
 @Override
 public boolean visit(final MethodDeclaration node) {
   final String methodType = MethodUtils.getMethodType(node);
   methodsForClasses.put(className.peek(), node.getName().getIdentifier() + ":" + methodType);
   return false;
 }
예제 #8
0
  private static void setProperty(Object object, String name, Object value)
      throws InvocationTargetException {
    try {
      if (name.contains("[")) {
        setIndexedProperty(object, name, value);
      } else {
        if (object instanceof Map<?, ?>) {
          @SuppressWarnings("unchecked")
          Map<String, Object> map = (Map<String, Object>) object;
          map.put(name, value);
        } else {
          BeanDescriptor cd = getBeanDescriptor(object.getClass());
          Method method = cd.getSetter(name);

          if (method == null) {
            throw new NoSuchMethodException(
                "No SET method for property "
                    + name
                    + " on instance of "
                    + object.getClass().getName());
          }

          Object[] params = new Object[] {value};

          try {
            method.invoke(object, params);
          } catch (Throwable t) {
            throw unwrapThrowable(t);
          }
        }
      }
    } catch (InvocationTargetException e) {
      throw e;
    } catch (Throwable t) {
      try {
        if (value != null) {
          MethodUtils.invokeSetter(object, name, value);
          return;
        }
      } catch (Throwable tt) {
        // ignore
      }

      if (object == null) {
        throw new InvocationTargetException(
            t,
            "Could not set property '"
                + name
                + "' to value '"
                + value
                + "' for null reference. Cause: "
                + t.toString());
      } else {
        throw new InvocationTargetException(
            t,
            "Could not set property '"
                + name
                + "' to value '"
                + value
                + "' for "
                + object.getClass().getName()
                + ". Cause: "
                + t.toString());
      }
    }
  }