001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.drools.asm.commons;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import org.drools.asm.Type;
035:
036: /**
037: * A named method descriptor.
038: *
039: * @author Juozas Baliuka
040: * @author Chris Nokleberg
041: * @author Eric Bruneton
042: */
043: public class Method {
044:
045: /**
046: * The method name.
047: */
048: private final String name;
049:
050: /**
051: * The method descriptor.
052: */
053: private final String desc;
054:
055: /**
056: * Maps primitive Java type names to their descriptors.
057: */
058: private final static Map DESCRIPTORS;
059:
060: static {
061: DESCRIPTORS = new HashMap();
062: Method.DESCRIPTORS.put("void", "V");
063: Method.DESCRIPTORS.put("byte", "B");
064: Method.DESCRIPTORS.put("char", "C");
065: Method.DESCRIPTORS.put("double", "D");
066: Method.DESCRIPTORS.put("float", "F");
067: Method.DESCRIPTORS.put("int", "I");
068: Method.DESCRIPTORS.put("long", "J");
069: Method.DESCRIPTORS.put("short", "S");
070: Method.DESCRIPTORS.put("boolean", "Z");
071: }
072:
073: /**
074: * Creates a new {@link Method}.
075: *
076: * @param name the method's name.
077: * @param desc the method's descriptor.
078: */
079: public Method(final String name, final String desc) {
080: this .name = name;
081: this .desc = desc;
082: }
083:
084: /**
085: * Creates a new {@link Method}.
086: *
087: * @param name the method's name.
088: * @param returnType the method's return type.
089: * @param argumentTypes the method's argument types.
090: */
091: public Method(final String name, final Type returnType,
092: final Type[] argumentTypes) {
093: this (name, Type.getMethodDescriptor(returnType, argumentTypes));
094: }
095:
096: /**
097: * Returns a {@link Method} corresponding to the given Java method
098: * declaration.
099: *
100: * @param method a Java method declaration, without argument names, of the
101: * form "returnType name (argumentType1, ... argumentTypeN)", where
102: * the types are in plain Java (e.g. "int", "float",
103: * "java.util.List", ...).
104: * @return a {@link Method} corresponding to the given Java method
105: * declaration.
106: * @throws IllegalArgumentException if <code>method</code> could not get
107: * parsed.
108: */
109: public static Method getMethod(final String method)
110: throws IllegalArgumentException {
111: final int space = method.indexOf(' ');
112: int start = method.indexOf('(', space) + 1;
113: final int end = method.indexOf(')', start);
114: if (space == -1 || start == -1 || end == -1) {
115: throw new IllegalArgumentException();
116: }
117: // TODO: Check validity of returnType, methodName and arguments.
118: final String returnType = method.substring(0, space);
119: final String methodName = method
120: .substring(space + 1, start - 1).trim();
121: final StringBuffer sb = new StringBuffer();
122: sb.append('(');
123: int p;
124: do {
125: p = method.indexOf(',', start);
126: if (p == -1) {
127: sb.append(map(method.substring(start, end).trim()));
128: } else {
129: sb.append(map(method.substring(start, p).trim()));
130: start = p + 1;
131: }
132: } while (p != -1);
133: sb.append(')');
134: sb.append(map(returnType));
135: return new Method(methodName, sb.toString());
136: }
137:
138: private static String map(final String type) {
139: if (type.equals("")) {
140: return type;
141: }
142:
143: final StringBuffer sb = new StringBuffer();
144: int index = 0;
145: while ((index = type.indexOf("[]", index) + 1) > 0) {
146: sb.append('[');
147: }
148:
149: final String t = type.substring(0, type.length() - sb.length()
150: * 2);
151: final String desc = (String) Method.DESCRIPTORS.get(t);
152: if (desc != null) {
153: sb.append(desc);
154: } else {
155: sb.append('L');
156: if (t.indexOf('.') < 0) {
157: sb.append("java/lang/" + t);
158: } else {
159: sb.append(t.replace('.', '/'));
160: }
161: sb.append(';');
162: }
163: return sb.toString();
164: }
165:
166: /**
167: * Returns the name of the method described by this object.
168: *
169: * @return the name of the method described by this object.
170: */
171: public String getName() {
172: return this .name;
173: }
174:
175: /**
176: * Returns the descriptor of the method described by this object.
177: *
178: * @return the descriptor of the method described by this object.
179: */
180: public String getDescriptor() {
181: return this .desc;
182: }
183:
184: /**
185: * Returns the return type of the method described by this object.
186: *
187: * @return the return type of the method described by this object.
188: */
189: public Type getReturnType() {
190: return Type.getReturnType(this .desc);
191: }
192:
193: /**
194: * Returns the argument types of the method described by this object.
195: *
196: * @return the argument types of the method described by this object.
197: */
198: public Type[] getArgumentTypes() {
199: return Type.getArgumentTypes(this .desc);
200: }
201:
202: public String toString() {
203: return this .name + this .desc;
204: }
205:
206: public boolean equals(final Object o) {
207: if (!(o instanceof Method)) {
208: return false;
209: }
210: final Method other = (Method) o;
211: return this .name.equals(other.name)
212: && this .desc.equals(other.desc);
213: }
214:
215: public int hashCode() {
216: return this.name.hashCode() ^ this.desc.hashCode();
217: }
218: }
|