/** * 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}) { @Override protected Object getCallback(Method method) { if (method.getDeclaringClass() == baseClass) { return new FixedValue() { @Override public Object loadObject() throws Exception { return null; } }; } else { return NoOp.INSTANCE; } } }; enhancer.setCallbackFilter(callbackHelper); enhancer.setCallbacks(callbackHelper.getCallbacks()); return (ExampleInterface) enhancer.create(); }
private <T> T createProxy(final Class<T> clazz) { if (invocationsMap == null) { invocationsMap = new HashMap<>(); } try { final FeatureResolver resolver = FeatureResolver.newFeatureResolver(clazz).build(); final TestInvocation invocation = new TestInvocationImpl(clazz, resolver); invocationsMap.put(clazz, invocation); executor.processBeforeAll(invocation); } catch (final Exception e) { throw new JpaUnitException("Could not execute beforeAll hook", e); } final T bean = createBean(clazz); final CallbackHelper helper = new CallbackHelper(clazz, new Class[0]) { @Override protected Object getCallback(final Method method) { if (hasCucumberAnnotations(method)) { return new CucumberInterceptor(executor, bean); } else if (method.getName().equals("equals")) { return new EqualsInterceptor(bean); } else { return (Dispatcher) () -> bean; } } }; return clazz.cast(Enhancer.create(clazz, new Class[0], helper, helper.getCallbacks())); }
private Object createProxy(final Object bean) { final CallbackHelper helper = new CallbackHelper(bean.getClass(), new Class[0]) { @Override protected Object getCallback(final Method method) { if (method.getName().equals("equals")) { return new EqualsInterceptor(bean); } else { return new ConcordionInterceptor(executor, bean); } } }; return Enhancer.create(bean.getClass(), new Class[0], helper, helper.getCallbacks()); }