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 com.tc.asm.commons;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import com.tc.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: DESCRIPTORS.put("void", "V");
063: DESCRIPTORS.put("byte", "B");
064: DESCRIPTORS.put("char", "C");
065: DESCRIPTORS.put("double", "D");
066: DESCRIPTORS.put("float", "F");
067: DESCRIPTORS.put("int", "I");
068: DESCRIPTORS.put("long", "J");
069: DESCRIPTORS.put("short", "S");
070: 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", ...). Classes of the java.lang package can be
104: * specified by their unqualified name; all other classes names must
105: * be fully qualified.
106: * @return a {@link Method} corresponding to the given Java method
107: * declaration.
108: * @throws IllegalArgumentException if <code>method</code> could not get
109: * parsed.
110: */
111: public static Method getMethod(final String method)
112: throws IllegalArgumentException {
113: return getMethod(method, false);
114: }
115:
116: /**
117: * Returns a {@link Method} corresponding to the given Java method
118: * declaration.
119: *
120: * @param method a Java method declaration, without argument names, of the
121: * form "returnType name (argumentType1, ... argumentTypeN)", where
122: * the types are in plain Java (e.g. "int", "float",
123: * "java.util.List", ...). Classes of the java.lang package may be
124: * specified by their unqualified name, depending on the
125: * defaultPackage argument; all other classes names must be fully
126: * qualified.
127: * @param defaultPackage true if unqualified class names belong to the
128: * default package, or false if they correspond to java.lang classes.
129: * For instance "Object" means "Object" if this option is true, or
130: * "java.lang.Object" otherwise.
131: * @return a {@link Method} corresponding to the given Java method
132: * declaration.
133: * @throws IllegalArgumentException if <code>method</code> could not get
134: * parsed.
135: */
136: public static Method getMethod(final String method,
137: final boolean defaultPackage)
138: throws IllegalArgumentException {
139: int space = method.indexOf(' ');
140: int start = method.indexOf('(', space) + 1;
141: int end = method.indexOf(')', start);
142: if (space == -1 || start == -1 || end == -1) {
143: throw new IllegalArgumentException();
144: }
145: // TODO: Check validity of returnType, methodName and arguments.
146: String returnType = method.substring(0, space);
147: String methodName = method.substring(space + 1, start - 1)
148: .trim();
149: StringBuffer sb = new StringBuffer();
150: sb.append('(');
151: int p;
152: do {
153: String s;
154: p = method.indexOf(',', start);
155: if (p == -1) {
156: s = map(method.substring(start, end).trim(),
157: defaultPackage);
158: } else {
159: s = map(method.substring(start, p).trim(),
160: defaultPackage);
161: start = p + 1;
162: }
163: sb.append(s);
164: } while (p != -1);
165: sb.append(')');
166: sb.append(map(returnType, defaultPackage));
167: return new Method(methodName, sb.toString());
168: }
169:
170: private static String map(final String type,
171: final boolean defaultPackage) {
172: if (type.equals("")) {
173: return type;
174: }
175:
176: StringBuffer sb = new StringBuffer();
177: int index = 0;
178: while ((index = type.indexOf("[]", index) + 1) > 0) {
179: sb.append('[');
180: }
181:
182: String t = type.substring(0, type.length() - sb.length() * 2);
183: String desc = (String) DESCRIPTORS.get(t);
184: if (desc != null) {
185: sb.append(desc);
186: } else {
187: sb.append('L');
188: if (t.indexOf('.') < 0) {
189: if (!defaultPackage) {
190: sb.append("java/lang/");
191: }
192: sb.append(t);
193: } else {
194: sb.append(t.replace('.', '/'));
195: }
196: sb.append(';');
197: }
198: return sb.toString();
199: }
200:
201: /**
202: * Returns the name of the method described by this object.
203: *
204: * @return the name of the method described by this object.
205: */
206: public String getName() {
207: return name;
208: }
209:
210: /**
211: * Returns the descriptor of the method described by this object.
212: *
213: * @return the descriptor of the method described by this object.
214: */
215: public String getDescriptor() {
216: return desc;
217: }
218:
219: /**
220: * Returns the return type of the method described by this object.
221: *
222: * @return the return type of the method described by this object.
223: */
224: public Type getReturnType() {
225: return Type.getReturnType(desc);
226: }
227:
228: /**
229: * Returns the argument types of the method described by this object.
230: *
231: * @return the argument types of the method described by this object.
232: */
233: public Type[] getArgumentTypes() {
234: return Type.getArgumentTypes(desc);
235: }
236:
237: public String toString() {
238: return name + desc;
239: }
240:
241: public boolean equals(final Object o) {
242: if (!(o instanceof Method)) {
243: return false;
244: }
245: Method other = (Method) o;
246: return name.equals(other.name) && desc.equals(other.desc);
247: }
248:
249: public int hashCode() {
250: return name.hashCode() ^ desc.hashCode();
251: }
252: }
|