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

x33g5p2x  于2022-01-25 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(140)

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

Proxy介绍

[英]Proxy defines methods for creating dynamic proxy classes and instances. A proxy class implements a declared set of interfaces and delegates method invocations to an InvocationHandler.
[中]Proxy定义了创建动态代理类和实例的方法。代理类实现一组声明的接口,并将方法调用委托给InvocationHandler。

代码示例

代码示例来源:origin: google/guava

private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
 Object object =
   Proxy.newProxyInstance(
     interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
 return interfaceType.cast(object);
}

代码示例来源:origin: spring-projects/spring-framework

private boolean isProxyForSameBshObject(Object other) {
    if (!Proxy.isProxyClass(other.getClass())) {
      return false;
    }
    InvocationHandler ih = Proxy.getInvocationHandler(other);
    return (ih instanceof BshObjectInvocationHandler &&
        this.xt.equals(((BshObjectInvocationHandler) ih).xt));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a composite interface Class for the given interfaces,
 * implementing the given interfaces in one single Class.
 * <p>This implementation builds a JDK proxy class for the given interfaces.
 * @param interfaces the interfaces to merge
 * @param classLoader the ClassLoader to create the composite Class in
 * @return the merged interface as Class
 * @throws IllegalArgumentException if the specified interfaces expose
 * conflicting method signatures (or a similar constraint is violated)
 * @see java.lang.reflect.Proxy#getProxyClass
 */
@SuppressWarnings("deprecation")
public static Class<?> createCompositeInterface(Class<?>[] interfaces, @Nullable ClassLoader classLoader) {
  Assert.notEmpty(interfaces, "Interfaces must not be empty");
  return Proxy.getProxyClass(classLoader, interfaces);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check whether the given object is a JDK dynamic proxy.
 * <p>This method goes beyond the implementation of
 * {@link Proxy#isProxyClass(Class)} by additionally checking if the
 * given object is an instance of {@link SpringProxy}.
 * @param object the object to check
 * @see java.lang.reflect.Proxy#isProxyClass
 */
public static boolean isJdkDynamicProxy(@Nullable Object object) {
  return (object instanceof SpringProxy && Proxy.isProxyClass(object.getClass()));
}

代码示例来源:origin: square/okhttp

@Override public @Nullable String getSelectedProtocol(SSLSocket socket) {
 try {
  AlpnProvider provider =
    (AlpnProvider) Proxy.getInvocationHandler(getMethod.invoke(null, socket));
  if (!provider.unsupported && provider.selected == null) {
   Platform.get().log(INFO, "ALPN callback dropped: HTTP/2 is disabled. "
     + "Is alpn-boot on the boot class path?", null);
   return null;
  }
  return provider.unsupported ? null : provider.selected;
 } catch (InvocationTargetException | IllegalAccessException e) {
  throw new AssertionError("failed to get ALPN selected protocol", e);
 }
}

代码示例来源:origin: google/guava

private static boolean isProxyOfSameInterfaces(Object arg, Class<?> proxyClass) {
  return proxyClass.isInstance(arg)
    // Equal proxy instances should mostly be instance of proxyClass
    // Under some edge cases (such as the proxy of JDK types serialized and then deserialized)
    // the proxy type may not be the same.
    // We first check isProxyClass() so that the common case of comparing with non-proxy objects
    // is efficient.
    || (Proxy.isProxyClass(arg.getClass())
      && Arrays.equals(arg.getClass().getInterfaces(), proxyClass.getInterfaces()));
 }
}

代码示例来源:origin: prestodb/presto

@Override public String getSelectedProtocol(SSLSocket socket) {
 try {
  JettyNegoProvider provider =
    (JettyNegoProvider) Proxy.getInvocationHandler(getMethod.invoke(null, socket));
  if (!provider.unsupported && provider.selected == null) {
   Platform.get().log(INFO, "ALPN callback dropped: HTTP/2 is disabled. "
     + "Is alpn-boot on the boot class path?", null);
   return null;
  }
  return provider.unsupported ? null : provider.selected;
 } catch (InvocationTargetException | IllegalAccessException e) {
  throw assertionError("unable to get selected protocol", e);
 }
}

代码示例来源:origin: jenkinsci/jenkins

public final <T> T wrap(Class<T> type, final T object) {
    return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, new InvocationHandler() {
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
          return call(object,method,args);
        } catch (InvocationTargetException e) {
          throw e.getTargetException();
        }
      }
    }));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.
 * <p>This method additionally checks if the given object is an instance
 * of {@link SpringProxy}.
 * @param object the object to check
 * @see #isJdkDynamicProxy
 * @see #isCglibProxy
 */
public static boolean isAopProxy(@Nullable Object object) {
  return (object instanceof SpringProxy &&
      (Proxy.isProxyClass(object.getClass()) || ClassUtils.isCglibProxyClass(object.getClass())));
}

代码示例来源:origin: org.springframework/spring-context

private boolean isProxyForSameBshObject(Object other) {
    if (!Proxy.isProxyClass(other.getClass())) {
      return false;
    }
    InvocationHandler ih = Proxy.getInvocationHandler(other);
    return (ih instanceof BshObjectInvocationHandler &&
        this.xt.equals(((BshObjectInvocationHandler) ih).xt));
  }
}

代码示例来源:origin: google/guava

public void testToString() {
 List<String> proxy = newDelegatingList(LIST1);
 assertEquals(Proxy.getInvocationHandler(proxy).toString(), proxy.toString());
}

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

@Override
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
  List<Class<?>> loadedClasses = new ArrayList<Class<?>>(interfaces.length);
  
  for (String name : interfaces) {
    Class<?> clazz = Class.forName(name, false, classLoader);
    loadedClasses.add(clazz);
  }
  
  return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[loadedClasses.size()]));
}

代码示例来源:origin: prestodb/presto

private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
 Object object =
   Proxy.newProxyInstance(
     interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
 return interfaceType.cast(object);
}

代码示例来源:origin: spring-projects/spring-framework

public boolean hasProxyTarget() {
  return (this.target != null && Proxy.isProxyClass(this.target.getType()));
}

代码示例来源:origin: hibernate/hibernate-orm

private boolean needsWrapping(Session session) {
  // try to make sure we don't wrap and already wrapped session
  if ( Proxy.isProxyClass( session.getClass() ) ) {
    final InvocationHandler invocationHandler = Proxy.getInvocationHandler( session );
    if ( invocationHandler != null && TransactionProtectionWrapper.class.isInstance( invocationHandler ) ) {
      return false;
    }
  }
  return true;
}

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

public static void stop( Object subprocess )
{
  ( (Handler) Proxy.getInvocationHandler( subprocess ) ).stop( null, 0 );
}

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

@Override
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
  List<Class<?>> loadedClasses = new ArrayList<Class<?>>(interfaces.length);
  
  for (String name : interfaces) {
    Class<?> clazz = Class.forName(name, false, classLoader);
    loadedClasses.add(clazz);
  }
  
  return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[loadedClasses.size()]));
}

代码示例来源:origin: google/guava

/**
 * Returns a proxy instance that implements {@code interfaceType} by dispatching method
 * invocations to {@code handler}. The class loader of {@code interfaceType} will be used to
 * define the proxy class. To implement multiple interfaces or specify a class loader, use {@link
 * Proxy#newProxyInstance}.
 *
 * @throws IllegalArgumentException if {@code interfaceType} does not specify the type of a Java
 *     interface
 */
public static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
 checkNotNull(handler);
 checkArgument(interfaceType.isInterface(), "%s is not an interface", interfaceType);
 Object object =
   Proxy.newProxyInstance(
     interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
 return interfaceType.cast(object);
}

代码示例来源:origin: prestodb/presto

private static boolean isProxyOfSameInterfaces(Object arg, Class<?> proxyClass) {
  return proxyClass.isInstance(arg)
    // Equal proxy instances should mostly be instance of proxyClass
    // Under some edge cases (such as the proxy of JDK types serialized and then deserialized)
    // the proxy type may not be the same.
    // We first check isProxyClass() so that the common case of comparing with non-proxy objects
    // is efficient.
    || (Proxy.isProxyClass(arg.getClass())
      && Arrays.equals(arg.getClass().getInterfaces(), proxyClass.getInterfaces()));
 }
}

代码示例来源:origin: spring-projects/spring-framework

otherProxy = (JdkDynamicAopProxy) other;
else if (Proxy.isProxyClass(other.getClass())) {
  InvocationHandler ih = Proxy.getInvocationHandler(other);
  if (!(ih instanceof JdkDynamicAopProxy)) {
    return false;

相关文章