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.objectweb.asm.jip.commons;
030:
031: import java.util.HashMap;
032: import java.util.Map;
033:
034: import org.objectweb.asm.jip.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", ...).
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: int space = method.indexOf(' ');
112: int start = method.indexOf('(', space) + 1;
113: 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: String returnType = method.substring(0, space);
119: String methodName = method.substring(space + 1, start - 1)
120: .trim();
121: 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: StringBuffer sb = new StringBuffer();
144: int index = 0;
145: while ((index = type.indexOf("[]", index) + 1) > 0) {
146: sb.append('[');
147: }
148:
149: String t = type.substring(0, type.length() - sb.length() * 2);
150: String desc = (String) DESCRIPTORS.get(t);
151: if (desc != null) {
152: sb.append(desc);
153: } else {
154: sb.append('L');
155: if (t.indexOf('.') < 0) {
156: sb.append("java/lang/" + t);
157: } else {
158: sb.append(t.replace('.', '/'));
159: }
160: sb.append(';');
161: }
162: return sb.toString();
163: }
164:
165: /**
166: * Returns the name of the method described by this object.
167: *
168: * @return the name of the method described by this object.
169: */
170: public String getName() {
171: return name;
172: }
173:
174: /**
175: * Returns the descriptor of the method described by this object.
176: *
177: * @return the descriptor of the method described by this object.
178: */
179: public String getDescriptor() {
180: return desc;
181: }
182:
183: /**
184: * Returns the return type of the method described by this object.
185: *
186: * @return the return type of the method described by this object.
187: */
188: public Type getReturnType() {
189: return Type.getReturnType(desc);
190: }
191:
192: /**
193: * Returns the argument types of the method described by this object.
194: *
195: * @return the argument types of the method described by this object.
196: */
197: public Type[] getArgumentTypes() {
198: return Type.getArgumentTypes(desc);
199: }
200:
201: public String toString() {
202: return name + desc;
203: }
204:
205: public boolean equals(final Object o) {
206: if (!(o instanceof Method)) {
207: return false;
208: }
209: Method other = (Method) o;
210: return name.equals(other.name) && desc.equals(other.desc);
211: }
212:
213: public int hashCode() {
214: return name.hashCode() ^ desc.hashCode();
215: }
216: }
|