@Override public ProxyProxettaFactory proxy() { return new ProxyProxettaFactory(this); } }
/** * {@inheritDoc} */ @Override protected WorkData process(final ClassReader cr, final TargetClassInfoReader targetClassInfoReader) { ProxettaClassBuilder pcb = new ProxettaClassBuilder( destClassWriter, proxetta.getAspects(new ProxyAspect[0]), resolveClassNameSuffix(), requestedProxyClassName, targetClassInfoReader); cr.accept(pcb, 0); return pcb.getWorkData(); }
@Test void testAdviceWithInnerClass() { ProxyAspect proxyAspect = new ProxyAspect(InvalidAdvice.class, methodInfo -> true); try { Proxetta.proxyProxetta().withAspect(proxyAspect).proxy().setTarget(Foo.class).newInstance(); fail("error"); } catch (ProxettaException ignore) { System.out.println(ignore); } } }
builder.setTarget(Object.class); try { builder.define(); fail("Default class loader should not load java.*"); } catch (RuntimeException rex) { builder.setTarget(Object.class); builder.setTargetProxyClassName("foo."); Object object = builder.newInstance(); builder = proxyProxetta.proxy().setTarget(ArrayList.class).setTargetProxyClassName("foo."); List list = (List) builder.newInstance(); assertNotNull(list); assertEquals("foo.ArrayList" + ProxettaNames.proxyClassNameSuffix, list.getClass().getName()); builder = proxyProxetta.proxy().setTarget(HashSet.class).setTargetProxyClassName("foo."); Set set = (Set) builder.newInstance();
new ProxyAspect(ReflectionReplacementAdvice.class, new AllMethodsPointcut())); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Retro.class).setTargetProxyClassName(".Retro2"); Class proxyClass = builder.define();
@Test void testInnerOverride() { ProxyProxetta proxyProxetta = Proxetta.proxyProxetta().withAspect(new ProxyAspect(FooProxyAdvice.class, new AllMethodsPointcut())); ProxyProxettaFactory builder = proxyProxetta.proxy(); builder.setTarget(Two.class); builder.setTargetProxyClassName("foo."); Two two = (Two) builder.newInstance(); assertNotNull(two); assertEquals("foo.Two" + ProxettaNames.proxyClassNameSuffix, two.getClass().getName()); }
@Test void testClassesWithGenericsAsReturnValueProxyDefault() { try { ProxyAspect aspect = ProxyAspect.of(DelegateAdvice.class, new AllMethodsPointcut()); ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspects(aspect); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Foo.class); builder.newInstance(); } catch (Exception ex) { ex.printStackTrace(); fail(ex.toString()); } } }
new ProxyAspect(ReturnNullAdvice.class, new AllMethodsPointcut())); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Retro.class).setTargetProxyClassName(".Retro3"); Class proxyClass = builder.define();
builder.setTarget(Foo.class); Foo foo = (Foo) builder.newInstance(); builder.setTarget(Foo.class); foo = (Foo) builder.newInstance(); builder.setTarget(Foo.class); foo = (Foo) builder.newInstance(); builder = proxyProxetta.proxy().setTarget(Foo.class).setTargetProxyClassName(".Too"); foo = (Foo) builder.newInstance(); builder.setTarget(Foo.class); builder.setTargetProxyClassName("foo."); foo = (Foo) builder.newInstance(); builder.setTargetProxyClassName("foo.Fff"); builder.setTarget(Foo.class); foo = (Foo) builder.newInstance();
@Test void testInterface() { ProxyAspect proxyAspect = new ProxyAspect(FooProxyAdvice.class, methodInfo -> true); try { Proxetta.proxyProxetta().withAspects(proxyAspect).proxy().setTarget(Inter.class).newInstance(); fail("error"); } catch (ProxettaException ignore) { } }
@Override public ProxyProxettaFactory proxy() { return new ProxyProxettaFactory(this); } }
/** * {@inheritDoc} */ @Override protected WorkData process(final ClassReader cr, final TargetClassInfoReader targetClassInfoReader) { ProxettaClassBuilder pcb = new ProxettaClassBuilder( destClassWriter, proxetta.getAspects(new ProxyAspect[0]), resolveClassNameSuffix(), requestedProxyClassName, targetClassInfoReader); cr.accept(pcb, 0); return pcb.getWorkData(); }
/** * Applies advice on given target class and returns proxy instance. */ public static <T> T applyAdvice(final Class<T> targetClass) { Class adviceClass = cache.get(targetClass); if (adviceClass == null) { // advice not yet created adviceClass = PROXY_PROXETTA.proxy().setTarget(targetClass).define(); cache.put(targetClass, adviceClass); } // create new advice instance and injects target instance to it try { Object advice = ClassUtil.newInstance(adviceClass); Field field = adviceClass.getField("$___target$0"); field.set(advice, targetClass); return (T) advice; } catch (Exception ex) { throw new ProxettaException(ex); } }
@Test void testAbstract2() { ProxyAspect proxyAspect = new ProxyAspect(FooProxyAdvice.class, methodInfo -> true); try { Proxetta.proxyProxetta().withAspect(proxyAspect).proxy().setTarget(Abstra2.class).newInstance(); fail("error"); } catch (ProxettaException ignore) { } }
/** * Applies proxetta on bean class before bean registration. */ @Override protected <T> BeanDefinition<T> createBeanDefinitionForRegistration(String name, Class<T> type, Scope scope, WiringMode wiringMode, Consumer<T> consumer) { if (proxetta != null) { ProxyProxettaFactory builder = proxetta.proxy(); builder.setTarget(type); type = builder.define(); } return super.createBeanDefinitionForRegistration(name, type, scope, wiringMode, consumer); }
@Test void testClassesWithGenericsAsReturnValueProxy() { try { ProxyAspect aspect = new ProxyAspect(DelegateAdvice.class, new AllMethodsPointcut()); ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspects(aspect); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Foo.class); builder.newInstance(); } catch (Exception ex) { ex.printStackTrace(); fail(ex.toString()); } }