KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > AssertConstructor


1 /*
2  * @(#)AssertConstructor.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.junit.v1;
28
29 import junit.framework.AssertionFailedError;
30 import junit.framework.Assert;
31
32 import java.lang.reflect.Constructor JavaDoc;
33 import java.lang.reflect.Modifier JavaDoc;
34
35
36 /**
37  * A set of assert methods that test a class for implementation of specific
38  * constructor types.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @version $Date: 2003/02/10 22:52:19 $
42  * @since July 21, 2002
43  */

44 public class AssertConstructor
45 {
46     /**
47      * Bit-mask that allows for the constructor to have public access.
48      */

49     public static final int PUBLIC = 0x01;
50     
51     /**
52      * Bit-mask that allows for the constructor to have protected access.
53      */

54     public static final int PROTECTED = 0x02;
55     
56     /**
57      * Bit-mask that allows for the constructor to have package-private access.
58      */

59     public static final int PACKAGE = 0x04;
60     
61     /**
62      * Bit-mask that allows for the constructor to have private access.
63      */

64     public static final int PRIVATE = 0x08;
65     
66     /**
67      * Bit-mask that allows the constructor to have any protection.
68      */

69     public static final int ANY_PROTECTION =
70         PUBLIC | PROTECTED | PACKAGE | PRIVATE;
71     
72     
73     /**
74      * Ensures that the class <tt>c</tt> has a default (no-arg), public
75      * constructor.
76      *
77      * @param message message that describes what failed if the assertion
78      * fails.
79      * @param c the class check for a constructor.
80      */

81     public static void assertHasDefaultConstructor( String JavaDoc message, Class JavaDoc c )
82     {
83         assertHasSameConstructor( message, c, new Class JavaDoc[0] );
84     }
85     
86     
87     /**
88      * Ensures that the class <tt>c</tt> has a default (no-arg), public
89      * constructor.
90      *
91      * @param c the class check for a constructor.
92      */

93     public static void assertHasDefaultConstructor( Class JavaDoc c )
94     {
95         assertHasSameConstructor( c, new Class JavaDoc[0] );
96     }
97     
98     
99     /**
100      * Ensures that the class for object <tt>o</tt> has a default (no-arg),
101      * public constructor.
102      *
103      * @param message message that describes what failed if the assertion
104      * fails.
105      * @param o the object to check the class's constructor.
106      */

107     public static void assertHasDefaultConstructor( String JavaDoc message, Object JavaDoc o )
108     {
109         assertHasSameConstructor( message, o, new Class JavaDoc[0] );
110     }
111     
112     
113     /**
114      * Ensures that the class for object <tt>o</tt> has a default (no-arg),
115      * public constructor.
116      *
117      * @param o the object to check the class's constructor.
118      */

119     public static void assertHasDefaultConstructor( Object JavaDoc o )
120     {
121         assertHasSameConstructor( o, new Class JavaDoc[0] );
122     }
123     
124     
125     
126     /**
127      * Ensures that there exists a constructor in class <tt>c</tt> that
128      * accepts the same number of arguments given in <tt>arguments</tt>,
129      * and that the constructor has the same class or an instance of the
130      * class in each position of the argument list.
131      *
132      * @param message message that describes what failed if the assertion
133      * fails.
134      * @param c the class check for a constructor.
135      * @param arguments list of classes which the constructor must have
136      * as parameters, or subclasses of the arguments. A <tt>null</tt>
137      * argument index indicates that any type is allowed (equivalent
138      * to specifying <tt>Object.class</tt> in the argument). If
139      * the array is <tt>null</tt>, then the argument list will only
140      * match the default (no-argument) constructor (which is the
141      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
142      * @param protection a bitwise ORed value containing one or more of the
143      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
144      * or <tt>PRIVATE</tt>.
145      * @see #assertHasConstructor( String, Class, Class[] )
146      * @see #assertHasConstructor( String, Object, Class[], int )
147      * @see #assertHasSameConstructor( String, Class, Class[], int )
148      */

149     public static void assertHasConstructor( String JavaDoc message,
150             Class JavaDoc c, Class JavaDoc[] arguments, int protection )
151     {
152         Assert.assertNotNull( "Null class argument.", c );
153         
154         Assert.assertNotNull( message,
155             getConstructor( c, arguments, protection ) );
156     }
157     
158     
159     /**
160      * Ensures that there exists a constructor in class <tt>c</tt> that
161      * accepts the same number of arguments given in <tt>arguments</tt>,
162      * and that the constructor has the same class or an instance of the
163      * class in each position of the argument list.
164      *
165      * @param arguments list of classes which the constructor must have
166      * as parameters, or subclasses of the arguments. A <tt>null</tt>
167      * argument index indicates that any type is allowed (equivalent
168      * to specifying <tt>Object.class</tt> in the argument). If
169      * the array is <tt>null</tt>, then the argument list will only
170      * match the default (no-argument) constructor (which is the
171      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
172      * @param protection a bitwise ORed value containing one or more of the
173      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
174      * or <tt>PRIVATE</tt>.
175      * @see #assertHasConstructor( Class, Class[] )
176      * @see #assertHasConstructor( Object, Class[], int )
177      * @see #assertHasSameConstructor( Class, Class[], int )
178      */

179     public static void assertHasConstructor(
180             Class JavaDoc c, Class JavaDoc[] arguments, int protection )
181     {
182         Assert.assertNotNull( "Null class argument.", c );
183         
184         Assert.assertNotNull( getConstructor( c, arguments, protection ) );
185     }
186     
187     
188     /**
189      * Ensures that there exists a public constructor in class <tt>c</tt> that
190      * accepts the same number of arguments given in <tt>arguments</tt>,
191      * and that the constructor has the same class or an instance of the
192      * class in each position of the argument list.
193      *
194      * @param arguments list of classes which the constructor must have
195      * as parameters, or subclasses of the arguments. A <tt>null</tt>
196      * argument index indicates that any type is allowed (equivalent
197      * to specifying <tt>Object.class</tt> in the argument). If
198      * the array is <tt>null</tt>, then the argument list will only
199      * match the default (no-argument) constructor (which is the
200      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
201      * @see #assertHasConstructor( String, Class, Class[], int )
202      * @see #assertHasConstructor( String, Object, Class[] )
203      * @see #assertHasSameConstructor( String, Class, Class[] )
204      */

205     public static void assertHasConstructor( String JavaDoc message,
206             Class JavaDoc c, Class JavaDoc[] arguments )
207     {
208         assertHasConstructor( message, c, arguments, PUBLIC );
209     }
210     
211     
212     /**
213      * Ensures that there exists a public constructor in class <tt>c</tt> that
214      * accepts the same number of arguments given in <tt>arguments</tt>,
215      * and that the constructor has the same class or an instance of the
216      * class in each position of the argument list.
217      *
218      * @param arguments list of classes which the constructor must have
219      * as parameters, or subclasses of the arguments. A <tt>null</tt>
220      * argument index indicates that any type is allowed (equivalent
221      * to specifying <tt>Object.class</tt> in the argument). If
222      * the array is <tt>null</tt>, then the argument list will only
223      * match the default (no-argument) constructor (which is the
224      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
225      * @see #assertHasConstructor( Class, Class[], int )
226      * @see #assertHasConstructor( Object, Class[] )
227      * @see #assertHasSameConstructor( Class, Class[] )
228      */

229     public static void assertHasConstructor(
230             Class JavaDoc c, Class JavaDoc[] arguments )
231     {
232         assertHasConstructor( c, arguments, PUBLIC );
233     }
234     
235     
236     /**
237      * Ensures that there exists a constructor in class <tt>c</tt> that
238      * accepts the same number of arguments given in <tt>arguments</tt>,
239      * and that the constructor has the same class or an instance of the
240      * class in each position of the argument list.
241      *
242      * @param arguments list of classes which the constructor must have
243      * as parameters, or subclasses of the arguments. A <tt>null</tt>
244      * argument index indicates that any type is allowed (equivalent
245      * to specifying <tt>Object.class</tt> in the argument). If
246      * the array is <tt>null</tt>, then the argument list will only
247      * match the default (no-argument) constructor (which is the
248      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
249      * @param protection a bitwise ORed value containing one or more of the
250      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
251      * or <tt>PRIVATE</tt>.
252      * @see #assertHasConstructor( String, Object, Class[] )
253      * @see #assertHasConstructor( String, Class, Class[], int )
254      * @see #assertHasSameConstructor( String, Object, Class[], int )
255      */

256     public static void assertHasConstructor( String JavaDoc message,
257             Object JavaDoc o, Class JavaDoc[] arguments, int protection )
258     {
259         Assert.assertNotNull( "Null object arguments.", o );
260         assertHasConstructor( message, o.getClass(), arguments, protection );
261     }
262     
263     
264     /**
265      * Ensures that there exists a constructor in class <tt>c</tt> that
266      * accepts the same number of arguments given in <tt>arguments</tt>,
267      * and that the constructor has the same class or an instance of the
268      * class in each position of the argument list.
269      *
270      * @param arguments list of classes which the constructor must have
271      * as parameters, or subclasses of the arguments. A <tt>null</tt>
272      * argument index indicates that any type is allowed (equivalent
273      * to specifying <tt>Object.class</tt> in the argument). If
274      * the array is <tt>null</tt>, then the argument list will only
275      * match the default (no-argument) constructor (which is the
276      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
277      * @param protection a bitwise ORed value containing one or more of the
278      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
279      * or <tt>PRIVATE</tt>.
280      * @see #assertHasConstructor( Object, Class[] )
281      * @see #assertHasConstructor( Class, Class[], int )
282      * @see #assertHasSameConstructor( Object, Class[], int )
283      */

284     public static void assertHasConstructor(
285             Object JavaDoc o, Class JavaDoc[] arguments, int protection )
286     {
287         Assert.assertNotNull( "Null object arguments.", o );
288         assertHasConstructor( o.getClass(), arguments, protection );
289     }
290     
291     
292     /**
293      * Ensures that there exists a public constructor in class <tt>c</tt> that
294      * accepts the same number of arguments given in <tt>arguments</tt>,
295      * and that the constructor has the same class or an instance of the
296      * class in each position of the argument list.
297      *
298      * @param arguments list of classes which the constructor must have
299      * as parameters, or subclasses of the arguments. A <tt>null</tt>
300      * argument index indicates that any type is allowed (equivalent
301      * to specifying <tt>Object.class</tt> in the argument). If
302      * the array is <tt>null</tt>, then the argument list will only
303      * match the default (no-argument) constructor (which is the
304      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
305      * @see #assertHasConstructor( String, Object, Class[], int )
306      * @see #assertHasConstructor( String, Class, Class[] )
307      * @see #assertHasSameConstructor( String, Object, Class[] )
308      */

309     public static void assertHasConstructor( String JavaDoc message,
310             Object JavaDoc o, Class JavaDoc[] arguments )
311     {
312         Assert.assertNotNull( "Null object arguments.", o );
313         assertHasConstructor( message, o.getClass(), arguments );
314     }
315     
316     
317     /**
318      * Ensures that there exists a public constructor in class <tt>c</tt> that
319      * accepts the same number of arguments given in <tt>arguments</tt>,
320      * and that the constructor has the same class or an instance of the
321      * class in each position of the argument list.
322      *
323      * @param arguments list of classes which the constructor must have
324      * as parameters, or subclasses of the arguments. A <tt>null</tt>
325      * argument index indicates that any type is allowed (equivalent
326      * to specifying <tt>Object.class</tt> in the argument). If
327      * the array is <tt>null</tt>, then the argument list will only
328      * match the default (no-argument) constructor (which is the
329      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
330      * @see #assertHasConstructor( Object, Class[], int )
331      * @see #assertHasConstructor( Class, Class[] )
332      * @see #assertHasSameConstructor( Object, Class[] )
333      */

334     public static void assertHasConstructor(
335             Object JavaDoc o, Class JavaDoc[] arguments )
336     {
337         Assert.assertNotNull( "Null object arguments.", o );
338         assertHasConstructor( o.getClass(), arguments );
339     }
340     
341     
342     //-------------------------------------------------------------------------
343

344     
345     /**
346      * Ensures that there exists a constructor in class <tt>c</tt> that
347      * accepts the same number of arguments given in <tt>arguments</tt>,
348      * and that the constructor has the same class or an instance of the
349      * class in each position of the argument list.
350      *
351      * @param arguments list of classes which the constructor must have
352      * as parameters, or subclasses of the arguments. A <tt>null</tt>
353      * argument index indicates that any type is allowed (equivalent
354      * to specifying <tt>Object.class</tt> in the argument). If
355      * the array is <tt>null</tt>, then the argument list will only
356      * match the default (no-argument) constructor (which is the
357      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
358      * @param protection a bitwise ORed value containing one or more of the
359      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
360      * or <tt>PRIVATE</tt>.
361      * @see #assertHasSameConstructor( String, Class, Class[] )
362      * @see #assertHasSameConstructor( String, Object, Class[], int )
363      * @see #assertHasConstructor( String, Class, Class[], int )
364      */

365     public static void assertHasSameConstructor( String JavaDoc message,
366             Class JavaDoc c, Class JavaDoc[] arguments, int protection )
367     {
368         Assert.assertNotNull( "Null class argument.", c );
369         
370         Assert.assertNotNull( message,
371             getSameConstructor( c, arguments, protection ) );
372     }
373     
374     
375     /**
376      * Ensures that there exists a constructor in class <tt>c</tt> that
377      * accepts the same number of arguments given in <tt>arguments</tt>,
378      * and that the constructor has the same class or an instance of the
379      * class in each position of the argument list.
380      *
381      * @param arguments list of classes which the constructor must have
382      * as parameters, or subclasses of the arguments. A <tt>null</tt>
383      * argument index indicates that any type is allowed (equivalent
384      * to specifying <tt>Object.class</tt> in the argument). If
385      * the array is <tt>null</tt>, then the argument list will only
386      * match the default (no-argument) constructor (which is the
387      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
388      * @param protection a bitwise ORed value containing one or more of the
389      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
390      * or <tt>PRIVATE</tt>.
391      * @see #assertHasSameConstructor( Class, Class[] )
392      * @see #assertHasSameConstructor( Object, Class[], int )
393      * @see #assertHasConstructor( Class, Class[], int )
394      */

395     public static void assertHasSameConstructor(
396             Class JavaDoc c, Class JavaDoc[] arguments, int protection )
397     {
398         Assert.assertNotNull( "Null class argument.", c );
399         
400         Assert.assertNotNull( getSameConstructor( c, arguments, protection ) );
401     }
402     
403     
404     /**
405      * Ensures that there exists a public constructor in class <tt>c</tt> that
406      * accepts the same number of arguments given in <tt>arguments</tt>,
407      * and that the constructor has the same class or an instance of the
408      * class in each position of the argument list.
409      *
410      * @param arguments list of classes which the constructor must have
411      * as parameters, or subclasses of the arguments. A <tt>null</tt>
412      * argument index indicates that any type is allowed (equivalent
413      * to specifying <tt>Object.class</tt> in the argument). If
414      * the array is <tt>null</tt>, then the argument list will only
415      * match the default (no-argument) constructor (which is the
416      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
417      * @see #assertHasSameConstructor( String, Class, Class[], int )
418      * @see #assertHasSameConstructor( String, Object, Class[] )
419      * @see #assertHasConstructor( String, Class, Class[] )
420      */

421     public static void assertHasSameConstructor( String JavaDoc message,
422             Class JavaDoc c, Class JavaDoc[] arguments )
423     {
424         assertHasSameConstructor( message, c, arguments, PUBLIC );
425     }
426     
427     
428     /**
429      * Ensures that there exists a public constructor in class <tt>c</tt> that
430      * accepts the same number of arguments given in <tt>arguments</tt>,
431      * and that the constructor has the same class or an instance of the
432      * class in each position of the argument list.
433      *
434      * @param arguments list of classes which the constructor must have
435      * as parameters, or subclasses of the arguments. A <tt>null</tt>
436      * argument index indicates that any type is allowed (equivalent
437      * to specifying <tt>Object.class</tt> in the argument). If
438      * the array is <tt>null</tt>, then the argument list will only
439      * match the default (no-argument) constructor (which is the
440      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
441      * @see #assertHasSameConstructor( Class, Class[], int )
442      * @see #assertHasSameConstructor( Object, Class[] )
443      * @see #assertHasConstructor( Class, Class[] )
444      */

445     public static void assertHasSameConstructor(
446             Class JavaDoc c, Class JavaDoc[] arguments )
447     {
448         assertHasSameConstructor( c, arguments, PUBLIC );
449     }
450     
451     
452     /**
453      * Ensures that there exists a constructor in class <tt>c</tt> that
454      * accepts the same number of arguments given in <tt>arguments</tt>,
455      * and that the constructor has the same class or an instance of the
456      * class in each position of the argument list.
457      *
458      * @param arguments list of classes which the constructor must have
459      * as parameters, or subclasses of the arguments. A <tt>null</tt>
460      * argument index indicates that any type is allowed (equivalent
461      * to specifying <tt>Object.class</tt> in the argument). If
462      * the array is <tt>null</tt>, then the argument list will only
463      * match the default (no-argument) constructor (which is the
464      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
465      * @param protection a bitwise ORed value containing one or more of the
466      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
467      * or <tt>PRIVATE</tt>.
468      * @see #assertHasSameConstructor( String, Object, Class[] )
469      * @see #assertHasSameConstructor( String, Class, Class[], int )
470      * @see #assertHasConstructor( String, Object, Class[], int )
471      */

472     public static void assertHasSameConstructor( String JavaDoc message,
473             Object JavaDoc o, Class JavaDoc[] arguments, int protection )
474     {
475         Assert.assertNotNull( "Null object arguments.", o );
476         assertHasSameConstructor( message, o.getClass(), arguments,
477             protection );
478     }
479     
480     
481     /**
482      * Ensures that there exists a constructor in class <tt>c</tt> that
483      * accepts the same number of arguments given in <tt>arguments</tt>,
484      * and that the constructor has the same class or an instance of the
485      * class in each position of the argument list.
486      *
487      * @param arguments list of classes which the constructor must have
488      * as parameters, or subclasses of the arguments. A <tt>null</tt>
489      * argument index indicates that any type is allowed (equivalent
490      * to specifying <tt>Object.class</tt> in the argument). If
491      * the array is <tt>null</tt>, then the argument list will only
492      * match the default (no-argument) constructor (which is the
493      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
494      * @param protection a bitwise ORed value containing one or more of the
495      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
496      * or <tt>PRIVATE</tt>.
497      * @see #assertHasSameConstructor( Object, Class[] )
498      * @see #assertHasSameConstructor( Class, Class[], int )
499      * @see #assertHasConstructor( Object, Class[], int )
500      */

501     public static void assertHasSameConstructor(
502             Object JavaDoc o, Class JavaDoc[] arguments, int protection )
503     {
504         Assert.assertNotNull( "Null object arguments.", o );
505         assertHasSameConstructor( o.getClass(), arguments, protection );
506     }
507     
508     
509     /**
510      * Ensures that there exists a public constructor in class <tt>c</tt> that
511      * accepts the same number of arguments given in <tt>arguments</tt>,
512      * and that the constructor has the same class or an instance of the
513      * class in each position of the argument list.
514      *
515      * @param arguments list of classes which the constructor must have
516      * as parameters, or subclasses of the arguments. A <tt>null</tt>
517      * argument index indicates that any type is allowed (equivalent
518      * to specifying <tt>Object.class</tt> in the argument). If
519      * the array is <tt>null</tt>, then the argument list will only
520      * match the default (no-argument) constructor (which is the
521      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
522      * @see #assertHasSameConstructor( String, Object, Class[], int )
523      * @see #assertHasSameConstructor( String, Class, Class[] )
524      * @see #assertHasConstructor( String, Object, Class[] )
525      */

526     public static void assertHasSameConstructor( String JavaDoc message,
527             Object JavaDoc o, Class JavaDoc[] arguments )
528     {
529         Assert.assertNotNull( "Null object arguments.", o );
530         assertHasSameConstructor( message, o.getClass(), arguments );
531     }
532     
533     
534     /**
535      * Ensures that there exists a public constructor in class <tt>c</tt> that
536      * accepts the same number of arguments given in <tt>arguments</tt>,
537      * and that the constructor has the same class or an instance of the
538      * class in each position of the argument list.
539      *
540      * @param arguments list of classes which the constructor must have
541      * as parameters, or subclasses of the arguments. A <tt>null</tt>
542      * argument index indicates that any type is allowed (equivalent
543      * to specifying <tt>Object.class</tt> in the argument). If
544      * the array is <tt>null</tt>, then the argument list will only
545      * match the default (no-argument) constructor (which is the
546      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
547      * @see #assertHasSameConstructor( Object, Class[], int )
548      * @see #assertHasSameConstructor( Class, Class[] )
549      * @see #assertHasConstructor( Object, Class[] )
550      */

551     public static void assertHasSameConstructor(
552             Object JavaDoc o, Class JavaDoc[] arguments )
553     {
554         Assert.assertNotNull( "Null object arguments.", o );
555         assertHasSameConstructor( o.getClass(), arguments );
556     }
557     
558     
559     /**
560      * Retrieves the first constructor in class <tt>c</tt> that
561      * accepts the same number of arguments given in <tt>arguments</tt>,
562      * and that the constructor has the same class or an instance of the
563      * class in each position of the argument list, or <tt>null</tt> if
564      * there is no such constructor.
565      *
566      * @param arguments list of classes which the constructor must have
567      * as parameters, or subclasses of the arguments. A <tt>null</tt>
568      * argument index indicates that any type is allowed (equivalent
569      * to specifying <tt>Object.class</tt> in the argument). If
570      * the array is <tt>null</tt>, then the argument list will only
571      * match the default (no-argument) constructor (which is the
572      * same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
573      * @param protection a bitwise ORed value containing one or more of the
574      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
575      * or <tt>PRIVATE</tt>.
576      * @see #getSameConstructor( Class, Class[], int )
577      */

578     public static Constructor JavaDoc getConstructor( Class JavaDoc c, Class JavaDoc[] arguments,
579             int protection )
580     {
581         Assert.assertNotNull( "Null class argument.", c );
582         Constructor JavaDoc[] cntrs = c.getConstructors();
583         
584         for (int i = 0; i < cntrs.length; ++i)
585         {
586             if ( hasCorrectProtection( cntrs[i], protection )
587                 && isInheritedParameters( cntrs[i], arguments ) )
588             {
589                 return cntrs[i];
590             }
591         }
592         
593         return null;
594     }
595     
596     
597     /**
598      * Retrieves the constructor in class <tt>c</tt> that
599      * accepts the exact argument list given in <tt>arguments</tt>, or
600      * <tt>null</tt> if there is no such constructor.
601      *
602      * @param protection a bitwise ORed value containing one or more of the
603      * constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
604      * or <tt>PRIVATE</tt>.
605      * @see Class#getConstructor( Class[] )
606      */

607     public static Constructor JavaDoc getSameConstructor( Class JavaDoc c, Class JavaDoc[] arguments,
608             int protection )
609     {
610         Assert.assertNotNull( "Null class argument.", c );
611         try
612         {
613             Constructor JavaDoc cntr = c.getConstructor( arguments );
614             if (cntr != null && hasCorrectProtection( cntr, protection ))
615             {
616                 return cntr;
617             }
618         }
619         catch (NoSuchMethodException JavaDoc nsme)
620         {
621             // ignore
622
}
623         return null;
624     }
625     
626     
627     
628     
629     protected static boolean isInheritedParameters( Constructor JavaDoc cntr,
630             Class JavaDoc[] arguments )
631     {
632         Class JavaDoc[] params = cntr.getParameterTypes();
633         if ( arguments == null )
634         {
635             // Avoid NPEs later by returning immediately
636
return ( params.length == 0 );
637         }
638         if ( params.length != arguments.length )
639         {
640             return false;
641         }
642         
643         for (int i = 0; i < params.length; ++i)
644         {
645             // null argument == Object.class == anything
646
if ( arguments[ i ] != null )
647             {
648                 if ( !arguments[ i ].isAssignableFrom( params[ i ] ) )
649                 {
650                     return false;
651                 }
652             }
653         }
654         
655         // every argument passed.
656
return true;
657     }
658     
659     
660     protected static boolean hasCorrectProtection( Constructor JavaDoc cntr,
661             int protection )
662     {
663         int modifiers = cntr.getModifiers();
664         boolean isPublic = Modifier.isPublic( modifiers );
665         if ( (protection & PUBLIC) != 0 && isPublic )
666         {
667             return true;
668         }
669         
670         boolean isProtected = Modifier.isProtected( modifiers );
671         if ( (protection & PROTECTED) != 0 && isProtected )
672         {
673             return true;
674         }
675         
676         boolean isPrivate = Modifier.isPrivate( modifiers );
677         if ( (protection & PRIVATE) != 0 && isPrivate )
678         {
679             return true;
680         }
681         
682         if ( (protection & PACKAGE) != 0
683             && !isPublic
684             && !isProtected
685             && !isPrivate )
686         {
687             return true;
688         }
689         
690         return false;
691     }
692 }
693
694
Popular Tags