001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JMethod.java 47 2006-02-28 10:42:29Z benoitf $
023: * --------------------------------------------------------------------------
024: */package com.bm.ejb3metadata.annotations;
025:
026: import java.lang.reflect.Method;
027: import java.util.Arrays;
028:
029: import org.ejb3unit.asm.Type;
030:
031: /**
032: * This class defines a Method object. It is not based on reflection but allows
033: * to build a JMethod based on java.lang.reflect.Method
034: * @author Florent Benoit
035: */
036: public class JMethod {
037:
038: /**
039: * Name of the method.
040: */
041: private String name = null;
042:
043: /**
044: * Access mode (see {@link org.ejb3unit.asm.Opcodes}).
045: */
046: private int access;
047:
048: /**
049: * Method's descriptor.
050: */
051: private String descriptor = null;
052:
053: /**
054: * Method's signature.
055: */
056: private String signature;
057:
058: /**
059: * Exceptions of the method.
060: */
061: private String[] exceptions;
062:
063: /**
064: * Constructor.
065: * @param access the access mode (see {@link org.ejb3unit.asm.Opcodes})
066: * @param name the method's name.
067: * @param descriptor the method's descriptor (see
068: * {@link org.ejb3unit.asm.Type Type}).
069: * @param signature the method's signature. May be <tt>null</tt> if the
070: * method parameters, return type and exceptions do not use generic
071: * types.
072: * @param exceptions the internal names of the method's exception classes
073: * (see
074: * {@link org.ejb3unit.asm.Type#getInternalName() getInternalName}).
075: * May be <tt>null</tt>.
076: */
077: public JMethod(final int access, final String name,
078: final String descriptor, final String signature,
079: final String[] exceptions) {
080: this .access = access;
081: this .name = name;
082: this .descriptor = descriptor;
083: this .signature = signature;
084: this .exceptions = exceptions;
085: }
086:
087: /**
088: * the access mode (see {@link org.ejb3unit.asm.Opcodes}).
089: * @return the access mode (see {@link org.ejb3unit.asm.Opcodes})
090: */
091: public int getAccess() {
092: return access;
093: }
094:
095: /**
096: * Constructor.
097: * @param m {@link java.lang.reflect.Method} method.
098: */
099: public JMethod(final Method m) {
100: this .name = m.getName();
101: this .descriptor = Type.getMethodDescriptor(m);
102: // FIXME: 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
113: public boolean equals(final Object 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)
124: && (!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: // if all tests succeed, return true
135: return true;
136: }
137: return false;
138: }
139:
140: /**
141: * a hash code value for the object.
142: * @return a hash code value for the object.
143: */
144: @Override
145: public int hashCode() {
146: return name.hashCode();
147: }
148:
149: /**
150: * method descriptor.
151: * @return method descriptor
152: */
153: public String getDescriptor() {
154: return descriptor;
155: }
156:
157: /**
158: * method exceptions.
159: * @return method exceptions
160: */
161: public String[] getExceptions() {
162: return exceptions;
163: }
164:
165: /**
166: * method name.
167: * @return method name
168: */
169: public String getName() {
170: return name;
171: }
172:
173: /**
174: * method signature.
175: * method signature
176: * @return method signature
177: */
178: public String getSignature() {
179: return signature;
180: }
181:
182: /**
183: * string representation.
184: * @return string representation
185: */
186: @Override
187: public String toString() {
188: StringBuilder sb = new StringBuilder();
189: // classname
190: sb.append(this .getClass().getName().substring(
191: 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: }
|