001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2007 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.objectweb.asm.commons;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import org.objectweb.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 static final 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: * Creates a new {@link Method}.
098: *
099: * @param m a java.lang.reflect method descriptor
100: * @return a {@link Method} corresponding to the given Java method
101: * declaration.
102: */
103: public static Method getMethod(java.lang.reflect.Method m) {
104: return new Method(m.getName(), Type.getMethodDescriptor(m));
105: }
106:
107: /**
108: * Creates a new {@link Method}.
109: *
110: * @param c a java.lang.reflect constructor descriptor
111: * @return a {@link Method} corresponding to the given Java constructor
112: * declaration.
113: */
114: public static Method getMethod(java.lang.reflect.Constructor c) {
115: return new Method("<init>", Type.getConstructorDescriptor(c));
116: }
117:
118: /**
119: * Returns a {@link Method} corresponding to the given Java method
120: * declaration.
121: *
122: * @param method a Java method declaration, without argument names, of the
123: * form "returnType name (argumentType1, ... argumentTypeN)", where
124: * the types are in plain Java (e.g. "int", "float",
125: * "java.util.List", ...). Classes of the java.lang package can be
126: * specified by their unqualified name; all other classes names must
127: * be fully qualified.
128: * @return a {@link Method} corresponding to the given Java method
129: * declaration.
130: * @throws IllegalArgumentException if <code>method</code> could not get
131: * parsed.
132: */
133: public static Method getMethod(final String method)
134: throws IllegalArgumentException {
135: return getMethod(method, false);
136: }
137:
138: /**
139: * Returns a {@link Method} corresponding to the given Java method
140: * declaration.
141: *
142: * @param method a Java method declaration, without argument names, of the
143: * form "returnType name (argumentType1, ... argumentTypeN)", where
144: * the types are in plain Java (e.g. "int", "float",
145: * "java.util.List", ...). Classes of the java.lang package may be
146: * specified by their unqualified name, depending on the
147: * defaultPackage argument; all other classes names must be fully
148: * qualified.
149: * @param defaultPackage true if unqualified class names belong to the
150: * default package, or false if they correspond to java.lang classes.
151: * For instance "Object" means "Object" if this option is true, or
152: * "java.lang.Object" otherwise.
153: * @return a {@link Method} corresponding to the given Java method
154: * declaration.
155: * @throws IllegalArgumentException if <code>method</code> could not get
156: * parsed.
157: */
158: public static Method getMethod(final String method,
159: final boolean defaultPackage)
160: throws IllegalArgumentException {
161: int space = method.indexOf(' ');
162: int start = method.indexOf('(', space) + 1;
163: int end = method.indexOf(')', start);
164: if (space == -1 || start == -1 || end == -1) {
165: throw new IllegalArgumentException();
166: }
167: String returnType = method.substring(0, space);
168: String methodName = method.substring(space + 1, start - 1)
169: .trim();
170: StringBuffer sb = new StringBuffer();
171: sb.append('(');
172: int p;
173: do {
174: String s;
175: p = method.indexOf(',', start);
176: if (p == -1) {
177: s = map(method.substring(start, end).trim(),
178: defaultPackage);
179: } else {
180: s = map(method.substring(start, p).trim(),
181: defaultPackage);
182: start = p + 1;
183: }
184: sb.append(s);
185: } while (p != -1);
186: sb.append(')');
187: sb.append(map(returnType, defaultPackage));
188: return new Method(methodName, sb.toString());
189: }
190:
191: private static String map(final String type,
192: final boolean defaultPackage) {
193: if ("".equals(type)) {
194: return type;
195: }
196:
197: StringBuffer sb = new StringBuffer();
198: int index = 0;
199: while ((index = type.indexOf("[]", index) + 1) > 0) {
200: sb.append('[');
201: }
202:
203: String t = type.substring(0, type.length() - sb.length() * 2);
204: String desc = (String) DESCRIPTORS.get(t);
205: if (desc != null) {
206: sb.append(desc);
207: } else {
208: sb.append('L');
209: if (t.indexOf('.') < 0) {
210: if (!defaultPackage) {
211: sb.append("java/lang/");
212: }
213: sb.append(t);
214: } else {
215: sb.append(t.replace('.', '/'));
216: }
217: sb.append(';');
218: }
219: return sb.toString();
220: }
221:
222: /**
223: * Returns the name of the method described by this object.
224: *
225: * @return the name of the method described by this object.
226: */
227: public String getName() {
228: return name;
229: }
230:
231: /**
232: * Returns the descriptor of the method described by this object.
233: *
234: * @return the descriptor of the method described by this object.
235: */
236: public String getDescriptor() {
237: return desc;
238: }
239:
240: /**
241: * Returns the return type of the method described by this object.
242: *
243: * @return the return type of the method described by this object.
244: */
245: public Type getReturnType() {
246: return Type.getReturnType(desc);
247: }
248:
249: /**
250: * Returns the argument types of the method described by this object.
251: *
252: * @return the argument types of the method described by this object.
253: */
254: public Type[] getArgumentTypes() {
255: return Type.getArgumentTypes(desc);
256: }
257:
258: public String toString() {
259: return name + desc;
260: }
261:
262: public boolean equals(final Object o) {
263: if (!(o instanceof Method)) {
264: return false;
265: }
266: Method other = (Method) o;
267: return name.equals(other.name) && desc.equals(other.desc);
268: }
269:
270: public int hashCode() {
271: return name.hashCode() ^ desc.hashCode();
272: }
273: }
|