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 com.bm.ejb3metadata.annotations;
27
28 import java.lang.reflect.Method;
29 import java.util.Arrays;
30
31 import org.hibernate.repackage.cglib.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 name = null;
44
45 /**
46 * Access mode (see {@link org.ejb3unit.asm.Opcodes}).
47 */
48 private int access;
49
50 /**
51 * Method's descriptor.
52 */
53 private String descriptor = null;
54
55 /**
56 * Method's signature.
57 */
58 private String signature;
59
60 /**
61 * Exceptions of the method.
62 */
63 private String[] exceptions;
64
65 /**
66 * Constructor.
67 * @param access the access mode (see {@link org.ejb3unit.asm.Opcodes})
68 * @param name the method's name.
69 * @param descriptor the method's descriptor (see
70 * {@link org.ejb3unit.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.ejb3unit.asm.Type#getInternalName() getInternalName}).
77 * May be <tt>null</tt>.
78 */
79 public JMethod(final int access, final String name, final String descriptor, final String signature,
80 final String[] 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 * the access mode (see {@link org.ejb3unit.asm.Opcodes}).
90 * @return the access mode (see {@link org.ejb3unit.asm.Opcodes})
91 */
92 public int getAccess() {
93 return access;
94 }
95
96 /**
97 * Constructor.
98 * @param m {@link java.lang.reflect.Method} method.
99 */
100 public JMethod(final Method m) {
101 this.name = m.getName();
102 this.descriptor = Type.getMethodDescriptor(m);
103 // FIXME: make this ok
104 // this.signature = Type.signature;
105 // this.exceptions = exceptions;
106 }
107
108 /**
109 * Indicates whether some other object is "equal to" this one.
110 * @param obj object to compare
111 * @return true if given object is equals
112 */
113 @Override
114 public boolean equals(final Object obj) {
115 if (obj != null && obj instanceof JMethod) {
116 JMethod other = (JMethod) obj;
117
118 // same name
119 if (!this.name.equals(other.name)) {
120 return false;
121 }
122
123 // same descriptor
124 if ((this.descriptor != null) && (!this.descriptor.equals(other.descriptor))) {
125 return false;
126 }
127
128 // Don't check signature (which include generics information)
129 // For example void method(List<Integer>) and
130 // void method(List<String>)
131 // and even if signature is different, they have same erasure
132 // This is not allowed, so don't need to check signature information.
133
134
135 // if all tests succeed, return true
136 return true;
137 }
138 return false;
139 }
140
141 /**
142 * a hash code value for the object.
143 * @return a hash code value for the object.
144 */
145 @Override
146 public int hashCode() {
147 return name.hashCode();
148 }
149
150 /**
151 * method descriptor.
152 * @return method descriptor
153 */
154 public String getDescriptor() {
155 return descriptor;
156 }
157
158 /**
159 * method exceptions.
160 * @return method exceptions
161 */
162 public String[] getExceptions() {
163 return exceptions;
164 }
165
166 /**
167 * method name.
168 * @return method name
169 */
170 public String getName() {
171 return name;
172 }
173
174 /**
175 * method signature.
176 * method signature
177 * @return method signature
178 */
179 public String getSignature() {
180 return signature;
181 }
182
183 /**
184 * string representation.
185 * @return string representation
186 */
187 @Override
188 public String toString() {
189 StringBuilder sb = new StringBuilder();
190 // classname
191 sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1));
192
193 // name
194 sb.append("[name=");
195 sb.append(name);
196
197 // access
198 sb.append(", access=");
199 sb.append(access);
200
201 // descriptor
202 if (descriptor != null) {
203 sb.append(", descriptor=");
204 sb.append(descriptor);
205 }
206
207 // signature
208 if (signature != null) {
209 sb.append(", signature=");
210 sb.append(signature);
211 }
212
213 // exceptions
214 if (exceptions != null) {
215 sb.append(", exceptions=");
216 sb.append(Arrays.asList(exceptions));
217 }
218 sb.append("]");
219 return sb.toString();
220 }
221 }