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: */
030: package org.objectweb.asm.commons;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import org.objectweb.asm.Type;
036:
037: /**
038: * A named method descriptor.
039: *
040: * @author Juozas Baliuka
041: * @author Chris Nokleberg
042: * @author Eric Bruneton
043: */
044: @SuppressWarnings("unchecked")
045: public class Method {
046:
047: /**
048: * The method name.
049: */
050: private final String name;
051:
052: /**
053: * The method descriptor.
054: */
055: private final String desc;
056:
057: /**
058: * Maps primitive Java type names to their descriptors.
059: */
060: private final static Map DESCRIPTORS;
061:
062: static {
063: DESCRIPTORS = new HashMap();
064: DESCRIPTORS.put("void", "V");
065: DESCRIPTORS.put("byte", "B");
066: DESCRIPTORS.put("char", "C");
067: DESCRIPTORS.put("double", "D");
068: DESCRIPTORS.put("float", "F");
069: DESCRIPTORS.put("int", "I");
070: DESCRIPTORS.put("long", "J");
071: DESCRIPTORS.put("short", "S");
072: DESCRIPTORS.put("boolean", "Z");
073: }
074:
075: /**
076: * Creates a new {@link Method}.
077: *
078: * @param name the method's name.
079: * @param desc the method's descriptor.
080: */
081: public Method(final String name, final String desc) {
082: this .name = name;
083: this .desc = desc;
084: }
085:
086: /**
087: * Creates a new {@link Method}.
088: *
089: * @param name the method's name.
090: * @param returnType the method's return type.
091: * @param argumentTypes the method's argument types.
092: */
093: public Method(final String name, final Type returnType,
094: final Type[] argumentTypes) {
095: this (name, Type.getMethodDescriptor(returnType, argumentTypes));
096: }
097:
098: /**
099: * Returns a {@link Method} corresponding to the given Java method
100: * declaration.
101: *
102: * @param method a Java method declaration, without argument names, of the
103: * form "returnType name (argumentType1, ... argumentTypeN)", where
104: * the types are in plain Java (e.g. "int", "float",
105: * "java.util.List", ...).
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: int space = method.indexOf(' ');
114: int start = method.indexOf('(', space) + 1;
115: int end = method.indexOf(')', start);
116: if (space == -1 || start == -1 || end == -1) {
117: throw new IllegalArgumentException();
118: }
119: // TODO: Check validity of returnType, methodName and arguments.
120: String returnType = method.substring(0, space);
121: String methodName = method.substring(space + 1, start - 1)
122: .trim();
123: StringBuilder sb = new StringBuilder();
124: sb.append('(');
125: int p;
126: do {
127: p = method.indexOf(',', start);
128: if (p == -1) {
129: sb.append(map(method.substring(start, end).trim()));
130: } else {
131: sb.append(map(method.substring(start, p).trim()));
132: start = p + 1;
133: }
134: } while (p != -1);
135: sb.append(')');
136: sb.append(map(returnType));
137: return new Method(methodName, sb.toString());
138: }
139:
140: private static String map(final String type) {
141: if (type.equals("")) {
142: return type;
143: }
144:
145: StringBuilder sb = new StringBuilder();
146: int index = 0;
147: while ((index = type.indexOf("[]", index) + 1) > 0) {
148: sb.append('[');
149: }
150:
151: String t = type.substring(0, type.length() - sb.length() * 2);
152: String desc = (String) DESCRIPTORS.get(t);
153: if (desc != null) {
154: sb.append(desc);
155: } else {
156: sb.append('L');
157: if (t.indexOf('.') < 0) {
158: sb.append("java/lang/" + t);
159: } else {
160: sb.append(t.replace('.', '/'));
161: }
162: sb.append(';');
163: }
164: return sb.toString();
165: }
166:
167: /**
168: * Returns the name of the method described by this object.
169: *
170: * @return the name of the method described by this object.
171: */
172: public String getName() {
173: return name;
174: }
175:
176: /**
177: * Returns the descriptor of the method described by this object.
178: *
179: * @return the descriptor of the method described by this object.
180: */
181: public String getDescriptor() {
182: return desc;
183: }
184:
185: /**
186: * Returns the return type of the method described by this object.
187: *
188: * @return the return type of the method described by this object.
189: */
190: public Type getReturnType() {
191: return Type.getReturnType(desc);
192: }
193:
194: /**
195: * Returns the argument types of the method described by this object.
196: *
197: * @return the argument types of the method described by this object.
198: */
199: public Type[] getArgumentTypes() {
200: return Type.getArgumentTypes(desc);
201: }
202:
203: @Override
204: public String toString() {
205: return name + desc;
206: }
207:
208: @Override
209: public boolean equals(final Object o) {
210: if (!(o instanceof Method)) {
211: return false;
212: }
213: Method other = (Method) o;
214: return name.equals(other.name) && desc.equals(other.desc);
215: }
216:
217: @Override
218: public int hashCode() {
219: return name.hashCode() ^ desc.hashCode();
220: }
221: }
|