001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations;
025:
026: import java.lang.reflect.Method;
027: import java.util.Arrays;
028:
029: import org.ow2.easybeans.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.ow2.easybeans.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 final String[] exceptions;
062:
063: /**
064: * Constructor.
065: * @param access the access mode (see {@link org.ow2.easybeans.asm.Opcodes})
066: * @param name the method's name.
067: * @param descriptor the method's descriptor (see
068: * {@link org.ow2.easybeans.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.ow2.easybeans.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: * @return the access mode (see {@link org.ow2.easybeans.asm.Opcodes})
089: */
090: public int getAccess() {
091: return access;
092: }
093:
094: /**
095: * Constructor.
096: * @param m {@link java.lang.reflect.Method} method.
097: */
098: public JMethod(final Method m) {
099: this .name = m.getName();
100: this .descriptor = Type.getMethodDescriptor(m);
101: // TODO: make this ok
102: // this.signature = Type.signature;
103: this .exceptions = null;
104: }
105:
106: /**
107: * Indicates whether some other object is "equal to" this one.
108: * @param obj object to compare
109: * @return true if given object is equals
110: */
111: @Override
112: public boolean equals(final Object obj) {
113: if (obj != null && obj instanceof JMethod) {
114: JMethod other = (JMethod) obj;
115:
116: // same name
117: if (!this .name.equals(other.name)) {
118: return false;
119: }
120:
121: // same descriptor
122: if ((this .descriptor != null)
123: && (!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: // if all tests succeed, return true
134: return true;
135: }
136: return false;
137: }
138:
139: /**
140: * @return a hash code value for the object.
141: */
142: @Override
143: public int hashCode() {
144: return name.hashCode();
145: }
146:
147: /**
148: * @return method descriptor
149: */
150: public String getDescriptor() {
151: return descriptor;
152: }
153:
154: /**
155: * @return method exceptions
156: */
157: public String[] getExceptions() {
158: return exceptions;
159: }
160:
161: /**
162: * @return method name
163: */
164: public String getName() {
165: return name;
166: }
167:
168: /**
169: * @return method signature
170: */
171: public String getSignature() {
172: return signature;
173: }
174:
175: /**
176: * @return string representation
177: */
178: @Override
179: public String toString() {
180: StringBuilder sb = new StringBuilder();
181: // classname
182: sb.append(this .getClass().getName().substring(
183: 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: }
|