java.lang.reflect.InvocationHandler类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(192)

本文整理了Java中java.lang.reflect.InvocationHandler类的一些代码示例,展示了InvocationHandler类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。InvocationHandler类的具体详情如下:
包路径:java.lang.reflect.InvocationHandler
类名称:InvocationHandler

InvocationHandler介绍

[英]InvocationHandler is the interface implemented by the invocation handler of a proxy instance.

Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invokemethod of its invocation handler.
[中]InvocationHandler是由代理实例的调用处理程序实现的接口。
每个代理实例都有一个关联的调用处理程序。在代理实例上调用方法时,方法调用将被编码并发送到其调用处理程序的invokemethod。

代码示例

代码示例来源:origin: javamelody/javamelody

/** {@inheritDoc} */
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      return delegate.invoke(proxy, method, args);
    } catch (final InvocationTargetException e) {
      if (e.getTargetException() != null) {
        throw e.getTargetException();
      }
      throw e;
    }
  }
}

代码示例来源:origin: stackoverflow.com

interface Foo {
  public void bar();
  public void baz();
  public void bat();
}

class FooImpl implements Foo {
  public void bar() {
   //... <-- your logic represented by this notation above
  }

  public void baz() {
   //... <-- your logic represented by this notation above
  }

  // and so forth
}

Foo underlying = new FooImpl();
InvocationHandler handler = new MyInvocationHandler(underlying);
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
   new Class[] { Foo.class },
   handler);

代码示例来源:origin: stackoverflow.com

InvocationHandler bInvocationHandler = new BInvocationHandler() {
  public Object invoke(Object proxy, Method method, Object[] args) {
    // Check this is someMethod
    // a.someMethodOfBIsCalled();
    // b.someMethod();
  }
};

B decoratedB = (B) Proxy.newProxyInstance(B.class.getClassLoader(),
  new Class[] { Foo.class }, bInvocationHandler );

代码示例来源:origin: rapidoid/rapidoid

public static <T> T implement(final Object target, final InvocationHandler handler, Class<?>... interfaces) {
  final Class<?> targetClass = target.getClass();
  return createProxy((proxy, method, args) -> {
    if (method.getDeclaringClass().isAssignableFrom(targetClass)) {
      return method.invoke(target, args);
    }
    return handler.invoke(proxy, method, args);
  }, interfaces);
}

代码示例来源:origin: stackoverflow.com

InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(
  Foo.class.getClassLoader(), new Class[] { Foo.class });
Foo f = (Foo) proxyClass.
  getConstructor(new Class[] { InvocationHandler.class }).
  newInstance(new Object[] { handler });

代码示例来源:origin: org.easymock/easymock

public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
  if (method.isBridge()) {
    method = BridgeMethodResolver.findBridgedMethod(method);
  }
  // Never intercept EasyMock's own calls to fillInStackTrace
  boolean internalFillInStackTraceCall = obj instanceof Throwable
      && method.getName().equals("fillInStackTrace")
      && ClassProxyFactory.isCallerMockInvocationHandlerInvoke(new Throwable());
  if (internalFillInStackTraceCall
      || isMocked(method) && !Modifier.isAbstract(method.getModifiers())) {
    return ProxyBuilder.callSuper(obj, method, args);
  }
  return delegate.invoke(obj, method, args);
}

代码示例来源:origin: stackoverflow.com

InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(
  Foo.class.getClassLoader(), new Class[] { Foo.class });
Foo f = (Foo) proxyClass.
  getConstructor(new Class[] { InvocationHandler.class }).
  newInstance(new Object[] { handler });

代码示例来源:origin: jdbi/jdbi

private static Object invoke(Object target, Method method, Object[] args) {
    if (Proxy.isProxyClass(target.getClass())) {
      InvocationHandler handler = Proxy.getInvocationHandler(target);
      return Unchecked.<Object[], Object>function((params) -> handler.invoke(target, method, params)).apply(args);
    } else {
      MethodHandle handle = Unchecked.function(MethodHandles.lookup()::unreflect).apply(method).bindTo(target);
      return Unchecked.<Object[], Object>function(handle::invokeWithArguments).apply(args);
    }
  }
}

代码示例来源:origin: stackoverflow.com

InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(
  Foo.class.getClassLoader(), new Class[] { Foo.class });
Foo f = (Foo) proxyClass.
  getConstructor(new Class[] { InvocationHandler.class }).
  newInstance(new Object[] { handler });

代码示例来源:origin: org.easymock/easymock

return handler.invoke(obj, method, args);
return handler.invoke(obj, method, args);

代码示例来源:origin: yu199195/myth

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
  if (Object.class.equals(method.getDeclaringClass())) {
    return method.invoke(this, args);
  } else {
    final Myth myth = method.getAnnotation(Myth.class);
    if (Objects.isNull(myth)) {
      return this.delegate.invoke(proxy, method, args);
    }
    try {
      final MythTransactionEngine mythTransactionEngine =
          SpringBeanUtils.getInstance().getBean(MythTransactionEngine.class);
      final MythParticipant participant = buildParticipant(myth, method, args);
      if (Objects.nonNull(participant)) {
        mythTransactionEngine.registerParticipant(participant);
      }
      return this.delegate.invoke(proxy, method, args);
    } catch (Throwable throwable) {
      throwable.printStackTrace();
      return DefaultValueUtils.getDefaultValue(method.getReturnType());
    }
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

@Override
  public void onMessage(final Message message) {
    a1[0] = message;
    try {
      handler.invoke(proxy, m1, a1);
    } catch (Throwable throwable) {
      Jvm.rethrow(throwable);
    }
  }
}

代码示例来源:origin: liuyangming/ByteTCC

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  if (Object.class.equals(method.getDeclaringClass())) {
    return this.delegate.invoke(proxy, method, args);
  } else {
    final SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance();
    CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
    CompensableManager compensableManager = beanFactory.getCompensableManager();
    CompensableTransactionImpl compensable = //
        (CompensableTransactionImpl) compensableManager.getCompensableTransactionQuietly();
    if (compensable == null) {
      return this.delegate.invoke(proxy, method, args);
    }
    final TransactionContext transactionContext = compensable.getTransactionContext();
    if (transactionContext.isCompensable() == false) {
      return this.delegate.invoke(proxy, method, args);
    }
    Method targetMethod = CompensableHystrixInvocationHandler.class.getDeclaredMethod(
        CompensableHystrixBeanPostProcessor.HYSTRIX_INVOKER_NAME,
        new Class<?>[] { CompensableHystrixInvocation.class });
    CompensableHystrixInvocation invocation = new CompensableHystrixInvocation();
    invocation.setThread(Thread.currentThread());
    invocation.setMethod(method);
    invocation.setArgs(args);
    Object[] targetArgs = new Object[] { invocation };
    return this.delegate.invoke(proxy, targetMethod, targetArgs);
  }
}

代码示例来源:origin: jersey/jersey

try {
  Object result = methodHandler.invoke(resource, method, args);

代码示例来源:origin: jersey/jersey

try {
  Object result = methodHandler.invoke(resource, method, args);

代码示例来源:origin: patric-r/jvmtop

.invoke(
  getOperatingSystemMXBean(),
  Class.forName(osMXBeanClassName).getMethod("getProcessCpuTime"),

代码示例来源:origin: yu199195/hmily

final Hmily hmily = method.getAnnotation(Hmily.class);
if (Objects.isNull(hmily)) {
  return this.delegate.invoke(proxy, method, args);
  final Object invoke = delegate.invoke(proxy, method, args);
  final HmilyParticipant hmilyParticipant = buildParticipant(hmily, method, args, hmilyTransactionContext);
  if (hmilyTransactionContext.getRole() == HmilyRoleEnum.INLINE.getCode()) {

代码示例来源:origin: Dreampie/Resty

result = ih.invoke(proxy, method, args);
 for (DataSourceMeta dsm : dataSourceMetas) {
  dsm.commitTransaction();
result = ih.invoke(proxy, method, args);

代码示例来源:origin: liuyangming/ByteTCC

(CompensableTransactionImpl) compensableManager.getCompensableTransactionQuietly();
if (compensable == null) {
  return this.delegate.invoke(proxy, method, args);
  return this.delegate.invoke(proxy, method, args);
  return this.delegate.invoke(proxy, method, args);
} finally {
  Object interceptedValue = response.getHeader(TransactionInterceptor.class.getName());

代码示例来源:origin: com.jayway.awaitility/awaitility

public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    return invocationHandler.invoke(obj, method, args);
  }
};

相关文章

InvocationHandler类方法