Source Code Cross Referenced for AssertConstructor.java in  » Test-Coverage » GroboUtils » net » sourceforge » groboutils » junit » v1 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Test Coverage » GroboUtils » net.sourceforge.groboutils.junit.v1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)AssertConstructor.java
003:         *
004:         * Copyright (C) 2002-2003 Matt Albrecht
005:         * groboclown@users.sourceforge.net
006:         * http://groboutils.sourceforge.net
007:         *
008:         *  Permission is hereby granted, free of charge, to any person obtaining a
009:         *  copy of this software and associated documentation files (the "Software"),
010:         *  to deal in the Software without restriction, including without limitation
011:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
012:         *  and/or sell copies of the Software, and to permit persons to whom the 
013:         *  Software is furnished to do so, subject to the following conditions:
014:         *
015:         *  The above copyright notice and this permission notice shall be included in 
016:         *  all copies or substantial portions of the Software. 
017:         *
018:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
019:         *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
020:         *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
021:         *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
022:         *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
023:         *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
024:         *  DEALINGS IN THE SOFTWARE.
025:         */
026:
027:        package net.sourceforge.groboutils.junit.v1;
028:
029:        import junit.framework.AssertionFailedError;
030:        import junit.framework.Assert;
031:
032:        import java.lang.reflect.Constructor;
033:        import java.lang.reflect.Modifier;
034:
035:        /**
036:         * A set of assert methods that test a class for implementation of specific
037:         * constructor types.
038:         *
039:         * @author     Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040:         * @version    $Date: 2003/02/10 22:52:19 $
041:         * @since      July 21, 2002
042:         */
043:        public class AssertConstructor {
044:            /**
045:             * Bit-mask that allows for the constructor to have public access.
046:             */
047:            public static final int PUBLIC = 0x01;
048:
049:            /**
050:             * Bit-mask that allows for the constructor to have protected access.
051:             */
052:            public static final int PROTECTED = 0x02;
053:
054:            /**
055:             * Bit-mask that allows for the constructor to have package-private access.
056:             */
057:            public static final int PACKAGE = 0x04;
058:
059:            /**
060:             * Bit-mask that allows for the constructor to have private access.
061:             */
062:            public static final int PRIVATE = 0x08;
063:
064:            /**
065:             * Bit-mask that allows the constructor to have any protection.
066:             */
067:            public static final int ANY_PROTECTION = PUBLIC | PROTECTED
068:                    | PACKAGE | PRIVATE;
069:
070:            /**
071:             * Ensures that the class <tt>c</tt> has a default (no-arg), public
072:             * constructor.
073:             *
074:             * @param message message that describes what failed if the assertion
075:             *      fails.
076:             * @param c the class check for a constructor.
077:             */
078:            public static void assertHasDefaultConstructor(String message,
079:                    Class c) {
080:                assertHasSameConstructor(message, c, new Class[0]);
081:            }
082:
083:            /**
084:             * Ensures that the class <tt>c</tt> has a default (no-arg), public
085:             * constructor.
086:             *
087:             * @param c the class check for a constructor.
088:             */
089:            public static void assertHasDefaultConstructor(Class c) {
090:                assertHasSameConstructor(c, new Class[0]);
091:            }
092:
093:            /**
094:             * Ensures that the class for object <tt>o</tt> has a default (no-arg),
095:             * public constructor.
096:             *
097:             * @param message message that describes what failed if the assertion
098:             *      fails.
099:             * @param o the object to check the class's constructor.
100:             */
101:            public static void assertHasDefaultConstructor(String message,
102:                    Object o) {
103:                assertHasSameConstructor(message, o, new Class[0]);
104:            }
105:
106:            /**
107:             * Ensures that the class for object <tt>o</tt> has a default (no-arg),
108:             * public constructor.
109:             *
110:             * @param o the object to check the class's constructor.
111:             */
112:            public static void assertHasDefaultConstructor(Object o) {
113:                assertHasSameConstructor(o, new Class[0]);
114:            }
115:
116:            /**
117:             * Ensures that there exists a constructor in class <tt>c</tt> that
118:             * accepts the same number of arguments given in <tt>arguments</tt>,
119:             * and that the constructor has the same class or an instance of the
120:             * class in each position of the argument list.
121:             *
122:             * @param message message that describes what failed if the assertion
123:             *      fails.
124:             * @param c the class check for a constructor.
125:             * @param arguments list of classes which the constructor must have
126:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
127:             *      argument index indicates that any type is allowed (equivalent
128:             *      to specifying <tt>Object.class</tt> in the argument).  If
129:             *      the array is <tt>null</tt>, then the argument list will only
130:             *      match the default (no-argument) constructor (which is the
131:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
132:             * @param protection a bitwise ORed value containing one or more of the
133:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
134:             *      or <tt>PRIVATE</tt>.
135:             * @see #assertHasConstructor( String, Class, Class[] )
136:             * @see #assertHasConstructor( String, Object, Class[], int )
137:             * @see #assertHasSameConstructor( String, Class, Class[], int )
138:             */
139:            public static void assertHasConstructor(String message, Class c,
140:                    Class[] arguments, int protection) {
141:                Assert.assertNotNull("Null class argument.", c);
142:
143:                Assert.assertNotNull(message, getConstructor(c, arguments,
144:                        protection));
145:            }
146:
147:            /**
148:             * Ensures that there exists a constructor in class <tt>c</tt> that
149:             * accepts the same number of arguments given in <tt>arguments</tt>,
150:             * and that the constructor has the same class or an instance of the
151:             * class in each position of the argument list.
152:             *
153:             * @param arguments list of classes which the constructor must have
154:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
155:             *      argument index indicates that any type is allowed (equivalent
156:             *      to specifying <tt>Object.class</tt> in the argument).  If
157:             *      the array is <tt>null</tt>, then the argument list will only
158:             *      match the default (no-argument) constructor (which is the
159:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
160:             * @param protection a bitwise ORed value containing one or more of the
161:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
162:             *      or <tt>PRIVATE</tt>.
163:             * @see #assertHasConstructor( Class, Class[] )
164:             * @see #assertHasConstructor( Object, Class[], int )
165:             * @see #assertHasSameConstructor( Class, Class[], int )
166:             */
167:            public static void assertHasConstructor(Class c, Class[] arguments,
168:                    int protection) {
169:                Assert.assertNotNull("Null class argument.", c);
170:
171:                Assert.assertNotNull(getConstructor(c, arguments, protection));
172:            }
173:
174:            /**
175:             * Ensures that there exists a public constructor in class <tt>c</tt> that
176:             * accepts the same number of arguments given in <tt>arguments</tt>,
177:             * and that the constructor has the same class or an instance of the
178:             * class in each position of the argument list.
179:             *
180:             * @param arguments list of classes which the constructor must have
181:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
182:             *      argument index indicates that any type is allowed (equivalent
183:             *      to specifying <tt>Object.class</tt> in the argument).  If
184:             *      the array is <tt>null</tt>, then the argument list will only
185:             *      match the default (no-argument) constructor (which is the
186:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
187:             * @see #assertHasConstructor( String, Class, Class[], int )
188:             * @see #assertHasConstructor( String, Object, Class[] )
189:             * @see #assertHasSameConstructor( String, Class, Class[] )
190:             */
191:            public static void assertHasConstructor(String message, Class c,
192:                    Class[] arguments) {
193:                assertHasConstructor(message, c, arguments, PUBLIC);
194:            }
195:
196:            /**
197:             * Ensures that there exists a public constructor in class <tt>c</tt> that
198:             * accepts the same number of arguments given in <tt>arguments</tt>,
199:             * and that the constructor has the same class or an instance of the
200:             * class in each position of the argument list.
201:             *
202:             * @param arguments list of classes which the constructor must have
203:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
204:             *      argument index indicates that any type is allowed (equivalent
205:             *      to specifying <tt>Object.class</tt> in the argument).  If
206:             *      the array is <tt>null</tt>, then the argument list will only
207:             *      match the default (no-argument) constructor (which is the
208:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
209:             * @see #assertHasConstructor( Class, Class[], int )
210:             * @see #assertHasConstructor( Object, Class[] )
211:             * @see #assertHasSameConstructor( Class, Class[] )
212:             */
213:            public static void assertHasConstructor(Class c, Class[] arguments) {
214:                assertHasConstructor(c, arguments, PUBLIC);
215:            }
216:
217:            /**
218:             * Ensures that there exists a constructor in class <tt>c</tt> that
219:             * accepts the same number of arguments given in <tt>arguments</tt>,
220:             * and that the constructor has the same class or an instance of the
221:             * class in each position of the argument list.
222:             *
223:             * @param arguments list of classes which the constructor must have
224:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
225:             *      argument index indicates that any type is allowed (equivalent
226:             *      to specifying <tt>Object.class</tt> in the argument).  If
227:             *      the array is <tt>null</tt>, then the argument list will only
228:             *      match the default (no-argument) constructor (which is the
229:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
230:             * @param protection a bitwise ORed value containing one or more of the
231:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
232:             *      or <tt>PRIVATE</tt>.
233:             * @see #assertHasConstructor( String, Object, Class[] )
234:             * @see #assertHasConstructor( String, Class, Class[], int )
235:             * @see #assertHasSameConstructor( String, Object, Class[], int )
236:             */
237:            public static void assertHasConstructor(String message, Object o,
238:                    Class[] arguments, int protection) {
239:                Assert.assertNotNull("Null object arguments.", o);
240:                assertHasConstructor(message, o.getClass(), arguments,
241:                        protection);
242:            }
243:
244:            /**
245:             * Ensures that there exists a constructor in class <tt>c</tt> that
246:             * accepts the same number of arguments given in <tt>arguments</tt>,
247:             * and that the constructor has the same class or an instance of the
248:             * class in each position of the argument list.
249:             *
250:             * @param arguments list of classes which the constructor must have
251:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
252:             *      argument index indicates that any type is allowed (equivalent
253:             *      to specifying <tt>Object.class</tt> in the argument).  If
254:             *      the array is <tt>null</tt>, then the argument list will only
255:             *      match the default (no-argument) constructor (which is the
256:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
257:             * @param protection a bitwise ORed value containing one or more of the
258:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
259:             *      or <tt>PRIVATE</tt>.
260:             * @see #assertHasConstructor( Object, Class[] )
261:             * @see #assertHasConstructor( Class, Class[], int )
262:             * @see #assertHasSameConstructor( Object, Class[], int )
263:             */
264:            public static void assertHasConstructor(Object o,
265:                    Class[] arguments, int protection) {
266:                Assert.assertNotNull("Null object arguments.", o);
267:                assertHasConstructor(o.getClass(), arguments, protection);
268:            }
269:
270:            /**
271:             * Ensures that there exists a public constructor in class <tt>c</tt> that
272:             * accepts the same number of arguments given in <tt>arguments</tt>,
273:             * and that the constructor has the same class or an instance of the
274:             * class in each position of the argument list.
275:             *
276:             * @param arguments list of classes which the constructor must have
277:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
278:             *      argument index indicates that any type is allowed (equivalent
279:             *      to specifying <tt>Object.class</tt> in the argument).  If
280:             *      the array is <tt>null</tt>, then the argument list will only
281:             *      match the default (no-argument) constructor (which is the
282:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
283:             * @see #assertHasConstructor( String, Object, Class[], int )
284:             * @see #assertHasConstructor( String, Class, Class[] )
285:             * @see #assertHasSameConstructor( String, Object, Class[] )
286:             */
287:            public static void assertHasConstructor(String message, Object o,
288:                    Class[] arguments) {
289:                Assert.assertNotNull("Null object arguments.", o);
290:                assertHasConstructor(message, o.getClass(), arguments);
291:            }
292:
293:            /**
294:             * Ensures that there exists a public constructor in class <tt>c</tt> that
295:             * accepts the same number of arguments given in <tt>arguments</tt>,
296:             * and that the constructor has the same class or an instance of the
297:             * class in each position of the argument list.
298:             *
299:             * @param arguments list of classes which the constructor must have
300:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
301:             *      argument index indicates that any type is allowed (equivalent
302:             *      to specifying <tt>Object.class</tt> in the argument).  If
303:             *      the array is <tt>null</tt>, then the argument list will only
304:             *      match the default (no-argument) constructor (which is the
305:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
306:             * @see #assertHasConstructor( Object, Class[], int )
307:             * @see #assertHasConstructor( Class, Class[] )
308:             * @see #assertHasSameConstructor( Object, Class[] )
309:             */
310:            public static void assertHasConstructor(Object o, Class[] arguments) {
311:                Assert.assertNotNull("Null object arguments.", o);
312:                assertHasConstructor(o.getClass(), arguments);
313:            }
314:
315:            //-------------------------------------------------------------------------
316:
317:            /**
318:             * Ensures that there exists a 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:             * @param protection a bitwise ORed value containing one or more of the
331:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
332:             *      or <tt>PRIVATE</tt>.
333:             * @see #assertHasSameConstructor( String, Class, Class[] )
334:             * @see #assertHasSameConstructor( String, Object, Class[], int )
335:             * @see #assertHasConstructor( String, Class, Class[], int )
336:             */
337:            public static void assertHasSameConstructor(String message,
338:                    Class c, Class[] arguments, int protection) {
339:                Assert.assertNotNull("Null class argument.", c);
340:
341:                Assert.assertNotNull(message, getSameConstructor(c, arguments,
342:                        protection));
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( Class, Class[] )
362:             * @see #assertHasSameConstructor( Object, Class[], int )
363:             * @see #assertHasConstructor( Class, Class[], int )
364:             */
365:            public static void assertHasSameConstructor(Class c,
366:                    Class[] arguments, int protection) {
367:                Assert.assertNotNull("Null class argument.", c);
368:
369:                Assert.assertNotNull(getSameConstructor(c, arguments,
370:                        protection));
371:            }
372:
373:            /**
374:             * Ensures that there exists a public constructor in class <tt>c</tt> that
375:             * accepts the same number of arguments given in <tt>arguments</tt>,
376:             * and that the constructor has the same class or an instance of the
377:             * class in each position of the argument list.
378:             *
379:             * @param arguments list of classes which the constructor must have
380:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
381:             *      argument index indicates that any type is allowed (equivalent
382:             *      to specifying <tt>Object.class</tt> in the argument).  If
383:             *      the array is <tt>null</tt>, then the argument list will only
384:             *      match the default (no-argument) constructor (which is the
385:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
386:             * @see #assertHasSameConstructor( String, Class, Class[], int )
387:             * @see #assertHasSameConstructor( String, Object, Class[] )
388:             * @see #assertHasConstructor( String, Class, Class[] )
389:             */
390:            public static void assertHasSameConstructor(String message,
391:                    Class c, Class[] arguments) {
392:                assertHasSameConstructor(message, c, arguments, PUBLIC);
393:            }
394:
395:            /**
396:             * Ensures that there exists a public constructor in class <tt>c</tt> that
397:             * accepts the same number of arguments given in <tt>arguments</tt>,
398:             * and that the constructor has the same class or an instance of the
399:             * class in each position of the argument list.
400:             *
401:             * @param arguments list of classes which the constructor must have
402:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
403:             *      argument index indicates that any type is allowed (equivalent
404:             *      to specifying <tt>Object.class</tt> in the argument).  If
405:             *      the array is <tt>null</tt>, then the argument list will only
406:             *      match the default (no-argument) constructor (which is the
407:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
408:             * @see #assertHasSameConstructor( Class, Class[], int )
409:             * @see #assertHasSameConstructor( Object, Class[] )
410:             * @see #assertHasConstructor( Class, Class[] )
411:             */
412:            public static void assertHasSameConstructor(Class c,
413:                    Class[] arguments) {
414:                assertHasSameConstructor(c, arguments, PUBLIC);
415:            }
416:
417:            /**
418:             * Ensures that there exists a constructor in class <tt>c</tt> that
419:             * accepts the same number of arguments given in <tt>arguments</tt>,
420:             * and that the constructor has the same class or an instance of the
421:             * class in each position of the argument list.
422:             *
423:             * @param arguments list of classes which the constructor must have
424:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
425:             *      argument index indicates that any type is allowed (equivalent
426:             *      to specifying <tt>Object.class</tt> in the argument).  If
427:             *      the array is <tt>null</tt>, then the argument list will only
428:             *      match the default (no-argument) constructor (which is the
429:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
430:             * @param protection a bitwise ORed value containing one or more of the
431:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
432:             *      or <tt>PRIVATE</tt>.
433:             * @see #assertHasSameConstructor( String, Object, Class[] )
434:             * @see #assertHasSameConstructor( String, Class, Class[], int )
435:             * @see #assertHasConstructor( String, Object, Class[], int )
436:             */
437:            public static void assertHasSameConstructor(String message,
438:                    Object o, Class[] arguments, int protection) {
439:                Assert.assertNotNull("Null object arguments.", o);
440:                assertHasSameConstructor(message, o.getClass(), arguments,
441:                        protection);
442:            }
443:
444:            /**
445:             * Ensures that there exists a constructor in class <tt>c</tt> that
446:             * accepts the same number of arguments given in <tt>arguments</tt>,
447:             * and that the constructor has the same class or an instance of the
448:             * class in each position of the argument list.
449:             *
450:             * @param arguments list of classes which the constructor must have
451:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
452:             *      argument index indicates that any type is allowed (equivalent
453:             *      to specifying <tt>Object.class</tt> in the argument).  If
454:             *      the array is <tt>null</tt>, then the argument list will only
455:             *      match the default (no-argument) constructor (which is the
456:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
457:             * @param protection a bitwise ORed value containing one or more of the
458:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
459:             *      or <tt>PRIVATE</tt>.
460:             * @see #assertHasSameConstructor( Object, Class[] )
461:             * @see #assertHasSameConstructor( Class, Class[], int )
462:             * @see #assertHasConstructor( Object, Class[], int )
463:             */
464:            public static void assertHasSameConstructor(Object o,
465:                    Class[] arguments, int protection) {
466:                Assert.assertNotNull("Null object arguments.", o);
467:                assertHasSameConstructor(o.getClass(), arguments, protection);
468:            }
469:
470:            /**
471:             * Ensures that there exists a public constructor in class <tt>c</tt> that
472:             * accepts the same number of arguments given in <tt>arguments</tt>,
473:             * and that the constructor has the same class or an instance of the
474:             * class in each position of the argument list.
475:             *
476:             * @param arguments list of classes which the constructor must have
477:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
478:             *      argument index indicates that any type is allowed (equivalent
479:             *      to specifying <tt>Object.class</tt> in the argument).  If
480:             *      the array is <tt>null</tt>, then the argument list will only
481:             *      match the default (no-argument) constructor (which is the
482:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
483:             * @see #assertHasSameConstructor( String, Object, Class[], int )
484:             * @see #assertHasSameConstructor( String, Class, Class[] )
485:             * @see #assertHasConstructor( String, Object, Class[] )
486:             */
487:            public static void assertHasSameConstructor(String message,
488:                    Object o, Class[] arguments) {
489:                Assert.assertNotNull("Null object arguments.", o);
490:                assertHasSameConstructor(message, o.getClass(), arguments);
491:            }
492:
493:            /**
494:             * Ensures that there exists a public constructor in class <tt>c</tt> that
495:             * accepts the same number of arguments given in <tt>arguments</tt>,
496:             * and that the constructor has the same class or an instance of the
497:             * class in each position of the argument list.
498:             *
499:             * @param arguments list of classes which the constructor must have
500:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
501:             *      argument index indicates that any type is allowed (equivalent
502:             *      to specifying <tt>Object.class</tt> in the argument).  If
503:             *      the array is <tt>null</tt>, then the argument list will only
504:             *      match the default (no-argument) constructor (which is the
505:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
506:             * @see #assertHasSameConstructor( Object, Class[], int )
507:             * @see #assertHasSameConstructor( Class, Class[] )
508:             * @see #assertHasConstructor( Object, Class[] )
509:             */
510:            public static void assertHasSameConstructor(Object o,
511:                    Class[] arguments) {
512:                Assert.assertNotNull("Null object arguments.", o);
513:                assertHasSameConstructor(o.getClass(), arguments);
514:            }
515:
516:            /**
517:             * Retrieves the first constructor in class <tt>c</tt> that
518:             * accepts the same number of arguments given in <tt>arguments</tt>,
519:             * and that the constructor has the same class or an instance of the
520:             * class in each position of the argument list, or <tt>null</tt> if
521:             * there is no such constructor.
522:             *
523:             * @param arguments list of classes which the constructor must have
524:             *      as parameters, or subclasses of the arguments.  A <tt>null</tt>
525:             *      argument index indicates that any type is allowed (equivalent
526:             *      to specifying <tt>Object.class</tt> in the argument).  If
527:             *      the array is <tt>null</tt>, then the argument list will only
528:             *      match the default (no-argument) constructor (which is the
529:             *      same as setting <tt>arguments</tt> to <tt>new Class[0]</tt>).
530:             * @param protection a bitwise ORed value containing one or more of the
531:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
532:             *      or <tt>PRIVATE</tt>.
533:             * @see #getSameConstructor( Class, Class[], int )
534:             */
535:            public static Constructor getConstructor(Class c,
536:                    Class[] arguments, int protection) {
537:                Assert.assertNotNull("Null class argument.", c);
538:                Constructor[] cntrs = c.getConstructors();
539:
540:                for (int i = 0; i < cntrs.length; ++i) {
541:                    if (hasCorrectProtection(cntrs[i], protection)
542:                            && isInheritedParameters(cntrs[i], arguments)) {
543:                        return cntrs[i];
544:                    }
545:                }
546:
547:                return null;
548:            }
549:
550:            /**
551:             * Retrieves the constructor in class <tt>c</tt> that
552:             * accepts the exact argument list given in <tt>arguments</tt>, or
553:             * <tt>null</tt> if there is no such constructor.
554:             *
555:             * @param protection a bitwise ORed value containing one or more of the
556:             *      constants <tt>PUBLIC</tt>, <tt>PROTECTED</tt>, <tt>PACKAGE</tt>,
557:             *      or <tt>PRIVATE</tt>.
558:             * @see Class#getConstructor( Class[] )
559:             */
560:            public static Constructor getSameConstructor(Class c,
561:                    Class[] arguments, int protection) {
562:                Assert.assertNotNull("Null class argument.", c);
563:                try {
564:                    Constructor cntr = c.getConstructor(arguments);
565:                    if (cntr != null && hasCorrectProtection(cntr, protection)) {
566:                        return cntr;
567:                    }
568:                } catch (NoSuchMethodException nsme) {
569:                    // ignore
570:                }
571:                return null;
572:            }
573:
574:            protected static boolean isInheritedParameters(Constructor cntr,
575:                    Class[] arguments) {
576:                Class[] params = cntr.getParameterTypes();
577:                if (arguments == null) {
578:                    // Avoid NPEs later by returning immediately
579:                    return (params.length == 0);
580:                }
581:                if (params.length != arguments.length) {
582:                    return false;
583:                }
584:
585:                for (int i = 0; i < params.length; ++i) {
586:                    // null argument == Object.class == anything
587:                    if (arguments[i] != null) {
588:                        if (!arguments[i].isAssignableFrom(params[i])) {
589:                            return false;
590:                        }
591:                    }
592:                }
593:
594:                // every argument passed.
595:                return true;
596:            }
597:
598:            protected static boolean hasCorrectProtection(Constructor cntr,
599:                    int protection) {
600:                int modifiers = cntr.getModifiers();
601:                boolean isPublic = Modifier.isPublic(modifiers);
602:                if ((protection & PUBLIC) != 0 && isPublic) {
603:                    return true;
604:                }
605:
606:                boolean isProtected = Modifier.isProtected(modifiers);
607:                if ((protection & PROTECTED) != 0 && isProtected) {
608:                    return true;
609:                }
610:
611:                boolean isPrivate = Modifier.isPrivate(modifiers);
612:                if ((protection & PRIVATE) != 0 && isPrivate) {
613:                    return true;
614:                }
615:
616:                if ((protection & PACKAGE) != 0 && !isPublic && !isProtected
617:                        && !isPrivate) {
618:                    return true;
619:                }
620:
621:                return false;
622:            }
623:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.