Example usage for java.lang.reflect Proxy newProxyInstance

List of usage examples for java.lang.reflect Proxy newProxyInstance

Introduction

In this page you can find the example usage for java.lang.reflect Proxy newProxyInstance.

Prototype

private static Object newProxyInstance(Class<?> caller, 
            Constructor<?> cons, InvocationHandler h) 

Source Link

Usage

From source file:MyInterface.java

public static void main(String[] argv) throws Exception {
    MyInterface myintf = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(),
            new Class[] { MyInterface.class }, new ProxyClass(new MyInterfaceImpl()));
    myintf.method();/* w w  w.j av a  2 s  .  co m*/
}

From source file:ProxyTest.java

public static void main(String[] args) {
    Object[] elements = new Object[1000];

    // fill elements with proxies for the integers 1 ... 1000
    for (int i = 0; i < elements.length; i++) {
        Integer value = i + 1;//from   www  . jav a 2  s . c o  m
        InvocationHandler handler = new TraceHandler(value);
        Object proxy = Proxy.newProxyInstance(null, new Class[] { Comparable.class }, handler);
        elements[i] = proxy;
    }

    // construct a random integer
    Integer key = new Random().nextInt(elements.length) + 1;

    // search for the key
    int result = Arrays.binarySearch(elements, key);

    // print match if found
    if (result >= 0)
        System.out.println(elements[result]);
}

From source file:com.netpet.spools.javacore.study.proxy.springcase.testProxyClient.java

/**
 * Use the Spring for calling the Proxy Class.
 *
 * @param args/* w  ww . j a  v  a 2 s  .  c  o  m*/
 */
public static void main(String[] args) {
    //SpringDIBean
    ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
    System.out.println(Subject.class.getCanonicalName());
    Subject sub = (Subject) ctx.getBean(Subject.class.getCanonicalName());

    MyHandler handler1 = new MyHandler();
    MyHandler handler2 = new MyHandler();

    handler1.SetSub(sub); //?

    Subject proxySub1 = (Subject) Proxy.newProxyInstance(RealSubject.class.getClassLoader(),
            RealSubject.class.getInterfaces(), handler1);
    Subject proxySub2 = (Subject) Proxy.newProxyInstance(RealSubject.class.getClassLoader(),
            RealSubject.class.getInterfaces(), handler2);

    proxySub1.sailBook();

    proxySub2.sailBook();
}

From source file:com.github.parisoft.resty.response.ResponseFactory.java

public static Response newResponse(HttpResponse httpResponse) {
    return (Response) Proxy.newProxyInstance(Response.class.getClassLoader(), new Class[] { Response.class },
            new ResponseInvocationHandler(httpResponse));
}

From source file:com.palantir.leader.proxy.ToggleableExceptionProxy.java

@SuppressWarnings("unchecked")
public static <T> T newProxyInstance(Class<T> interfaceClass, T delegate, AtomicBoolean throwException,
        Exception exception) {//w  w  w  .j a v  a 2s.  c  o  m
    return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass },
            new ToggleableExceptionProxy(delegate, throwException, exception));
}

From source file:de.anhquan.config4j.ConfigFactory.java

public static <T extends Config> T getConfig(Class<T> configType) {
    T configInst = (T) instances.get(configType);
    if (configInst != null) {
        return configInst;
    }//from   w  w w  . j  av  a2 s  . c  o m

    ClassLoader classLoader = configType.getClassLoader();
    Class[] classes = { configType };
    InvocationHandler handler = new ConfigHandler(configType);
    Object proxy = Proxy.newProxyInstance(classLoader, classes, handler);
    configInst = configType.cast(proxy);
    instances.put(configType, configInst);

    return configInst;
}

From source file:com.link_intersystems.lang.reflect.ThreadLocalProxy.java

@SuppressWarnings("unchecked")
public static <T, TC> T createProxy(ThreadLocal<T> threadLocal, T nullInstance, Class<TC> targetClass) {
    List<Class<?>> allInterfaces = new ArrayList<Class<?>>(ClassUtils.getAllInterfaces(targetClass));
    if (targetClass.isInterface()) {
        allInterfaces.add(targetClass);//from   w w w  .j a  v a2 s. com
    }
    Class<?>[] interfaces = (Class<?>[]) allInterfaces.toArray(new Class<?>[allInterfaces.size()]);
    T proxy = (T) Proxy.newProxyInstance(targetClass.getClassLoader(), interfaces,
            new ThreadLocalProxy(threadLocal, nullInstance));
    return proxy;
}

From source file:org.apache.nifi.authorization.UserGroupProviderFactory.java

public static UserGroupProvider withNarLoader(final UserGroupProvider baseUserGroupProvider,
        final ClassLoader classLoader) {
    final UserGroupProviderInvocationHandler invocationHandler = new UserGroupProviderInvocationHandler(
            baseUserGroupProvider, classLoader);

    // extract all interfaces... baseUserGroupProvider is non null so getAllInterfaces is non null
    final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(baseUserGroupProvider.getClass());
    final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]);

    return (UserGroupProvider) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
}

From source file:com.sina.cloudstorage.http.conn.ClientConnectionManagerFactory.java

/**
 * Returns a wrapped instance of {@link ClientConnectionManager}
 * to capture the necessary performance metrics.
 * @param orig the target instance to be wrapped
 *///from  w  ww.j a  v a  2  s  . c  o  m
public static ClientConnectionManager wrap(ClientConnectionManager orig) {
    if (orig instanceof Wrapped)
        throw new IllegalArgumentException();
    return (ClientConnectionManager) Proxy.newProxyInstance(
            // https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423
            ClientConnectionManagerFactory.class.getClassLoader(), interfaces, new Handler(orig));
}

From source file:com.ksc.http.conn.ClientConnectionRequestFactory.java

/**
 * Returns a wrapped instance of {@link ConnectionRequest}
 * to capture the necessary performance metrics.
 * @param orig the target instance to be wrapped
 *//*from   w ww  . j a  va  2s.com*/
static ConnectionRequest wrap(ConnectionRequest orig) {
    if (orig instanceof Wrapped)
        throw new IllegalArgumentException();
    return (ConnectionRequest) Proxy.newProxyInstance(
            // https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423
            ClientConnectionRequestFactory.class.getClassLoader(), interfaces, new Handler(orig));
}