Mercurial > lasercutter
diff src/clojure/asm/commons/Method.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/asm/commons/Method.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,267 @@ 1.4 +/*** 1.5 + * ASM: a very small and fast Java bytecode manipulation framework 1.6 + * Copyright (c) 2000-2005 INRIA, France Telecom 1.7 + * All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 2. Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 3. Neither the name of the copyright holders nor the names of its 1.18 + * contributors may be used to endorse or promote products derived from 1.19 + * this software without specific prior written permission. 1.20 + * 1.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 1.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 1.31 + * THE POSSIBILITY OF SUCH DAMAGE. 1.32 + */ 1.33 +package clojure.asm.commons; 1.34 + 1.35 +import java.util.HashMap; 1.36 +import java.util.Map; 1.37 + 1.38 +import clojure.asm.Type; 1.39 + 1.40 +/** 1.41 + * A named method descriptor. 1.42 + * 1.43 + * @author Juozas Baliuka 1.44 + * @author Chris Nokleberg 1.45 + * @author Eric Bruneton 1.46 + */ 1.47 +public class Method{ 1.48 + 1.49 +/** 1.50 + * The method name. 1.51 + */ 1.52 +private final String name; 1.53 + 1.54 +/** 1.55 + * The method descriptor. 1.56 + */ 1.57 +private final String desc; 1.58 + 1.59 +/** 1.60 + * Maps primitive Java type names to their descriptors. 1.61 + */ 1.62 +private final static Map DESCRIPTORS; 1.63 + 1.64 +static 1.65 + { 1.66 + DESCRIPTORS = new HashMap(); 1.67 + DESCRIPTORS.put("void", "V"); 1.68 + DESCRIPTORS.put("byte", "B"); 1.69 + DESCRIPTORS.put("char", "C"); 1.70 + DESCRIPTORS.put("double", "D"); 1.71 + DESCRIPTORS.put("float", "F"); 1.72 + DESCRIPTORS.put("int", "I"); 1.73 + DESCRIPTORS.put("long", "J"); 1.74 + DESCRIPTORS.put("short", "S"); 1.75 + DESCRIPTORS.put("boolean", "Z"); 1.76 + } 1.77 + 1.78 +/** 1.79 + * Creates a new {@link Method}. 1.80 + * 1.81 + * @param name the method's name. 1.82 + * @param desc the method's descriptor. 1.83 + */ 1.84 +public Method(final String name, final String desc){ 1.85 + this.name = name; 1.86 + this.desc = desc; 1.87 +} 1.88 + 1.89 +/** 1.90 + * Creates a new {@link Method}. 1.91 + * 1.92 + * @param name the method's name. 1.93 + * @param returnType the method's return type. 1.94 + * @param argumentTypes the method's argument types. 1.95 + */ 1.96 +public Method( 1.97 + final String name, 1.98 + final Type returnType, 1.99 + final Type[] argumentTypes){ 1.100 + this(name, Type.getMethodDescriptor(returnType, argumentTypes)); 1.101 +} 1.102 + 1.103 +/** 1.104 + * Returns a {@link Method} corresponding to the given Java method 1.105 + * declaration. 1.106 + * 1.107 + * @param method a Java method declaration, without argument names, of the 1.108 + * form "returnType name (argumentType1, ... argumentTypeN)", where 1.109 + * the types are in plain Java (e.g. "int", "float", 1.110 + * "java.util.List", ...). Classes of the java.lang package can be 1.111 + * specified by their unqualified name; all other classes names must 1.112 + * be fully qualified. 1.113 + * @return a {@link Method} corresponding to the given Java method 1.114 + * declaration. 1.115 + * @throws IllegalArgumentException if <code>method</code> could not get 1.116 + * parsed. 1.117 + */ 1.118 +public static Method getMethod(final String method) 1.119 + throws IllegalArgumentException{ 1.120 + return getMethod(method, false); 1.121 +} 1.122 + 1.123 +/** 1.124 + * Returns a {@link Method} corresponding to the given Java method 1.125 + * declaration. 1.126 + * 1.127 + * @param method a Java method declaration, without argument names, of the 1.128 + * form "returnType name (argumentType1, ... argumentTypeN)", where 1.129 + * the types are in plain Java (e.g. "int", "float", 1.130 + * "java.util.List", ...). Classes of the java.lang package may be 1.131 + * specified by their unqualified name, depending on the 1.132 + * defaultPackage argument; all other classes names must be fully 1.133 + * qualified. 1.134 + * @param defaultPackage true if unqualified class names belong to the 1.135 + * default package, or false if they correspond to java.lang classes. 1.136 + * For instance "Object" means "Object" if this option is true, or 1.137 + * "java.lang.Object" otherwise. 1.138 + * @return a {@link Method} corresponding to the given Java method 1.139 + * declaration. 1.140 + * @throws IllegalArgumentException if <code>method</code> could not get 1.141 + * parsed. 1.142 + */ 1.143 +public static Method getMethod( 1.144 + final String method, 1.145 + final boolean defaultPackage) throws IllegalArgumentException{ 1.146 + int space = method.indexOf(' '); 1.147 + int start = method.indexOf('(', space) + 1; 1.148 + int end = method.indexOf(')', start); 1.149 + if(space == -1 || start == -1 || end == -1) 1.150 + { 1.151 + throw new IllegalArgumentException(); 1.152 + } 1.153 + // TODO: Check validity of returnType, methodName and arguments. 1.154 + String returnType = method.substring(0, space); 1.155 + String methodName = method.substring(space + 1, start - 1).trim(); 1.156 + StringBuffer sb = new StringBuffer(); 1.157 + sb.append('('); 1.158 + int p; 1.159 + do 1.160 + { 1.161 + String s; 1.162 + p = method.indexOf(',', start); 1.163 + if(p == -1) 1.164 + { 1.165 + s = map(method.substring(start, end).trim(), defaultPackage); 1.166 + } 1.167 + else 1.168 + { 1.169 + s = map(method.substring(start, p).trim(), defaultPackage); 1.170 + start = p + 1; 1.171 + } 1.172 + sb.append(s); 1.173 + } while(p != -1); 1.174 + sb.append(')'); 1.175 + sb.append(map(returnType, defaultPackage)); 1.176 + return new Method(methodName, sb.toString()); 1.177 +} 1.178 + 1.179 +private static String map(final String type, final boolean defaultPackage){ 1.180 + if(type.equals("")) 1.181 + { 1.182 + return type; 1.183 + } 1.184 + 1.185 + StringBuffer sb = new StringBuffer(); 1.186 + int index = 0; 1.187 + while((index = type.indexOf("[]", index) + 1) > 0) 1.188 + { 1.189 + sb.append('['); 1.190 + } 1.191 + 1.192 + String t = type.substring(0, type.length() - sb.length() * 2); 1.193 + String desc = (String) DESCRIPTORS.get(t); 1.194 + if(desc != null) 1.195 + { 1.196 + sb.append(desc); 1.197 + } 1.198 + else 1.199 + { 1.200 + sb.append('L'); 1.201 + if(t.indexOf('.') < 0) 1.202 + { 1.203 + if(!defaultPackage) 1.204 + { 1.205 + sb.append("java/lang/"); 1.206 + } 1.207 + sb.append(t); 1.208 + } 1.209 + else 1.210 + { 1.211 + sb.append(t.replace('.', '/')); 1.212 + } 1.213 + sb.append(';'); 1.214 + } 1.215 + return sb.toString(); 1.216 +} 1.217 + 1.218 +/** 1.219 + * Returns the name of the method described by this object. 1.220 + * 1.221 + * @return the name of the method described by this object. 1.222 + */ 1.223 +public String getName(){ 1.224 + return name; 1.225 +} 1.226 + 1.227 +/** 1.228 + * Returns the descriptor of the method described by this object. 1.229 + * 1.230 + * @return the descriptor of the method described by this object. 1.231 + */ 1.232 +public String getDescriptor(){ 1.233 + return desc; 1.234 +} 1.235 + 1.236 +/** 1.237 + * Returns the return type of the method described by this object. 1.238 + * 1.239 + * @return the return type of the method described by this object. 1.240 + */ 1.241 +public Type getReturnType(){ 1.242 + return Type.getReturnType(desc); 1.243 +} 1.244 + 1.245 +/** 1.246 + * Returns the argument types of the method described by this object. 1.247 + * 1.248 + * @return the argument types of the method described by this object. 1.249 + */ 1.250 +public Type[] getArgumentTypes(){ 1.251 + return Type.getArgumentTypes(desc); 1.252 +} 1.253 + 1.254 +public String toString(){ 1.255 + return name + desc; 1.256 +} 1.257 + 1.258 +public boolean equals(final Object o){ 1.259 + if(!(o instanceof Method)) 1.260 + { 1.261 + return false; 1.262 + } 1.263 + Method other = (Method) o; 1.264 + return name.equals(other.name) && desc.equals(other.desc); 1.265 +} 1.266 + 1.267 +public int hashCode(){ 1.268 + return name.hashCode() ^ desc.hashCode(); 1.269 +} 1.270 +} 1.271 \ No newline at end of file