net.sf.cglib.proxy.FixedValue Java Examples

The following examples show how to use net.sf.cglib.proxy.FixedValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassByImplementationBenchmark.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a benchmark of an interface implementation using cglib.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public ExampleInterface benchmarkCglib() {
    Enhancer enhancer = new Enhancer();
    enhancer.setUseCache(false);
    enhancer.setClassLoader(newClassLoader());
    enhancer.setSuperclass(baseClass);
    CallbackHelper callbackHelper = new CallbackHelper(Object.class, new Class[]{baseClass}) {
        protected Object getCallback(Method method) {
            if (method.getDeclaringClass() == baseClass) {
                return new FixedValue() {
                    public Object loadObject() {
                        return null;
                    }
                };
            } else {
                return NoOp.INSTANCE;
            }
        }
    };
    enhancer.setCallbackFilter(callbackHelper);
    enhancer.setCallbacks(callbackHelper.getCallbacks());
    return (ExampleInterface) enhancer.create();
}
 
Example #2
Source File: ClassUtilsTest.java    From reflection-util with Apache License 2.0 5 votes vote down vote up
private static <T> T createCglibProxy(T object) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(object.getClass());
	enhancer.setCallback((FixedValue) () -> null);
	@SuppressWarnings("unchecked")
	T proxy = (T) enhancer.create();
	return proxy;
}
 
Example #3
Source File: ProxyDetectionTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testCglibProxy() {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Person.class);
    enhancer.setCallback((FixedValue) () -> null);
    Person proxyObject = (Person) enhancer.create();
    assertTrue(ClassUtils.isProxy(proxyObject.getClass()));
}
 
Example #4
Source File: AnnotationUtilTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void cglibProxy() {
    final Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(TestGrandChildClass.class);
    enhancer.setCallback((FixedValue) () -> null);
    final TestGrandChildClass proxy = (TestGrandChildClass) enhancer.create();

    // No declared annotations on proxy.
    assertThat(findDeclared(proxy.getClass(), TestAnnotation.class)).isEmpty();
    // No declared annotations on proxy, so no meta annotations as well.
    assertThat(find(proxy.getClass(), TestMetaAnnotation.class,
                    EnumSet.of(FindOption.LOOKUP_META_ANNOTATIONS))).isEmpty();

    final List<TestAnnotation> list1 = findAll(proxy.getClass(), TestAnnotation.class);
    assertThat(list1).hasSize(3);
    assertThat(list1.get(0).value()).isEqualTo("class-level:TestGrandChildClass");
    assertThat(list1.get(1).value()).isEqualTo("class-level:TestChildClass");
    assertThat(list1.get(2).value()).isEqualTo("class-level:TestClass");

    final List<TestMetaAnnotation> list2 = findAll(proxy.getClass(), TestMetaAnnotation.class);
    assertThat(list2).hasSize(6);
    assertThat(list2.get(0).value()).isEqualTo("TestRepeatables");
    assertThat(list2.get(1).value()).isEqualTo("TestAnnotation");
    assertThat(list2.get(2).value()).isEqualTo("TestRepeatables");
    assertThat(list2.get(3).value()).isEqualTo("TestAnnotation");
    assertThat(list2.get(4).value()).isEqualTo("TestRepeatables");
    assertThat(list2.get(5).value()).isEqualTo("TestAnnotation");
}
 
Example #5
Source File: ProxyIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
    // given
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(PersonService.class);
    enhancer.setCallback((FixedValue) () -> "Hello Tom!");
    PersonService proxy = (PersonService) enhancer.create();

    // when
    String res = proxy.sayHello(null);

    // then
    assertEquals("Hello Tom!", res);
}