/** * Generates new class. */ public Class defineProxy(final Class target) { ProxyProxettaFactory builder = proxetta.proxy(); builder.setTarget(target); return 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()); }
/** * Generates new class. */ public Class defineProxy(final Class target) { ProxyProxettaFactory builder = proxetta.proxy(); builder.setTarget(target); return builder.define(); }
new ProxyAspect(ReflectionReplacementAdvice.class, new AllMethodsPointcut())); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Retro.class).setTargetProxyClassName(".Retro2");
/** * 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); } }
new ProxyAspect(ReturnNullAdvice.class, new AllMethodsPointcut())); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Retro.class).setTargetProxyClassName(".Retro3");
/** * 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 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()); } } }
@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); } } }
@Test void testCollector() throws NoSuchFieldException, IllegalAccessException { ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspects( new ProxyAspect(CollectorAdvice.class, new AllTopMethodsPointcut()) ); // proxetta.setDebugFolder("d:\\"); Foo foo = (Foo) proxetta.proxy().setTarget(Foo.class).newInstance(); Field field = foo.getClass().getDeclaredField("$__methods$0"); field.setAccessible(true); List<String> list = (List<String>) field.get(foo); assertNotNull(list); assertEquals(0, list.size()); foo.m1(); assertEquals(1, list.size()); assertEquals("m1", list.get(0)); foo.m1(); assertEquals(2, list.size()); assertEquals("m1", list.get(0)); assertEquals("m1", list.get(1)); } }
@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()); } }
@Test void testClassesWithGenericArraysAsReturnValueProxy() { try { ProxyAspect aspect = new ProxyAspect(DelegateAdvice.class, new AllMethodsPointcut()); ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspect(aspect); ProxyProxettaFactory builder = proxetta.proxy().setTarget(Foo.class); builder.newInstance(); } catch (Exception e) { e.printStackTrace(); fail(e.toString()); } } }
@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) { } }
@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) { } }
proxyProxetta.setClassNameSuffix("$$$Proxetta"); ProxyProxettaFactory pb = proxyProxetta.proxy(); pb.setTarget(Foo.class); Foo foo = (Foo) pb.newInstance();
@Test void testProxyInfo_createNotRightAfterTheMethod() { ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspects(aspects()); //proxetta.setDebugFolder(SystemUtil.userHome()); DateDao dateDateProxy = (DateDao) proxetta.proxy().setTarget(DateDao.class).newInstance(); LocalDateTime jDateTime = dateDateProxy.currentTime(); assertNotNull(jDateTime); }
@Test void testAbstract1() { ProxyAspect proxyAspect = new ProxyAspect(FooProxyAdvice.class, methodInfo -> true); Abstra aaa = (Abstra) Proxetta.proxyProxetta().withAspect(proxyAspect).proxy().setTarget(Abstra.class).newInstance(); assertNotNull(aaa); aaa.foo(); }
@Test void testMethRefOnProxifiedClass() { Methref<? extends Oink> m = Methref.on(Oink.class); m.to().woink(); assertEquals("woink", m.ref()); ProxyAspect a1 = new ProxyAspect(DummyAdvice.class, new AllTopMethodsPointcut()); ProxyProxetta pp = Proxetta.proxyProxetta().withAspect(a1); Oink oink = (Oink) pp.proxy().setTarget(Oink.class).newInstance(); assertFalse(oink.getClass().equals(Oink.class)); m = Methref.on(oink.getClass()); m.to().woink(); assertEquals("woink", m.ref()); }
@Test void testProxyProxetta_open() { ProxyAspect proxyAspect = ProxyAspect.of(LogProxyAdvice.class, MethodWithAnnotationPointcut.of(Transaction.class)); Test568OpenService service = (Test568OpenService) Proxetta.proxyProxetta() .withAspect(proxyAspect) .proxy() .setTarget(Test568OpenService.class) .newInstance(); String result = service.findMember(2L); assertEquals("2", result); } }
@Test void testExtendingGenerics() { ProxyAspect aspect = new ProxyAspect(LogAdvice.class, new AllMethodsPointcut()); ProxyProxetta proxetta = Proxetta.proxyProxetta().withAspects(aspect); ProxyProxettaFactory builder = proxetta.proxy().setTarget(MyBoo.class); Boo boo = (Boo) builder.newInstance(); boo.save(new Foo()); } }