KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > JMethod


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JMethod.java 47 2006-02-28 10:42:29Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations;
27
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 import org.objectweb.asm.Type;
32
33 /**
34  * This class defines a Method object. It is not based on reflection but allows
35  * to build a JMethod based on java.lang.reflect.Method
36  * @author Florent Benoit
37  */

38 public class JMethod {
39
40     /**
41      * Name of the method.
42      */

43     private String JavaDoc name = null;
44
45     /**
46      * Access mode (see {@link org.objectweb.asm.Opcodes}).
47      */

48     private int access;
49
50     /**
51      * Method's descriptor.
52      */

53     private String JavaDoc descriptor = null;
54
55     /**
56      * Method's signature.
57      */

58     private String JavaDoc signature;
59
60     /**
61      * Exceptions of the method.
62      */

63     private String JavaDoc[] exceptions;
64
65     /**
66      * Constructor.
67      * @param access the access mode (see {@link org.objectweb.asm.Opcodes})
68      * @param name the method's name.
69      * @param descriptor the method's descriptor (see
70      * {@link org.objectweb.asm.Type Type}).
71      * @param signature the method's signature. May be <tt>null</tt> if the
72      * method parameters, return type and exceptions do not use generic
73      * types.
74      * @param exceptions the internal names of the method's exception classes
75      * (see
76      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}).
77      * May be <tt>null</tt>.
78      */

79     public JMethod(final int access, final String JavaDoc name, final String JavaDoc descriptor, final String JavaDoc signature,
80             final String JavaDoc[] exceptions) {
81         this.access = access;
82         this.name = name;
83         this.descriptor = descriptor;
84         this.signature = signature;
85         this.exceptions = exceptions;
86     }
87
88     /**
89      * @return the access mode (see {@link org.objectweb.asm.Opcodes})
90      */

91     public int getAccess() {
92         return access;
93     }
94
95     /**
96      * Constructor.
97      * @param m {@link java.lang.reflect.Method} method.
98      */

99     public JMethod(final Method JavaDoc m) {
100         this.name = m.getName();
101         this.descriptor = Type.getMethodDescriptor(m);
102         // TODO: make this ok
103
// this.signature = Type.signature;
104
// this.exceptions = exceptions;
105
}
106
107     /**
108      * Indicates whether some other object is "equal to" this one.
109      * @param obj object to compare
110      * @return true if given object is equals
111      */

112     @Override JavaDoc
113     public boolean equals(final Object JavaDoc obj) {
114         if (obj != null && obj instanceof JMethod) {
115             JMethod other = (JMethod) obj;
116
117             // same name
118
if (!this.name.equals(other.name)) {
119                 return false;
120             }
121
122             // same descriptor
123
if ((this.descriptor != null) && (!this.descriptor.equals(other.descriptor))) {
124                 return false;
125             }
126
127             // Don't check signature (which include generics information)
128
// For example void method(List<Integer>) and
129
// void method(List<String>)
130
// and even if signature is different, they have same erasure
131
// This is not allowed, so don't need to check signature information.
132

133
134             // if all tests succeed, return true
135
return true;
136         }
137         return false;
138     }
139
140     /**
141      * @return a hash code value for the object.
142      */

143     @Override JavaDoc
144     public int hashCode() {
145         return name.hashCode();
146     }
147
148     /**
149      * @return method descriptor
150      */

151     public String JavaDoc getDescriptor() {
152         return descriptor;
153     }
154
155     /**
156      * @return method exceptions
157      */

158     public String JavaDoc[] getExceptions() {
159         return exceptions;
160     }
161
162     /**
163      * @return method name
164      */

165     public String JavaDoc getName() {
166         return name;
167     }
168
169     /**
170      * @return method signature
171      */

172     public String JavaDoc getSignature() {
173         return signature;
174     }
175
176     /**
177      * @return string representation
178      */

179     @Override JavaDoc
180     public String JavaDoc toString() {
181         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
182         // classname
183
sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1));
184
185         // name
186
sb.append("[name=");
187         sb.append(name);
188
189         // access
190
sb.append(", access=");
191         sb.append(access);
192
193         // descriptor
194
if (descriptor != null) {
195             sb.append(", descriptor=");
196             sb.append(descriptor);
197         }
198
199         // signature
200
if (signature != null) {
201             sb.append(", signature=");
202             sb.append(signature);
203         }
204
205         // exceptions
206
if (exceptions != null) {
207             sb.append(", exceptions=");
208             sb.append(Arrays.asList(exceptions));
209         }
210         sb.append("]");
211         return sb.toString();
212     }
213 }
214
Popular Tags