1 16 17 package org.springframework.aop.framework; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Proxy ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 import javax.servlet.ServletException ; 25 26 import junit.framework.TestCase; 27 import org.aopalliance.aop.Advice; 28 import org.aopalliance.intercept.MethodInterceptor; 29 import org.aopalliance.intercept.MethodInvocation; 30 31 import org.springframework.aop.ClassFilter; 32 import org.springframework.aop.IntroductionAdvisor; 33 import org.springframework.aop.IntroductionInterceptor; 34 import org.springframework.aop.framework.adapter.ThrowsAdviceInterceptorTests; 35 import org.springframework.aop.interceptor.DebugInterceptor; 36 import org.springframework.aop.interceptor.NopInterceptor; 37 import org.springframework.aop.interceptor.SideEffectBean; 38 import org.springframework.aop.support.AopUtils; 39 import org.springframework.aop.support.DefaultIntroductionAdvisor; 40 import org.springframework.aop.support.DynamicMethodMatcherPointcutAdvisor; 41 import org.springframework.beans.ITestBean; 42 import org.springframework.beans.Person; 43 import org.springframework.beans.TestBean; 44 import org.springframework.beans.factory.BeanCreationException; 45 import org.springframework.beans.factory.BeanFactory; 46 import org.springframework.beans.factory.FactoryBean; 47 import org.springframework.beans.factory.ListableBeanFactory; 48 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 49 import org.springframework.beans.factory.support.RootBeanDefinition; 50 import org.springframework.beans.factory.xml.XmlBeanFactory; 51 import org.springframework.context.ApplicationListener; 52 import org.springframework.context.event.ConsoleListener; 53 import org.springframework.core.io.ClassPathResource; 54 import org.springframework.util.SerializationTestUtils; 55 56 63 public class ProxyFactoryBeanTests extends TestCase { 64 65 private BeanFactory factory; 66 67 protected void setUp() throws Exception { 68 DefaultListableBeanFactory parent = new DefaultListableBeanFactory(); 69 parent.registerBeanDefinition("target2", new RootBeanDefinition(ConsoleListener.class)); 70 this.factory = new XmlBeanFactory(new ClassPathResource("proxyFactoryTests.xml", getClass()), parent); 71 } 72 73 public void testIsDynamicProxy() { 74 ITestBean test1 = (ITestBean) factory.getBean("test1"); 75 assertTrue("test1 is a dynamic proxy", Proxy.isProxyClass(test1.getClass())); 76 } 77 78 82 public void testDoubleTargetSourcesAreRejected() { 83 testDoubleTargetSourceIsRejected("doubleTarget"); 84 testDoubleTargetSourceIsRejected("arbitraryTarget"); 86 } 87 88 private void testDoubleTargetSourceIsRejected(String name) { 89 try { 90 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryDoubleTargetSourceTests.xml", getClass())); 91 ITestBean tb = (ITestBean) bf.getBean(name); 92 fail("Should not allow TargetSource to be specified in interceptorNames as well as targetSource property"); 94 } 95 catch (BeanCreationException ex) { 96 AopConfigException aex = (AopConfigException) ex.getCause(); 98 assertTrue(aex.getMessage().indexOf("TargetSource") != -1); 99 } 100 } 101 102 public void testTargetSourceNotAtEndOfInterceptorNamesIsRejected() { 103 try { 104 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTargetSourceNotLastTests.xml", getClass())); 105 bf.getBean("targetSourceNotLast"); 106 fail("TargetSource or non-advised object must be last in interceptorNames"); 107 } 108 catch (BeanCreationException ex) { 109 AopConfigException aex = (AopConfigException) ex.getCause(); 111 assertTrue(aex.getMessage().indexOf("interceptorNames") != -1); 112 } 113 } 114 115 public void testGetObjectTypeWithDirectTarget() { 116 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTargetSourceTests.xml", getClass())); 117 118 CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice"); 120 assertEquals(0, cba.getCalls()); 121 122 ITestBean tb = (ITestBean) bf.getBean("directTarget"); 123 assertTrue(tb.getName().equals("Adam")); 124 assertEquals(1, cba.getCalls()); 125 126 ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget"); 127 assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType())); 128 } 129 130 public void testGetObjectTypeWithTargetViaTargetSource() { 131 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTargetSourceTests.xml", getClass())); 132 ITestBean tb = (ITestBean) bf.getBean("viaTargetSource"); 133 assertTrue(tb.getName().equals("Adam")); 134 ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&viaTargetSource"); 135 assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType())); 136 } 137 138 public void testGetObjectTypeWithNoTargetOrTargetSource() { 139 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTargetSourceTests.xml", getClass())); 140 141 ITestBean tb = (ITestBean) bf.getBean("noTarget"); 142 try { 143 tb.getName(); 144 fail(); 145 } 146 catch (UnsupportedOperationException ex) { 147 assertEquals("getName", ex.getMessage()); 148 } 149 FactoryBean pfb = (ProxyFactoryBean) bf.getBean("&noTarget"); 150 assertTrue(ITestBean.class.isAssignableFrom(pfb.getObjectType())); 151 } 152 153 157 public void testSingletonInstancesAreEqual() { 158 ITestBean test1 = (ITestBean) factory.getBean("test1"); 159 ITestBean test1_1 = (ITestBean) factory.getBean("test1"); 160 assertEquals("Singleton instances ==", test1, test1_1); 162 test1.setAge(25); 163 assertEquals(test1.getAge(), test1_1.getAge()); 164 test1.setAge(250); 165 assertEquals(test1.getAge(), test1_1.getAge()); 166 Advised pc1 = (Advised) test1; 167 Advised pc2 = (Advised) test1_1; 168 assertEquals(pc1.getAdvisors(), pc2.getAdvisors()); 169 int oldLength = pc1.getAdvisors().length; 170 NopInterceptor di = new NopInterceptor(); 171 pc1.addAdvice(1, di); 172 assertEquals(pc1.getAdvisors(), pc2.getAdvisors()); 173 assertEquals("Now have one more advisor", oldLength + 1, pc2.getAdvisors().length); 174 assertEquals(di.getCount(), 0); 175 test1.setAge(5); 176 assertEquals(test1_1.getAge(), test1.getAge()); 177 assertEquals(di.getCount(), 3); 178 } 179 180 181 public void testPrototypeInstancesAreNotEqual() { 182 ITestBean test2 = (ITestBean) factory.getBean("prototype"); 183 ITestBean test2_1 = (ITestBean) factory.getBean("prototype"); 184 assertTrue("Prototype instances !=", test2 != test2_1); 185 assertTrue("Prototype instances equal", test2.equals(test2_1)); 186 } 187 188 189 194 private Object testPrototypeInstancesAreIndependent(String beanName) { 195 int INITIAL_COUNT = 10; 197 198 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("prototypeTests.xml", getClass())); 199 200 SideEffectBean raw = (SideEffectBean) bf.getBean("prototypeTarget"); 202 assertEquals(INITIAL_COUNT, raw.getCount() ); 203 raw.doWork(); 204 assertEquals(INITIAL_COUNT+1, raw.getCount() ); 205 raw = (SideEffectBean) bf.getBean("prototypeTarget"); 206 assertEquals(INITIAL_COUNT, raw.getCount() ); 207 208 SideEffectBean prototype2FirstInstance = (SideEffectBean) bf.getBean(beanName); 210 assertEquals(INITIAL_COUNT, prototype2FirstInstance.getCount() ); 211 prototype2FirstInstance.doWork(); 212 assertEquals(INITIAL_COUNT + 1, prototype2FirstInstance.getCount() ); 213 214 SideEffectBean prototype2SecondInstance = (SideEffectBean) bf.getBean(beanName); 215 assertFalse("Prototypes are not ==", prototype2FirstInstance == prototype2SecondInstance); 216 assertEquals(INITIAL_COUNT, prototype2SecondInstance.getCount() ); 217 assertEquals(INITIAL_COUNT + 1, prototype2FirstInstance.getCount() ); 218 219 return prototype2FirstInstance; 220 } 221 222 public void testCglibPrototypeInstancesAreIndependent() { 223 Object prototype = testPrototypeInstancesAreIndependent("cglibPrototype"); 224 assertTrue("It's a cglib proxy", AopUtils.isCglibProxy(prototype)); 225 assertFalse("It's not a dynamic proxy", AopUtils.isJdkDynamicProxy(prototype)); 226 } 227 228 public void testPrototypeInstancesAreIndependentWithTargetName() { 229 Object prototype = testPrototypeInstancesAreIndependent("prototype"); 230 } 232 233 236 public void testAutoInvoker() { 237 String name = "Hieronymous"; 238 TestBean target = (TestBean) factory.getBean("test"); 239 target.setName(name); 240 ITestBean autoInvoker = (ITestBean) factory.getBean("autoInvoker"); 241 assertTrue(autoInvoker.getName().equals(name)); 242 } 243 244 public void testCanGetFactoryReferenceAndManipulate() { 245 ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1"); 246 assertEquals("Have one advisors", 1, config.getAdvisors().length); 247 248 ITestBean tb = (ITestBean) factory.getBean("test1"); 249 tb.hashCode(); 251 252 final Exception ex = new UnsupportedOperationException ("invoke"); 253 config.addAdvice(0, new MethodInterceptor() { 255 public Object invoke(MethodInvocation invocation) throws Throwable { 256 throw ex; 257 } 258 }); 259 assertEquals("Have correct advisor count", 2, config.getAdvisors().length); 260 261 tb = (ITestBean) factory.getBean("test1"); 262 try { 263 tb.toString(); 265 fail("Evil interceptor added programmatically should fail all method calls"); 266 } 267 catch (Exception thrown) { 268 assertTrue(thrown == ex); 269 } 270 } 271 272 public static class DependsOnITestBean { 273 public final ITestBean tb; 274 public DependsOnITestBean(ITestBean tb) { 275 this.tb = tb; 276 } 277 } 278 279 283 public void testTargetAsInnerBean() { 284 ListableBeanFactory bf = new XmlBeanFactory(new ClassPathResource("innerBeanTarget.xml", getClass())); 285 ITestBean itb = (ITestBean) bf.getBean("testBean"); 286 assertEquals("innerBeanTarget", itb.getName()); 287 assertEquals("Only have proxy and interceptor: no target", 3, bf.getBeanDefinitionCount()); 288 DependsOnITestBean doit = (DependsOnITestBean) bf.getBean("autowireCheck"); 289 assertSame(itb, doit.tb); 290 } 291 292 295 public void testCanAddAndRemoveAspectInterfacesOnSingleton() { 296 try { 297 TimeStamped ts = (TimeStamped) factory.getBean("test1"); 298 fail("Shouldn't implement TimeStamped before manipulation"); 299 } 300 catch (ClassCastException ex) { 301 } 302 303 ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test1"); 304 long time = 666L; 305 TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); 306 ti.setTime(time); 307 308 int oldCount = config.getAdvisors().length; 310 config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); 311 312 assertTrue(config.getAdvisors().length == oldCount + 1); 313 314 TimeStamped ts = (TimeStamped) factory.getBean("test1"); 315 assertTrue(ts.getTimeStamp() == time); 316 317 config.removeAdvice(ti); 319 320 assertTrue(config.getAdvisors().length == oldCount); 321 322 try { 323 ts.getTimeStamp(); 325 fail("Existing object won't implement this interface any more"); 326 } 327 catch (RuntimeException ex) { 328 } 329 330 331 try { 332 ts = (TimeStamped) factory.getBean("test1"); 333 fail("Should no longer implement TimeStamped"); 334 } 335 catch (ClassCastException ex) { 336 } 337 338 config.removeAdvice(new DebugInterceptor()); 340 341 assertTrue(config.getAdvisors().length == oldCount); 342 343 ITestBean it = (ITestBean) ts; 344 DebugInterceptor debugInterceptor = new DebugInterceptor(); 345 config.addAdvice(0, debugInterceptor); 346 it.getSpouse(); 347 assertEquals(1, debugInterceptor.getCount()); 348 config.removeAdvice(debugInterceptor); 349 it.getSpouse(); 350 assertTrue(debugInterceptor.getCount() == 1); 352 } 353 354 359 public void testCanAddAndRemoveAspectInterfacesOnPrototype() { 360 try { 361 TimeStamped ts = (TimeStamped) factory.getBean("test2"); 362 fail("Shouldn't implement TimeStamped before manipulation"); 363 } 364 catch (ClassCastException ex) { 365 } 366 367 ProxyFactoryBean config = (ProxyFactoryBean) factory.getBean("&test2"); 368 long time = 666L; 369 TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); 370 ti.setTime(time); 371 int oldCount = config.getAdvisors().length; 373 config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); 374 assertTrue(config.getAdvisors().length == oldCount + 1); 375 376 TimeStamped ts = (TimeStamped) factory.getBean("test2"); 377 assertEquals(time, ts.getTimeStamp()); 378 379 config.removeAdvice(ti); 381 assertTrue(config.getAdvisors().length == oldCount); 382 383 assertTrue(ts.getTimeStamp() == time); 385 386 try { 387 ts = (TimeStamped) factory.getBean("test2"); 388 fail("Should no longer implement TimeStamped"); 389 } 390 catch (ClassCastException ex) { 391 } 392 393 config.removeAdvice(new DebugInterceptor()); 395 assertTrue(config.getAdvisors().length == oldCount); 396 397 ITestBean it = (ITestBean) ts; 398 DebugInterceptor debugInterceptor = new DebugInterceptor(); 399 config.addAdvice(0, debugInterceptor); 400 it.getSpouse(); 401 assertTrue(debugInterceptor.getCount() == 0); 403 it = (ITestBean) factory.getBean("test2"); 404 it.getSpouse(); 405 assertEquals(1, debugInterceptor.getCount()); 406 config.removeAdvice(debugInterceptor); 407 it.getSpouse(); 408 409 assertEquals(2, debugInterceptor.getCount()); 411 412 it = (ITestBean) factory.getBean("test2"); 414 it.getSpouse(); 415 assertEquals(2, debugInterceptor.getCount()); 416 417 assertEquals(time, ts.getTimeStamp()); 419 } 420 421 426 public void testCanAddAndRemoveAspectInterfacesOnSingletonByCasting() { 427 ITestBean it = (ITestBean) factory.getBean("test1"); 428 Advised pc = (Advised) it; 429 it.getAge(); 430 NopInterceptor di = new NopInterceptor(); 431 pc.addAdvice(0, di); 432 assertEquals(0, di.getCount()); 433 it.setAge(25); 434 assertEquals(25, it.getAge()); 435 assertEquals(2, di.getCount()); 436 } 437 438 public void testMethodPointcuts() { 439 ITestBean tb = (ITestBean) factory.getBean("pointcuts"); 440 PointcutForVoid.reset(); 441 assertTrue("No methods intercepted", PointcutForVoid.methodNames.isEmpty()); 442 tb.getAge(); 443 assertTrue("Not void: shouldn't have intercepted", PointcutForVoid.methodNames.isEmpty()); 444 tb.setAge(1); 445 tb.getAge(); 446 tb.setName("Tristan"); 447 tb.toString(); 448 assertEquals("Recorded wrong number of invocations", 2, PointcutForVoid.methodNames.size()); 449 assertTrue(PointcutForVoid.methodNames.get(0).equals("setAge")); 450 assertTrue(PointcutForVoid.methodNames.get(1).equals("setName")); 451 } 452 453 public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable { 454 BeanFactory f = new XmlBeanFactory(new ClassPathResource("throwsAdvice.xml", getClass())); 455 ThrowsAdviceInterceptorTests.MyThrowsHandler th = (ThrowsAdviceInterceptorTests.MyThrowsHandler) f.getBean("throwsAdvice"); 456 CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice"); 457 assertEquals(0, cba.getCalls()); 458 assertEquals(0, th.getCalls()); 459 ThrowsAdviceInterceptorTests.IEcho echo = (ThrowsAdviceInterceptorTests.IEcho) f.getBean("throwsAdvised"); 460 int i = 12; 461 echo.setA(i); 462 assertEquals(i, echo.getA()); 463 assertEquals(2, cba.getCalls()); 464 assertEquals(0, th.getCalls()); 465 Exception expected = new Exception (); 466 try { 467 echo.echoException(1, expected); 468 fail(); 469 } 470 catch (Exception ex) { 471 assertEquals(expected, ex); 472 } 473 assertEquals(0, th.getCalls()); 475 476 expected = new ServletException (); 478 try { 479 echo.echoException(1, expected); 480 fail(); 481 } 482 catch (ServletException ex) { 483 assertEquals(expected, ex); 484 } 485 assertEquals(1, th.getCalls("servletException")); 487 } 488 489 506 507 public void testEmptyInterceptorNames() { 508 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("invalidProxyFactory.xml", getClass())); 509 try { 510 ITestBean tb = (ITestBean) factory.getBean("emptyInterceptorNames"); 511 fail("Interceptor names cannot be empty"); 512 } 513 catch (BeanCreationException ex) { 514 } 516 } 517 518 521 public void testGlobalsWithoutTarget() { 522 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("invalidProxyFactory.xml", getClass())); 523 try { 524 ITestBean tb = (ITestBean) factory.getBean("globalsWithoutTarget"); 525 fail("Should require target name"); 526 } 527 catch (BeanCreationException ex) { 528 assertTrue(ex.getCause() instanceof AopConfigException); 529 } 530 } 531 532 538 public void testGlobalsCanAddAspectInterfaces() { 539 AddedGlobalInterface agi = (AddedGlobalInterface) factory.getBean("autoInvoker"); 540 assertTrue(agi.globalsAdded() == -1); 541 542 ProxyFactoryBean pfb = (ProxyFactoryBean) factory.getBean("&validGlobals"); 543 assertEquals("Have 2 globals and 2 explicit advisors", 3, pfb.getAdvisors().length); 545 546 ApplicationListener l = (ApplicationListener) factory.getBean("validGlobals"); 547 agi = (AddedGlobalInterface) l; 548 assertTrue(agi.globalsAdded() == -1); 549 550 try { 551 agi = (AddedGlobalInterface) factory.getBean("test1"); 552 fail("Aspect interface should't be implemeneted without globals"); 553 } 554 catch (ClassCastException ex) { 555 } 556 } 557 558 public void testSerializableSingletonProxy() throws Exception { 559 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("serializationTests.xml", getClass())); 560 Person p = (Person) bf.getBean("serializableSingleton"); 561 assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton")); 562 Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p); 563 assertEquals(p, p2); 564 assertNotSame(p, p2); 565 assertEquals("serializableSingleton", p2.getName()); 566 567 Advice nop = new NopInterceptor(); 569 ((Advised) p).addAdvice(nop); 570 assertEquals(p2.getName(), p2.getName()); 572 assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p)); 573 574 assertTrue(((Advised) p).removeAdvice(nop)); 576 assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p)); 577 } 578 579 public void testSerializablePrototypeProxy() throws Exception { 580 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("serializationTests.xml", getClass())); 581 Person p = (Person) bf.getBean("serializablePrototype"); 582 assertNotSame("Should not be a Singleton", p, bf.getBean("serializablePrototype")); 583 Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p); 584 assertEquals(p, p2); 585 assertNotSame(p, p2); 586 assertEquals("serializablePrototype", p2.getName()); 587 } 588 589 public void testProxyNotSerializableBecauseOfAdvice() throws Exception { 590 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("serializationTests.xml", getClass())); 591 Person p = (Person) bf.getBean("interceptorNotSerializableSingleton"); 592 assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p)); 593 } 594 595 public void testPrototypeAdvisor() { 596 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTests.xml", getClass())); 597 598 ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxy"); 599 ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxy"); 600 601 bean1.setAge(3); 602 bean2.setAge(4); 603 604 assertEquals(3, bean1.getAge()); 605 assertEquals(4, bean2.getAge()); 606 607 ((Lockable) bean1).lock(); 608 609 try { 610 bean1.setAge(5); 611 fail("expected LockedException"); 612 } 613 catch (LockedException ex) { 614 } 616 617 try { 618 bean2.setAge(6); 619 } 620 catch (LockedException ex) { 621 fail("did not expect LockedException"); 622 } 623 } 624 625 public void testPrototypeInterceptorSingletonTarget() { 626 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryTests.xml", getClass())); 627 628 ITestBean bean1 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget"); 629 ITestBean bean2 = (ITestBean) bf.getBean("prototypeTestBeanProxySingletonTarget"); 630 631 bean1.setAge(1); 632 bean2.setAge(2); 633 634 assertEquals(2, bean1.getAge()); 635 636 ((Lockable) bean1).lock(); 637 638 try { 639 bean1.setAge(5); 640 fail("expected LockedException"); 641 } 642 catch (LockedException ex) { 643 } 645 646 try { 647 bean2.setAge(6); 648 } 649 catch (LockedException ex) { 650 fail("did not expect LockedException"); 651 } 652 } 653 654 658 public void testInnerBeanTargetUsingAutowiring() { 659 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryBeanAutowiringTests.xml", getClass())); 660 bf.getBean("testBean"); 661 } 662 663 public void testFrozenFactoryBean() { 664 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("frozenProxyFactoryBean.xml", getClass())); 665 666 Advised advised = (Advised)bf.getBean("frozen"); 667 assertTrue("The proxy should be frozen", advised.isFrozen()); 668 } 669 670 673 public static class PointcutForVoid extends DynamicMethodMatcherPointcutAdvisor { 674 675 public static List methodNames = new LinkedList (); 676 677 public static void reset() { 678 methodNames.clear(); 679 } 680 681 public PointcutForVoid() { 682 super( new MethodInterceptor() { 683 public Object invoke(MethodInvocation invocation) throws Throwable { 684 methodNames.add(invocation.getMethod().getName()); 685 return invocation.proceed(); 686 } 687 }); 688 } 689 690 691 public boolean matches(Method m, Class targetClass, Object [] args) { return m.getReturnType() == Void.TYPE; 694 } 695 } 696 697 698 701 public interface AddedGlobalInterface { 702 int globalsAdded(); 703 } 704 705 706 711 public static class GlobalAspectInterfaceInterceptor implements IntroductionInterceptor { 712 713 public boolean implementsInterface(Class intf) { 714 return intf.equals(AddedGlobalInterface.class); 715 } 716 717 public Object invoke(MethodInvocation mi) throws Throwable { 718 if (mi.getMethod().getDeclaringClass().equals(AddedGlobalInterface.class)) { 719 return new Integer (-1); 720 } 721 return mi.proceed(); 722 } 723 } 724 725 726 public static class GlobalIntroductionAdvice implements IntroductionAdvisor { 727 728 private IntroductionInterceptor gi = new GlobalAspectInterfaceInterceptor(); 729 730 public ClassFilter getClassFilter() { 731 return ClassFilter.TRUE; 732 } 733 734 public Advice getAdvice() { 735 return this.gi; 736 } 737 738 public Class [] getInterfaces() { 739 return new Class [] { AddedGlobalInterface.class }; 740 } 741 742 public boolean isPerInstance() { 743 return false; 744 } 745 746 public void validateInterfaces() { 747 } 748 } 749 750 } 751 | Popular Tags |