KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > Signature


1 package alt.jiapi.reflect;
2
3 /**
4  * This class represents a method signature.
5  */

6 public class Signature {
7     private String JavaDoc descriptor;
8     private String JavaDoc returnType;
9     private String JavaDoc[] params;
10     
11     /**
12      * Constructor for Signature.
13      *
14      * @param returnType return type of the method. Return type is
15      * is given in simple form like 'int' or 'java.lang.Object'
16      * @param params Parameters of method, in simple form.
17      */

18     public Signature(String JavaDoc returnType, String JavaDoc[] params) {
19         if (params == null) {
20             throw new IllegalArgumentException JavaDoc("params may not be null");
21         }
22
23         // Remove ' ' characters
24
for (int i = 0; i < params.length; i++) {
25             String JavaDoc s = params[i].trim();
26             int idx = s.indexOf(' ');
27
28             if (idx != -1) {
29                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
30                 for (int j = 0; j < s.length(); j++) {
31                     if (s.charAt(j) != ' ') {
32                         sb.append(s.charAt(j));
33                     }
34                 }
35
36                 params[i] = sb.toString();
37             }
38         }
39
40
41         this.returnType = returnType;
42         this.params = params;
43         this.descriptor = TypeHelper.signatureToDescriptor(this);
44     }
45     
46     /**
47      * Constructor for Signature.
48      *
49      * @param descriptor Methods descriptor in internal format
50      */

51     public Signature(String JavaDoc descriptor) {
52         try {
53             this.descriptor = descriptor;
54         
55             int idx = descriptor.lastIndexOf(')');
56             this.returnType =
57                 TypeHelper.descriptorToType(descriptor.substring(idx + 1));
58             
59             this.params =
60                 TypeHelper.descriptorsToTypes(descriptor.substring(1, idx));
61         }
62         catch(RuntimeException JavaDoc e) {
63             System.err.println("Failed to create Signature from " +
64                                descriptor);
65             throw e ;
66         }
67     }
68     
69     /**
70      * Gets the return type in simple form like 'int' or 'java.lang.Object'.
71      */

72     public String JavaDoc getReturnType() {
73         return returnType;
74     }
75     
76     /**
77      * Gets the parameters in simple form like 'int' or 'java.lang.Object'.
78      */

79     public String JavaDoc[] getParameters() {
80         return params;
81     }
82     
83     /**
84      * Get an internal representation of method signature as String.
85      */

86     public String JavaDoc getDescriptor() {
87         return descriptor;
88     }
89
90     public String JavaDoc toString() {
91         return descriptor;
92     }
93
94
95     public boolean equals(Signature s) {
96         // NOTE: we cannot just compare descriptors, since return-type
97
// differences must provide 'true' with signature comparison
98

99         String JavaDoc[] otherParams = s.getParameters();
100         if (params.length != otherParams.length) {
101             return false;
102         }
103         
104         for (int i = 0; i < params.length; i++) {
105             if (!params[i].equals(otherParams[i])) {
106                 return false;
107             }
108         }
109         
110         return true;
111     }
112 }
113
Popular Tags