@Test public void testInterfaceWrapper() throws Exception { Calc calc = new CalcImpl(); WrapperProxetta proxetta = WrapperProxetta.withAspects( new ProxyAspect( StatCounterAdvice.class, new ProxyPointcutSupport() { public boolean apply(MethodInfo methodInfo) { return isTopLevelMethod(methodInfo) && isPublic(methodInfo); } })); // proxetta.setDebugFolder("d:\\"); // wrapper over INTERFACE // resulting object has ONE interface // only interface methods are wrapped WrapperProxettaBuilder builder = proxetta.builder(Calc.class, ".CalcImpl3"); Class<Calc> calc2Class = builder.define(); Calc calc2 = calc2Class.newInstance(); builder.injectTargetIntoWrapper(calc, calc2); assertEquals(1, StatCounter.counter); // counter in static block !!! calc2.hello(); assertEquals(2, StatCounter.counter); assertEquals(10, calc2.calculate(3, 7)); assertEquals(3, StatCounter.counter); try { calc2Class.getMethod("customMethod"); fail(); } catch (Exception ex) { } }
@Test public void testClassWrapperCastToInterface() throws Exception { Calc calc = new CalcImpl(); WrapperProxetta proxetta = WrapperProxetta.withAspects( new ProxyAspect( StatCounterAdvice.class, new ProxyPointcutSupport() { public boolean apply(MethodInfo methodInfo) { return !isRootMethod(methodInfo) && isPublic(methodInfo); } })); // proxetta.setDebugFolder("d:\\"); // wrapper over CLASS casted to interface, // resulting object has ONE interface // ALL target methods are wrapped WrapperProxettaBuilder builder = proxetta.builder(calc.getClass(), Calc.class, ".CalcImpl2"); Class<Calc> calc2Class = builder.define(); Calc calc2 = calc2Class.newInstance(); builder.injectTargetIntoWrapper(calc, calc2); assertEquals(1, StatCounter.counter); // counter in static block !!! calc2.hello(); assertEquals(2, StatCounter.counter); assertEquals(10, calc2.calculate(3, 7)); assertEquals(3, StatCounter.counter); assertNotNull(calc2Class.getMethod("customMethod")); }