KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > introspector > Ctors


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.introspector;
4
5 import java.lang.reflect.Constructor JavaDoc;
6
7 /**
8  * Constructors map collection.
9  */

10 class Ctors {
11
12     Constructor JavaDoc[] allCtors;
13     Class JavaDoc[][] allArgs;
14     boolean locked = false;
15     Constructor JavaDoc defaultCtor;
16
17     void addCtors(Constructor JavaDoc[] ctors) {
18         if (locked == true) {
19             throw new IllegalStateException JavaDoc("Constructor introspection is already finished.");
20         }
21         allCtors = ctors;
22         allArgs = new Class JavaDoc[allCtors.length][];
23         for (int i = 0; i < ctors.length; i++) {
24             Constructor JavaDoc ctor = ctors[i];
25             allArgs[i] = ctor.getParameterTypes();
26             if (allArgs[i].length == 0) {
27                 defaultCtor = ctor;
28             }
29         }
30     }
31
32     void lock() {
33         locked = true;
34     }
35
36
37     // ---------------------------------------------------------------- get
38

39     Constructor JavaDoc getDefaultCtor() {
40         return defaultCtor;
41     }
42
43     Constructor JavaDoc getCtor(Class JavaDoc[] args) {
44         ctors:
45         for (int i = 0; i < allArgs.length; i++) {
46             Class JavaDoc[] arg = allArgs[i];
47             if (arg.length != args.length) {
48                 continue;
49             }
50             for (int j = 0; j < arg.length; j++) {
51                 if (arg[j] != args[j]) {
52                     continue ctors;
53                 }
54             }
55             return allCtors[i];
56         }
57         return null;
58     }
59
60     int getCount() {
61         return allCtors.length;
62     }
63
64     Constructor JavaDoc[] getAllCtors() {
65         return allCtors;
66     }
67
68 }
69
Popular Tags