Package com.esotericsoftware.reflectasm

Examples of com.esotericsoftware.reflectasm.MethodAccess


public class MethodAccessBenchmark extends Benchmark {
  public MethodAccessBenchmark () throws Exception {
    int count = 100000;
    Object[] dontCompileMeAway = new Object[count];

    MethodAccess access = MethodAccess.get(SomeClass.class);
    SomeClass someObject = new SomeClass();
    int index = access.getIndex("getName");

    Method method = SomeClass.class.getMethod("getName");

    for (int i = 0; i < 100; i++) {
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = access.invoke(someObject, index);
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = method.invoke(someObject);
    }
    warmup = false;

    for (int i = 0; i < 100; i++) {
      start();
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = access.invoke(someObject, index);
      end("MethodAccess");
    }
    for (int i = 0; i < 100; i++) {
      start();
      for (int ii = 0; ii < count; ii++)
View Full Code Here


  public MethodAccessBenchmark () throws Exception {
    int count = 100000;
    Object[] dontCompileMeAway = new Object[count];
    Object[] args = new Object[0];

    MethodAccess access = MethodAccess.get(SomeClass.class);
    SomeClass someObject = new SomeClass();
    int index = access.getIndex("getName");

    Method method = SomeClass.class.getMethod("getName");
    // method.setAccessible(true); // Improves reflection a bit.

    for (int i = 0; i < 100; i++) {
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = access.invoke(someObject, index, args);
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = method.invoke(someObject, args);
    }
    warmup = false;

    for (int i = 0; i < 100; i++) {
      start();
      for (int ii = 0; ii < count; ii++)
        dontCompileMeAway[ii] = access.invoke(someObject, index, args);
      end("MethodAccess");
    }
    for (int i = 0; i < 100; i++) {
      start();
      for (int ii = 0; ii < count; ii++)
View Full Code Here

          .getProviderByVersion(version);
      Class<?> processorClass = serviceProvider.getProcessorClass();
      Object processor = processorClass.newInstance();
      Object[] args = request.getArgs();

      MethodAccess method = serviceProvider.getMethodAccess();
      int methodIndex = method.getIndex(methodName,request.getArgsTypes());
      result = method.invoke(processor, methodIndex, args);
    } catch (InstantiationException e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      logger.error(e.getMessage(), e);
View Full Code Here

     * @return 调用指定的方法的返回值如果接口方法的声明返回类型是基本类型,则此值一定
     * 是相应基本包装对象类的实例;否则,它一定是可分配到声明返回类型的类型。如果此值为 null则
     * 接口方法的返回类型是void或者接口方法返回了null
     */
    public static final Object invokeMethod(Object host, String methodName, Object... args) {
      MethodAccess access = getMethodAccess(host.getClass());
      if (args != null && args.length != 0) {
        Class<?>[] clazz = new Class<?>[args.length];
        for (int i = 0; i < args.length; ++i) {
          clazz[i] = args[i].getClass();
        }
        return access.invoke(host, access.getIndex(methodName, clazz), args);
      }
        return access.invoke(host, methodName, args);
    }
View Full Code Here

  /**
   * Test method for {@link org.apache.niolex.commons.reflect.FastMethodUtil#getMethodAccess(java.lang.Class)}.
   */
  @Test
  public void testGetMethodAccess() {
    MethodAccess ac = FastMethodUtil.getMethodAccess(MethodTestBean.class);
    MethodTestBean host = new MethodTestBean("niolex-common-utils");
    String name = (String)ac.invoke(host, "echoName");
    assertEquals(name, "niolex-common-utils");
  }
View Full Code Here

     * @param clazz 需要获取的类
     * @return 所有方法数组
     * @throws SecurityException 如果设置了安全检查并拒绝对这个类使用反射
     */
    public static final String[] getMethods(Class<?> clazz) {
      MethodAccess access = MethodAccess.get(clazz);

        return access.getMethodNames();
    }
View Full Code Here

     * @return 调用指定的方法的返回值如果接口方法的声明返回类型是基本类型,则此值一定
     * 是相应基本包装对象类的实例;否则,它一定是可分配到声明返回类型的类型。如果此值为 null则
     * 接口方法的返回类型是void或者接口方法返回了null
     */
    public static final Object invokeMethod(Object host, String methodName, Object... args) {
      MethodAccess access = MethodAccess.get(host.getClass());
      if (args != null && args.length != 0) {
        Class<?>[] clazz = new Class<?>[args.length];
        for (int i = 0; i < args.length; ++i) {
          clazz[i] = args[i].getClass();
        }
        return access.invoke(host, access.getIndex(methodName, clazz), args);
      }
        return access.invoke(host, methodName, args);
    }
View Full Code Here

TOP

Related Classes of com.esotericsoftware.reflectasm.MethodAccess

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.