Example usage for org.apache.commons.lang3.reflect ProxyUtils newProxyInstance

List of usage examples for org.apache.commons.lang3.reflect ProxyUtils newProxyInstance

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect ProxyUtils newProxyInstance.

Prototype

public static final <T> T newProxyInstance(Class<T> resultType, ClassLoader loader, InvocationHandler h,
        Class<?>... interfaces) 

Source Link

Document

Convenience varargs method for Proxy#newProxyInstance(ClassLoader,Class[],InvocationHandler)

Usage

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImplTest.java

@Test
public void testValidateProxyClass() {
    assertValidationFails("testValidateProxyClass",
            ProxyUtils.newProxyInstance(Appendable.class, ExtendedClassUtils.getDefaultClassLoader(),
                    Mockito.mock(InvocationHandler.class), Appendable.class));
}

From source file:org.springframework.validation.ExtendedValidationUtils.java

/**
 * @param validator The {@link TypedValidator} 
 * @param cl The {@link ClassLoader} to use for the proxy
 * @return A proxy {@link Validator} that delegates the calls to the typed one
 *///from   w  w  w. ja v  a  2  s . com
public static final <E> Validator toValidator(final TypedValidator<E> validator, ClassLoader cl) {
    Assert.notNull(validator, "No validator instance");
    Assert.notNull(cl, "No class loader");

    return ProxyUtils.newProxyInstance(Validator.class, cl, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (SUPPORTS_METHOD_SIGNATURE.evaluate(method)) {
                Class<E> expected = validator.getEntityClass();
                Class<?> actual = (Class<?>) args[0];
                return Boolean.valueOf(expected.isAssignableFrom(actual));
            } else if (VALIDATE_METHOD_SIGNATURE.evaluate(method)) {
                Class<E> expected = validator.getEntityClass();
                validator.validate(expected.cast(args[0]), (Errors) args[1]);
                return null;
            } else {
                return method.invoke(validator, args);
            }
        }
    }, Validator.class);
}