KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > reflection > ReflectionPOJO


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.reflection;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:kabirkhan@bigfoot.com">Kabir Khan</a>
33  */

34
35 public class ReflectionPOJO
36 {
37
38    final static Package JavaDoc LANG_PACKAGE = Package.getPackage("java.lang");
39
40    public ReflectionPOJO()
41    {
42       
43    }
44    
45    public ReflectionPOJO(int x)
46    {
47       try
48       {
49          //Sanity checks
50
System.out.println("*** reflection (from constructor): Sanity checks");
51          
52          SimplePerVmInterceptor.reset();
53          ReflectionAopPOJO pojo = new ReflectionAopPOJO(8);
54          if (SimplePerVmInterceptor.constructorIntercepted != 1)
55          {
56             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor");
57          }
58          if (pojo.j != 8)
59          {
60             throw new RuntimeException JavaDoc("POJO.j was not 8");
61          }
62
63          SimplePerVmInterceptor.reset();
64          pojo.method(10);
65          if (SimplePerVmInterceptor.methodIntercepted != 1)
66          {
67             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method call");
68          }
69          
70          SimplePerVmInterceptor.reset();
71          pojo.j = 5;
72          if (SimplePerVmInterceptor.fieldWriteIntercepted != 1)
73          {
74             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept field write");
75          }
76          
77          SimplePerVmInterceptor.reset();
78          int i = pojo.j;
79          System.out.println("i=" + i);
80          if (SimplePerVmInterceptor.fieldReadIntercepted != 1)
81          {
82             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept field read");
83          }
84          
85          CallerInterceptor.reset();
86          pojo = new ReflectionAopPOJO(false);
87          if (CallerInterceptor.intercepted != 1)
88          {
89             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept construction");
90          }
91          
92          CallerInterceptor.reset();
93          pojo.method(false);
94          if (CallerInterceptor.intercepted != 1)
95          {
96             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(boolean) call");
97          }
98          
99          CallerInterceptor.reset();
100          SimplePerVmInterceptor.reset();
101          pojo = new ReflectionAopPOJO(100L);
102          if (CallerInterceptor.intercepted != 1)
103          {
104             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept constructor(long) call");
105          }
106          if (SimplePerVmInterceptor.constructorIntercepted != 1)
107          {
108             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor(long) call");
109          }
110
111          CallerInterceptor.reset();
112          SimplePerVmInterceptor.reset();
113          pojo.otherMethod(200L);
114          if (CallerInterceptor.intercepted != 1)
115          {
116             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(long) call");
117          }
118          if (SimplePerVmInterceptor.methodIntercepted != 1)
119          {
120             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method(long) call");
121          }
122          
123          //Check Class.newInstance() interception
124
System.out.println("*** reflection (from constructor): Class.newInstance");
125          Class JavaDoc clazz = ReflectionAopPOJO.class;
126          SimplePerVmInterceptor.reset();
127          System.out.println("reflection call");
128          pojo = (ReflectionAopPOJO)clazz.newInstance();
129          System.out.println("reflection call - end");
130          if (ReflectionAspectTester.constructor == null) throw new RuntimeException JavaDoc("Not intercepted");
131          if (SimplePerVmInterceptor.constructorIntercepted != 1)
132          {
133             throw new RuntimeException JavaDoc("SimplePerVmInterceptor not invoked for reflected default constructor");
134          }
135
136          //Check Constructor.newInstance() interception
137
System.out.println("*** reflection (from constructor): Constructor.newInstance");
138          Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[]{Integer.TYPE});
139          SimplePerVmInterceptor.reset();
140          System.out.println("reflection call");
141          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{new Integer JavaDoc(4)});
142          System.out.println("reflection call - end");
143          if (SimplePerVmInterceptor.constructorIntercepted != 1)
144          {
145             throw new RuntimeException JavaDoc("SimplePerVmInterceptor not invoked for reflected constructor(int)");
146          }
147          if (pojo.j != 4)
148          {
149             throw new RuntimeException JavaDoc("POJO.j was not 8 following reflection");
150          }
151          
152          //Check Method.invoke() interception (Not handled by ReflectionAspect,
153
//should work out of the box as wrapper is method itself
154
System.out.println("*** reflection (from constructor): Method.invoke");
155          Method JavaDoc method = clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE});
156          SimplePerVmInterceptor.reset();
157          System.out.println("reflection call");
158          method.invoke(pojo, new Object JavaDoc[]{new Integer JavaDoc(55)});
159          System.out.println("reflection call - end");
160          if (SimplePerVmInterceptor.methodIntercepted != 1)
161          {
162             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected method call");
163          }
164          
165          //Check Field.setInt() interception
166
System.out.println("*** reflection (from constructor): Field.setInt");
167          Field JavaDoc field = clazz.getField("j");
168          SimplePerVmInterceptor.reset();
169          System.out.println("reflection call");
170          field.setInt(pojo, 10);
171          System.out.println("reflection call - end");
172          if (ReflectionAspectTester.field == null
173                  || ((Integer JavaDoc) ReflectionAspectTester.args[0]).intValue() != 10)
174          {
175             throw new RuntimeException JavaDoc("Not intercepted");
176          }
177          if (SimplePerVmInterceptor.fieldWriteIntercepted != 1)
178          {
179             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected field write");
180          }
181                
182          
183          //Check Field.getInt() interception
184
System.out.println("*** reflection (from constructor): Field.getInt");
185          SimplePerVmInterceptor.reset();
186          System.out.println("reflection call");
187          int j = field.getInt(pojo);
188          System.out.println("reflection call - end");
189          if (ReflectionAspectTester.field == null
190                  || j != 10)
191          {
192             throw new RuntimeException JavaDoc("Not intercepted");
193          }
194          if (SimplePerVmInterceptor.fieldReadIntercepted != 1)
195          {
196             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected field read");
197          }
198          
199          //These should not be intercepted
200
System.out.println("*** reflection (from constructor): Checking not advised constructors, fields and methods");
201          SimplePerVmInterceptor.reset();
202          constructor = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
203          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{"x"});
204          method = clazz.getMethod("method", new Class JavaDoc[0]);
205          method.invoke(pojo, new Object JavaDoc[0]);
206          field = clazz.getField("k");
207          field.setInt(pojo, 5);
208          field.getInt(pojo);
209          if (SimplePerVmInterceptor.hasIntercepted())
210          {
211             throw new RuntimeException JavaDoc("SimplePerVmInterceptor intercepted something that should have been left alone");
212          }
213          
214          System.out.println("*** reflection (from constructor): Checking not advised constructor");
215          Class JavaDoc notAdvisedClazz = NotAdvisedPOJO.class;
216          constructor = notAdvisedClazz.getConstructor(new Class JavaDoc[0]);
217          NotAdvisedPOJO naPojo = (NotAdvisedPOJO)constructor.newInstance(new Object JavaDoc[0]);
218
219          System.out.println("*** reflection (from constructor): Checking not advised method");
220          method = notAdvisedClazz.getMethod("method", new Class JavaDoc[0]);
221          method.invoke(naPojo, new Object JavaDoc[0]);
222          
223          System.out.println("*** reflection (from constructor): Checking not advised field set");
224          field = notAdvisedClazz.getField("i");
225          field.setInt(naPojo, 1);
226          
227          System.out.println("*** reflection (from constructor): Checking not advised field get");
228          field.getInt(naPojo);
229
230          System.out.println("*** reflection (from constructor): Checking constructor caller");
231          CallerInterceptor.reset();
232          constructor = clazz.getConstructor(new Class JavaDoc[]{Boolean.TYPE});
233          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{Boolean.TRUE});
234          if (CallerInterceptor.intercepted != 1)
235          {
236             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept reflected construction");
237          }
238          
239          System.out.println("*** reflection (from constructor): Checking method caller");
240          CallerInterceptor.reset();
241          method = clazz.getMethod("method", new Class JavaDoc[]{Boolean.TYPE});
242          method.invoke(pojo, new Object JavaDoc[]{Boolean.FALSE});
243          if (CallerInterceptor.intercepted != 1)
244          {
245             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept reflected method(boolean) call");
246          }
247          
248          //These have both caller and execution pointcuts
249
System.out.println("*** reflection (from constructor): Checking constructor caller and execution");
250          CallerInterceptor.reset();
251          SimplePerVmInterceptor.reset();
252          constructor = clazz.getConstructor(new Class JavaDoc[]{Long.TYPE});
253          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{new Long JavaDoc(100L)});
254          if (CallerInterceptor.intercepted != 1)
255          {
256             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept constructor(long) call");
257          }
258          if (SimplePerVmInterceptor.constructorIntercepted != 1)
259          {
260             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor(long) call");
261          }
262
263          System.out.println("*** reflection (from constructor): Checking method caller and execution");
264          CallerInterceptor.reset();
265          SimplePerVmInterceptor.reset();
266          method = clazz.getMethod("otherMethod", new Class JavaDoc[]{Long.TYPE});
267          method.invoke(pojo, new Object JavaDoc[]{new Long JavaDoc(100L)});
268          if (CallerInterceptor.intercepted != 1)
269          {
270             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(long) call");
271          }
272          if (SimplePerVmInterceptor.methodIntercepted != 1)
273          {
274             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method(long) call");
275          }
276       }
277       catch (Exception JavaDoc e)
278       {
279          throw new RuntimeException JavaDoc(e);
280       }
281    }
282
283
284    public void testCreationAndFieldAccess()
285    {
286       try
287       {
288          //Sanity checks
289
System.out.println("*** reflection (from method): Sanity checks");
290          
291          SimplePerVmInterceptor.reset();
292          ReflectionAopPOJO pojo = new ReflectionAopPOJO(8);
293          if (SimplePerVmInterceptor.constructorIntercepted != 1)
294          {
295             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor");
296          }
297          if (pojo.j != 8)
298          {
299             throw new RuntimeException JavaDoc("POJO.j was not 8");
300          }
301
302          SimplePerVmInterceptor.reset();
303          pojo.method(10);
304          if (SimplePerVmInterceptor.methodIntercepted != 1)
305          {
306             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method call");
307          }
308          
309          SimplePerVmInterceptor.reset();
310          pojo.j = 5;
311          if (SimplePerVmInterceptor.fieldWriteIntercepted != 1)
312          {
313             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept field write");
314          }
315          
316          SimplePerVmInterceptor.reset();
317          int i = pojo.j;
318          System.out.println("i=" + i);
319          if (SimplePerVmInterceptor.fieldReadIntercepted != 1)
320          {
321             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept field read");
322          }
323          
324          CallerInterceptor.reset();
325          pojo = new ReflectionAopPOJO(false);
326          if (CallerInterceptor.intercepted != 1)
327          {
328             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept construction");
329          }
330          
331          CallerInterceptor.reset();
332          pojo.method(false);
333          if (CallerInterceptor.intercepted != 1)
334          {
335             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(boolean) call");
336          }
337          
338          CallerInterceptor.reset();
339          SimplePerVmInterceptor.reset();
340          pojo = new ReflectionAopPOJO(100L);
341          if (CallerInterceptor.intercepted != 1)
342          {
343             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept constructor(long) call");
344          }
345          if (SimplePerVmInterceptor.constructorIntercepted != 1)
346          {
347             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor(long) call");
348          }
349
350          CallerInterceptor.reset();
351          SimplePerVmInterceptor.reset();
352          pojo.otherMethod(200L);
353          if (CallerInterceptor.intercepted != 1)
354          {
355             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(long) call");
356          }
357          if (SimplePerVmInterceptor.methodIntercepted != 1)
358          {
359             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method(long) call");
360          }
361          
362          //Check Class.newInstance() interception
363
System.out.println("*** reflection (from method): Class.newInstance");
364          Class JavaDoc clazz = ReflectionAopPOJO.class;
365          SimplePerVmInterceptor.reset();
366          System.out.println("reflection call");
367          pojo = (ReflectionAopPOJO)clazz.newInstance();
368          System.out.println("reflection call - end");
369          if (ReflectionAspectTester.constructor == null) throw new RuntimeException JavaDoc("Not intercepted");
370          if (SimplePerVmInterceptor.constructorIntercepted != 1)
371          {
372             throw new RuntimeException JavaDoc("SimplePerVmInterceptor not invoked for reflected default constructor");
373          }
374
375          //Check Constructor.newInstance() interception
376
System.out.println("*** reflection (from method): Constructor.newInstance");
377          Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[]{Integer.TYPE});
378          SimplePerVmInterceptor.reset();
379          System.out.println("reflection call");
380          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{new Integer JavaDoc(4)});
381          System.out.println("reflection call - end");
382          if (SimplePerVmInterceptor.constructorIntercepted != 1)
383          {
384             throw new RuntimeException JavaDoc("SimplePerVmInterceptor not invoked for reflected constructor(int)");
385          }
386          if (pojo.j != 4)
387          {
388             throw new RuntimeException JavaDoc("POJO.j was not 8 following reflection");
389          }
390          
391          //Check Method.invoke() interception (Not handled by ReflectionAspect,
392
//should work out of the box as wrapper is method itself
393
System.out.println("*** reflection (from method): Method.invoke");
394          Method JavaDoc method = clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE});
395          SimplePerVmInterceptor.reset();
396          System.out.println("reflection call");
397          method.invoke(pojo, new Object JavaDoc[]{new Integer JavaDoc(55)});
398          System.out.println("reflection call - end");
399          if (SimplePerVmInterceptor.methodIntercepted != 1)
400          {
401             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected method call");
402          }
403          
404          //Check Field.setInt() interception
405
System.out.println("*** reflection (from method): Field.setInt");
406          Field JavaDoc field = clazz.getField("j");
407          SimplePerVmInterceptor.reset();
408          System.out.println("reflection call");
409          field.setInt(pojo, 10);
410          System.out.println("reflection call - end");
411          if (ReflectionAspectTester.field == null
412                  || ((Integer JavaDoc) ReflectionAspectTester.args[0]).intValue() != 10)
413          {
414             throw new RuntimeException JavaDoc("Not intercepted");
415          }
416          if (SimplePerVmInterceptor.fieldWriteIntercepted != 1)
417          {
418             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected field write");
419          }
420                
421          
422          //Check Field.getInt() interception
423
System.out.println("*** reflection (from method): Field.getInt");
424          SimplePerVmInterceptor.reset();
425          System.out.println("reflection call");
426          int j = field.getInt(pojo);
427          System.out.println("reflection call - end");
428          if (ReflectionAspectTester.field == null
429                  || j != 10)
430          {
431             throw new RuntimeException JavaDoc("Not intercepted");
432          }
433          if (SimplePerVmInterceptor.fieldReadIntercepted != 1)
434          {
435             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept reflected field read");
436          }
437          
438          //These should not be intercepted
439
SimplePerVmInterceptor.reset();
440          constructor = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
441          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{"x"});
442          method = clazz.getMethod("method", new Class JavaDoc[0]);
443          method.invoke(pojo, new Object JavaDoc[0]);
444          field = clazz.getField("k");
445          field.setInt(pojo, 5);
446          field.getInt(pojo);
447          
448          if (SimplePerVmInterceptor.hasIntercepted())
449          {
450             throw new RuntimeException JavaDoc("SimplePerVmInterceptor intercepted something that should have been left alone");
451          }
452          
453          System.out.println("*** reflection (from method): Checking not advised constructor");
454          Class JavaDoc notAdvisedClazz = NotAdvisedPOJO.class;
455          constructor = notAdvisedClazz.getConstructor(new Class JavaDoc[0]);
456          NotAdvisedPOJO naPojo = (NotAdvisedPOJO)constructor.newInstance(new Object JavaDoc[0]);
457
458          System.out.println("*** reflection (from method): Checking not advised method");
459          method = notAdvisedClazz.getMethod("method", new Class JavaDoc[0]);
460          method.invoke(naPojo, new Object JavaDoc[0]);
461          
462          System.out.println("*** reflection (from method): Checking not advised field set");
463          field = notAdvisedClazz.getField("i");
464          field.setInt(naPojo, 1);
465          
466          System.out.println("*** reflection (from method): Checking not advised field get");
467          field.getInt(naPojo);
468
469          System.out.println("*** reflection (from method): Checking constructor caller");
470          CallerInterceptor.reset();
471          constructor = clazz.getConstructor(new Class JavaDoc[]{Boolean.TYPE});
472          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{Boolean.TRUE});
473          if (CallerInterceptor.intercepted != 1)
474          {
475             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept reflected construction");
476          }
477          
478          System.out.println("*** reflection (from method): Checking method caller");
479          CallerInterceptor.reset();
480          method = clazz.getMethod("method", new Class JavaDoc[]{Boolean.TYPE});
481          method.invoke(pojo, new Object JavaDoc[]{Boolean.FALSE});
482          if (CallerInterceptor.intercepted != 1)
483          {
484             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept reflected method(boolean) call");
485          }
486          
487          //These have both caller and execution pointcuts
488
System.out.println("*** reflection (from constructor): Checking constructor caller and execution");
489          CallerInterceptor.reset();
490          SimplePerVmInterceptor.reset();
491          constructor = clazz.getConstructor(new Class JavaDoc[]{Long.TYPE});
492          pojo = (ReflectionAopPOJO)constructor.newInstance(new Object JavaDoc[]{new Long JavaDoc(100L)});
493          if (CallerInterceptor.intercepted != 1)
494          {
495             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept constructor(long) call");
496          }
497          if (SimplePerVmInterceptor.constructorIntercepted != 1)
498          {
499             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept constructor(long) call");
500          }
501
502          System.out.println("*** reflection (from constructor): Checking method caller and execution");
503          CallerInterceptor.reset();
504          SimplePerVmInterceptor.reset();
505          method = clazz.getMethod("otherMethod", new Class JavaDoc[]{Long.TYPE});
506          method.invoke(pojo, new Object JavaDoc[]{new Long JavaDoc(100L)});
507          if (CallerInterceptor.intercepted != 1)
508          {
509             throw new RuntimeException JavaDoc("CallerInterceptor did not intercept method(long) call");
510          }
511          if (SimplePerVmInterceptor.methodIntercepted != 1)
512          {
513             throw new RuntimeException JavaDoc("SimplePerVmInterceptor did not intercept method(long) call");
514          }
515          
516       }
517       catch (Exception JavaDoc e)
518       {
519          throw new RuntimeException JavaDoc(e);
520       }
521    }
522
523    public void testCleanGetMethods()
524    {
525       System.out.println("*** reflection (from method): Class.getMethods()");
526       Class JavaDoc pojoClass = ReflectionAopPOJO.class;
527       Class JavaDoc pojoRoot = ReflectionAopRootPOJO.class;
528       try
529       {
530          //helloWorld() is introduced
531
Method JavaDoc[] check = new Method JavaDoc[]{
532             pojoClass.getMethod("method", new Class JavaDoc[0]),
533             pojoClass.getMethod("method", new Class JavaDoc[]{Integer.TYPE}),
534             pojoClass.getMethod("method", new Class JavaDoc[]{Boolean.TYPE}),
535             pojoClass.getMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE}),
536             pojoRoot.getMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE, Short.TYPE}),
537             pojoClass.getMethod("otherMethod", new Class JavaDoc[]{Long.TYPE}),
538             pojoClass.getMethod("helloWorld", new Class JavaDoc[]{String JavaDoc.class})};
539
540          Method JavaDoc[] methods = pojoClass.getMethods();
541
542          ArrayList JavaDoc methodList = new ArrayList JavaDoc();
543
544          for (int i = 0; i < methods.length; i++)
545          {
546             if (!methods[i].getDeclaringClass().getPackage().equals(LANG_PACKAGE))
547             {
548                methodList.add(methods[i]);
549             }
550          }
551
552
553          for (int i = 0; i < check.length; i++)
554          {
555             if (!methodList.remove(check[i]))
556             {
557                throw new RuntimeException JavaDoc("\"Cleaned\" Class.getMethods() did not return all expected methods: " + check[i]);
558             }
559          }
560
561          if (methodList.size() > 0)
562          {
563             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("The following methods should not "
564                     + "have ben returned by \"Cleaned\" Class.getMethods():\n");
565
566             for (Iterator JavaDoc it = methodList.iterator(); it.hasNext();)
567             {
568                sb.append(it.next());
569             }
570
571             throw new RuntimeException JavaDoc(sb.toString());
572          }
573          
574       }
575       catch (NoSuchMethodException JavaDoc e)
576       {
577          throw new RuntimeException JavaDoc(e);
578       }
579    }
580
581    public void testCleanGetDeclaredMethods()
582    {
583
584       System.out.println("*** reflection (from method): Class.getDeclaredMethods()");
585       Class JavaDoc clazz = ReflectionAopPOJO.class;
586       try
587       {
588          //helloWorld() is introduced
589
Method JavaDoc[] check = new Method JavaDoc[]{
590             clazz.getDeclaredMethod("method", new Class JavaDoc[0]),
591             clazz.getDeclaredMethod("method", new Class JavaDoc[]{Integer.TYPE}),
592             clazz.getDeclaredMethod("method", new Class JavaDoc[]{Boolean.TYPE}),
593             clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE}),
594             clazz.getMethod("otherMethod", new Class JavaDoc[]{Long.TYPE}),
595             clazz.getDeclaredMethod("privateMethod", new Class JavaDoc[0]),
596             clazz.getMethod("helloWorld", new Class JavaDoc[]{String JavaDoc.class})};
597
598          Method JavaDoc[] methods = clazz.getDeclaredMethods();
599
600          ArrayList JavaDoc methodList = new ArrayList JavaDoc();
601
602          for (int i = 0; i < methods.length; i++)
603          {
604             methodList.add(methods[i]);
605          }
606
607
608          for (int i = 0; i < check.length; i++)
609          {
610             if (!methodList.remove(check[i]))
611             {
612                throw new RuntimeException JavaDoc("\"Cleaned\" Class.getDeclaredMethods() did not return all expected methods: " + check[i]);
613             }
614          }
615
616          if (methodList.size() > 0)
617          {
618             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("The following methods should not "
619                     + "have ben returned by \"Cleaned\" Class.getDeclaredMethods():\n");
620
621             for (Iterator JavaDoc it = methodList.iterator(); it.hasNext();)
622             {
623                sb.append(it.next());
624             }
625
626             throw new RuntimeException JavaDoc(sb.toString());
627          }
628       }
629       catch (NoSuchMethodException JavaDoc e)
630       {
631          throw new RuntimeException JavaDoc(e);
632       }
633    }
634
635    public void testCleanGetDeclaredFields()
636    {
637
638       System.out.println("*** reflection (from method): Class.getDeclaredFields()");
639       Class JavaDoc clazz = ReflectionAopPOJO.class;
640       try
641       {
642          Field JavaDoc[] check = new Field JavaDoc[]{
643             clazz.getDeclaredField("i"),
644             clazz.getDeclaredField("j"),
645             clazz.getDeclaredField("k")};
646
647          Field JavaDoc[] fields = clazz.getDeclaredFields();
648
649          ArrayList JavaDoc fieldList = new ArrayList JavaDoc();
650
651          for (int i = 0; i < fields.length; i++)
652          {
653             fieldList.add(fields[i]);
654          }
655
656
657          for (int i = 0; i < check.length; i++)
658          {
659             if (!fieldList.remove(check[i]))
660             {
661                throw new RuntimeException JavaDoc("\"Cleaned\" Class.getDeclaredFields() did not return all expected fields: " + check[i]);
662             }
663          }
664
665          if (fieldList.size() > 0)
666          {
667             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("The following methods should not "
668                     + "have ben returned by \"Cleaned\" Class.getDeclaredFields():\n");
669
670             for (Iterator JavaDoc it = fieldList.iterator(); it.hasNext();)
671             {
672                sb.append(it.next());
673             }
674
675             throw new RuntimeException JavaDoc(sb.toString());
676          }
677       }
678       catch (NoSuchFieldException JavaDoc e)
679       {
680          throw new RuntimeException JavaDoc(e);
681       }
682    }
683
684
685    public void testCleanGetInterfaces()
686    {
687
688       System.out.println("*** reflection (from method): Class.getInterfaces()");
689       ReflectionAopPOJO pojo = new ReflectionAopPOJO();
690       System.out.println("pojo=" + pojo);
691       Class JavaDoc clazz = ReflectionAopPOJO.class;
692       Class JavaDoc[] interfaces = clazz.getInterfaces();
693
694       if (interfaces.length != 1)
695       {
696          throw new RuntimeException JavaDoc("Expected 1 interface, got " + interfaces.length);
697       }
698
699       if (!interfaces[0].getName().equals("org.jboss.test.aop.reflection.Introduction"))
700       {
701          throw new RuntimeException JavaDoc("Did not get expected interface org.jboss.test.aop.reflection.Introduction");
702       }
703    }
704
705    public void testCleanGetDeclaredMethod()
706    {
707
708       System.out.println("*** reflection (from method): Class.getDeclaredMethod()");
709       Class JavaDoc pojoClass = ReflectionAopPOJO.class;
710       Class JavaDoc pojoRoot = ReflectionAopRootPOJO.class;
711
712       try
713       {
714          Method JavaDoc m = pojoClass.getDeclaredMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE, Short.TYPE});
715          throw new RuntimeException JavaDoc("Should not be here: " + m);
716       }
717       catch (NoSuchMethodException JavaDoc e)
718       {
719          //Fine, it is declared in ReflectionAopRootPOJO
720
}
721
722       try
723       {
724          Method JavaDoc m = pojoRoot.getDeclaredMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE, Short.TYPE});
725
726          if (!m.getName().equals("method")
727                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopRootPOJO")
728                  || !m.getParameterTypes()[0].equals(Integer.TYPE)
729                  || !m.getParameterTypes()[1].equals(Long.TYPE))
730          {
731             throw new RuntimeException JavaDoc("Wrong method got");
732          }
733       }
734       catch (NoSuchMethodException JavaDoc e)
735       {
736          throw new RuntimeException JavaDoc("Expected method not there");
737       }
738
739       try
740       {
741          Method JavaDoc m = pojoClass.getDeclaredMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE});
742
743          if (!m.getName().equals("method")
744                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
745                  || !m.getParameterTypes()[0].equals(Integer.TYPE)
746                  || !m.getParameterTypes()[1].equals(Long.TYPE))
747          {
748             throw new RuntimeException JavaDoc("Wrong method got");
749          }
750       }
751       catch (NoSuchMethodException JavaDoc e)
752       {
753          throw new RuntimeException JavaDoc("Expected method not there");
754       }
755
756       try
757       {
758          Method JavaDoc m = pojoClass.getDeclaredMethod("method", new Class JavaDoc[]{Integer.TYPE});
759
760          if (!m.getName().equals("method")
761                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
762                  || !m.getParameterTypes()[0].equals(Integer.TYPE))
763          {
764             throw new RuntimeException JavaDoc("Wrong method got");
765          }
766       }
767       catch (NoSuchMethodException JavaDoc e)
768       {
769          throw new RuntimeException JavaDoc("Expected method not there");
770       }
771
772       try
773       {
774          Method JavaDoc m = pojoClass.getDeclaredMethod("method", new Class JavaDoc[0]);
775
776          if (!m.getName().equals("method")
777                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO"))
778          {
779             throw new RuntimeException JavaDoc("Wrong method got");
780          }
781       }
782       catch (NoSuchMethodException JavaDoc e)
783       {
784          throw new RuntimeException JavaDoc("Expected method not there");
785       }
786
787       try
788       {
789          Method JavaDoc m = pojoClass.getDeclaredMethod("privateMethod", new Class JavaDoc[0]);
790
791          if (!m.getName().equals("privateMethod")
792                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO"))
793          {
794             throw new RuntimeException JavaDoc("Wrong method got");
795          }
796       }
797       catch (NoSuchMethodException JavaDoc e)
798       {
799          throw new RuntimeException JavaDoc("Expected method not there");
800       }
801
802       try
803       {
804          Method JavaDoc m = pojoClass.getMethod("notThere", new Class JavaDoc[0]);
805          throw new RuntimeException JavaDoc("Method should not be there " + m);
806       }
807       catch (NoSuchMethodException JavaDoc e)
808       {
809          //Fine
810
}
811
812       try
813       {
814          Method JavaDoc m = pojoClass.getMethod("_getAdvisor", new Class JavaDoc[0]);
815          throw new RuntimeException JavaDoc("Method should have been cleaned " + m);
816       }
817       catch (NoSuchMethodException JavaDoc e)
818       {
819          //Fine, this is the cleaning bit
820
}
821
822       try
823       {
824          Method JavaDoc m = pojoRoot.getMethod("_getAdvisor", new Class JavaDoc[0]);
825          throw new RuntimeException JavaDoc("Method should have been cleaned " + m);
826       }
827       catch (NoSuchMethodException JavaDoc e)
828       {
829          //Fine, this is the cleaning bit
830
}
831
832    }
833
834    public void testCleanGetMethod()
835    {
836
837       System.out.println("*** reflection (from method): Class.getMethod()");
838       Class JavaDoc clazz = ReflectionAopPOJO.class;
839
840       try
841       {
842          Method JavaDoc m = clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE, Short.TYPE});
843
844          if (!m.getName().equals("method")
845                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopRootPOJO")
846                  || !m.getParameterTypes()[0].equals(Integer.TYPE)
847                  || !m.getParameterTypes()[1].equals(Long.TYPE))
848          {
849             throw new RuntimeException JavaDoc("Wrong method got");
850          }
851       }
852       catch (NoSuchMethodException JavaDoc e)
853       {
854          throw new RuntimeException JavaDoc("Expected method not there");
855       }
856
857       try
858       {
859          Method JavaDoc m = clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE, Long.TYPE});
860
861          if (!m.getName().equals("method")
862                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
863                  || !m.getParameterTypes()[0].equals(Integer.TYPE)
864                  || !m.getParameterTypes()[1].equals(Long.TYPE))
865          {
866             throw new RuntimeException JavaDoc("Wrong method got");
867          }
868       }
869       catch (NoSuchMethodException JavaDoc e)
870       {
871          throw new RuntimeException JavaDoc("Expected method not there");
872       }
873
874       try
875       {
876          Method JavaDoc m = clazz.getMethod("method", new Class JavaDoc[]{Integer.TYPE});
877
878          if (!m.getName().equals("method")
879                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
880                  || !m.getParameterTypes()[0].equals(Integer.TYPE))
881          {
882             throw new RuntimeException JavaDoc("Wrong method got");
883          }
884       }
885       catch (NoSuchMethodException JavaDoc e)
886       {
887          throw new RuntimeException JavaDoc("Expected method not there");
888       }
889
890       try
891       {
892          Method JavaDoc m = clazz.getMethod("method", new Class JavaDoc[0]);
893
894          if (!m.getName().equals("method")
895                  || !m.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO"))
896          {
897             throw new RuntimeException JavaDoc("Wrong method got");
898          }
899       }
900       catch (NoSuchMethodException JavaDoc e)
901       {
902          throw new RuntimeException JavaDoc("Expected method not there");
903       }
904
905       try
906       {
907          Method JavaDoc m = clazz.getMethod("notThere", new Class JavaDoc[0]);
908          throw new RuntimeException JavaDoc("Method should not be there " + m);
909       }
910       catch (NoSuchMethodException JavaDoc e)
911       {
912          //Fine
913
}
914
915       try
916       {
917          Method JavaDoc m = clazz.getMethod("_getAdvisor", new Class JavaDoc[0]);
918          throw new RuntimeException JavaDoc("Method should have been cleaned " + m);
919       }
920       catch (NoSuchMethodException JavaDoc e)
921       {
922          //Fine, this is the cleaning bit
923
}
924
925    }
926
927    public void testCleanGetDeclaredField()
928    {
929
930       System.out.println("*** reflection (from method): Class.getDeclaredField()");
931       Class JavaDoc clazz = ReflectionAopPOJO.class;
932       try
933       {
934          Field JavaDoc f = clazz.getDeclaredField("i");
935
936          if (!f.getName().equals("i")
937                  || !f.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
938                  || !f.getType().equals(Integer.TYPE))
939          {
940             throw new RuntimeException JavaDoc("Wrong field got");
941          }
942       }
943       catch (NoSuchFieldException JavaDoc e)
944       {
945          throw new RuntimeException JavaDoc("Expected method not there");
946       }
947
948       try
949       {
950          Field JavaDoc f = clazz.getDeclaredField("j");
951
952          if (!f.getName().equals("j")
953                  || !f.getDeclaringClass().getName().equals("org.jboss.test.aop.reflection.ReflectionAopPOJO")
954                  || !f.getType().equals(Integer.TYPE))
955          {
956             throw new RuntimeException JavaDoc("Wrong field got");
957          }
958       }
959       catch (NoSuchFieldException JavaDoc e)
960       {
961          throw new RuntimeException JavaDoc("Expected method not there");
962       }
963    }
964
965    public void testCleanGetClasses()
966    {
967       //No extra public classes are added by AOP, but include this test in case stuff changes in future
968
System.out.println("*** reflection (from method): Class.getClasses()");
969       HashSet JavaDoc classSet = new HashSet JavaDoc();
970       classSet.add("org.jboss.test.aop.reflection.ReflectionAopPOJO$AopPOJOInner");
971       classSet.add("org.jboss.test.aop.reflection.ReflectionAopRootPOJO$AopRootPOJOInner");
972       Class JavaDoc clazz = ReflectionAopPOJO.class;
973
974       Class JavaDoc[] classes = clazz.getClasses();
975
976       for (int i = 0; i < classes.length; i++)
977       {
978          String JavaDoc name = classes[i].getName();
979          if (!classSet.contains(name))
980          {
981             throw new RuntimeException JavaDoc(name + " was in the list of classes and should have been removed");
982          }
983       }
984
985       if (classes.length != classSet.size())
986       {
987          throw new RuntimeException JavaDoc("Not all the declared classes were returned");
988       }
989    }
990
991    public void testCleanGetDeclaredClasses()
992    {
993       System.out.println("*** reflection (from method): Class.getDeclaredClasses()");
994       HashSet JavaDoc declaredClasses = new HashSet JavaDoc();
995       declaredClasses.add("org.jboss.test.aop.reflection.ReflectionAopPOJO$AopPOJOInner");
996
997       Class JavaDoc clazz = ReflectionAopPOJO.class;
998       Class JavaDoc[] classes = clazz.getDeclaredClasses();
999
1000      for (int i = 0; i < classes.length; i++)
1001      {
1002         String JavaDoc name = classes[i].getName();
1003         if (!declaredClasses.contains(name))
1004         {
1005            throw new RuntimeException JavaDoc(name + " was in the list of declared classes and should have been removed");
1006         }
1007      }
1008
1009      if (classes.length != declaredClasses.size())
1010      {
1011         throw new RuntimeException JavaDoc("Not all the declared classes were returned");
1012      }
1013   }
1014}
1015
Popular Tags