GNU Classpath (0.17) | ||
Frames | No Frames |
1: /* java.beans.Statement 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.beans; 40: 41: import java.lang.reflect.Array; 42: import java.lang.reflect.Constructor; 43: import java.lang.reflect.Method; 44: 45: /** 46: * class Statement 47: * 48: * A Statement captures the execution of an object method. It stores 49: * the object, the method to call, and the arguments to the method and 50: * provides the ability to execute the method on the object, using the 51: * provided arguments. 52: * 53: * @since 1.4 54: */ 55: public class Statement 56: { 57: private Object target; 58: private String methodName; 59: private Object[] arguments; 60: 61: // One or the other of these will get a value after execute is 62: // called once, but not both. 63: private transient Method method; 64: private transient Constructor ctor; 65: 66: /** 67: * Constructs a statement representing the invocation of 68: * object.methodName(arg[0], arg[1], ...); 69: * 70: * @param target The object to invoke the method on. 71: * @param methodName The object method to invoke. 72: * @param arguments An array of arguments to pass to the method. 73: */ 74: public Statement(Object target, String methodName, Object[] arguments) 75: { 76: this.target = target; 77: this.methodName = methodName; 78: this.arguments = arguments; 79: } 80: 81: /** 82: * Execute the statement. 83: * 84: * Finds the specified method in the target object and calls it with 85: * the arguments given in the constructor. 86: * 87: * The most specific method according to the JLS(15.11) is used when 88: * there are multiple methods with the same name. 89: * 90: * Execute performs some special handling for methods and 91: * parameters: 92: * 93: * Static methods can be executed by providing the class as a 94: * target. 95: * 96: * The method name new is reserved to call the constructor 97: * new() will construct an object and return it. Not useful unless 98: * an expression :-) 99: * 100: * If the target is an array, get and set as defined in 101: * java.util.List are recognized as valid methods and mapped to the 102: * methods of the same name in java.lang.reflect.Array. 103: * 104: * The native datatype wrappers Boolean, Byte, Character, Double, 105: * Float, Integer, Long, and Short will map to methods that have 106: * native datatypes as parameters, in the same way as Method.invoke. 107: * However, these wrappers also select methods that actually take 108: * the wrapper type as an argument. 109: * 110: * The Sun spec doesn't deal with overloading between int and 111: * Integer carefully. If there are two methods, one that takes an 112: * Integer and the other taking an int, the method chosen is not 113: * specified, and can depend on the order in which the methods are 114: * declared in the source file. 115: * 116: * @throws Exception if an exception occurs while locating or 117: * invoking the method. 118: */ 119: public void execute() throws Exception 120: { 121: doExecute(); 122: } 123: 124: private static Class wrappers[] = 125: { 126: Boolean.class, Byte.class, Character.class, Double.class, Float.class, 127: Integer.class, Long.class, Short.class 128: }; 129: 130: private static Class natives[] = 131: { 132: Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE, Float.TYPE, 133: Integer.TYPE, Long.TYPE, Short.TYPE 134: }; 135: 136: // Given a wrapper class, return the native class for it. For 137: // example, if c is Integer, Integer.TYPE is returned. 138: private Class unwrap(Class c) 139: { 140: for (int i = 0; i < wrappers.length; i++) 141: if (c == wrappers[i]) 142: return natives[i]; 143: return null; 144: } 145: 146: // Return true if all args can be assigned to params, false 147: // otherwise. Arrays are guaranteed to be the same length. 148: private boolean compatible(Class[] params, Class[] args) 149: { 150: for (int i = 0; i < params.length; i++) 151: { 152: // Treat Integer like int if appropriate 153: Class nativeType = unwrap(args[i]); 154: if (nativeType != null && params[i].isPrimitive() 155: && params[i].isAssignableFrom(nativeType)) 156: continue; 157: if (params[i].isAssignableFrom(args[i])) 158: continue; 159: 160: return false; 161: } 162: return true; 163: } 164: 165: /** 166: * Return true if the method arguments in first are more specific 167: * than the method arguments in second, i.e. all args in first can 168: * be assigned to those in second. 169: * 170: * A method is more specific if all parameters can also be fed to 171: * the less specific method, because, e.g. the less specific method 172: * accepts a base class of the equivalent argument for the more 173: * specific one. 174: * 175: * @param first a <code>Class[]</code> value 176: * @param second a <code>Class[]</code> value 177: * @return a <code>boolean</code> value 178: */ 179: private boolean moreSpecific(Class[] first, Class[] second) 180: { 181: for (int j=0; j < first.length; j++) 182: { 183: if (second[j].isAssignableFrom(first[j])) 184: continue; 185: return false; 186: } 187: return true; 188: } 189: 190: final Object doExecute() throws Exception 191: { 192: Class klazz = (target instanceof Class) 193: ? (Class) target : target.getClass(); 194: Object args[] = (arguments == null) ? new Object[0] : arguments; 195: Class argTypes[] = new Class[args.length]; 196: for (int i = 0; i < args.length; i++) 197: argTypes[i] = args[i].getClass(); 198: 199: if (target.getClass().isArray()) 200: { 201: // FIXME: invoke may have to be used. For now, cast to Number 202: // and hope for the best. If caller didn't behave, we go boom 203: // and throw the exception. 204: if (methodName.equals("get") && argTypes.length == 1) 205: return Array.get(target, ((Number)args[0]).intValue()); 206: if (methodName.equals("set") && argTypes.length == 2) 207: { 208: Object obj = Array.get(target, ((Number)args[0]).intValue()); 209: Array.set(target, ((Number)args[0]).intValue(), args[1]); 210: return obj; 211: } 212: throw new NoSuchMethodException("No matching method for statement " + toString()); 213: } 214: 215: // If we already cached the method, just use it. 216: if (method != null) 217: return method.invoke(target, args); 218: else if (ctor != null) 219: return ctor.newInstance(args); 220: 221: // Find a matching method to call. JDK seems to go through all 222: // this to find the method to call. 223: 224: // if method name or length don't match, skip 225: // Need to go through each arg 226: // If arg is wrapper - check if method arg is matchable builtin 227: // or same type or super 228: // - check that method arg is same or super 229: 230: if (methodName.equals("new") && target instanceof Class) 231: { 232: Constructor ctors[] = klazz.getConstructors(); 233: for (int i = 0; i < ctors.length; i++) 234: { 235: // Skip methods with wrong number of args. 236: Class ptypes[] = ctors[i].getParameterTypes(); 237: System.out.println("ptypeslen = " + ptypes.length); 238: System.out.println("ptypes = " + ptypes); 239: System.out.println("ctor = " + ctors[i].getName()); 240: for (int j=0; j < ptypes.length; j++) { 241: System.out.println("param = " + ptypes[i].getName()); 242: 243: } 244: 245: 246: if (ptypes.length != args.length) 247: continue; 248: 249: // Check if method matches 250: if (!compatible(ptypes, argTypes)) 251: continue; 252: 253: // Use method[i] if it is more specific. 254: // FIXME: should this check both directions and throw if 255: // neither is more specific? 256: if (ctor == null) 257: { 258: ctor = ctors[i]; 259: continue; 260: } 261: Class mptypes[] = ctor.getParameterTypes(); 262: if (moreSpecific(ptypes, mptypes)) 263: ctor = ctors[i]; 264: } 265: if (ctor == null) 266: throw new InstantiationException("No matching constructor for statement " + toString()); 267: return ctor.newInstance(args); 268: } 269: 270: Method methods[] = klazz.getMethods(); 271: 272: for (int i = 0; i < methods.length; i++) 273: { 274: // Skip methods with wrong name or number of args. 275: if (!methods[i].getName().equals(methodName)) 276: continue; 277: Class ptypes[] = methods[i].getParameterTypes(); 278: if (ptypes.length != args.length) 279: continue; 280: 281: // Check if method matches 282: if (!compatible(ptypes, argTypes)) 283: continue; 284: 285: // Use method[i] if it is more specific. 286: // FIXME: should this check both directions and throw if 287: // neither is more specific? 288: if (method == null) 289: { 290: method = methods[i]; 291: continue; 292: } 293: Class mptypes[] = method.getParameterTypes(); 294: if (moreSpecific(ptypes, mptypes)) 295: method = methods[i]; 296: } 297: if (method == null) 298: throw new NoSuchMethodException("No matching method for statement " + toString()); 299: return method.invoke(target, args); 300: } 301: 302: 303: 304: /** Return the statement arguments. */ 305: public Object[] getArguments() { return arguments; } 306: 307: /** Return the statement method name. */ 308: public String getMethodName() { return methodName; } 309: 310: /** Return the statement object. */ 311: public Object getTarget() { return target; } 312: 313: /** Return a string representation. */ 314: public String toString() 315: { 316: String result = target.getClass().getName() + "." + methodName + "("; 317: String sep = ""; 318: for (int i = 0; i < arguments.length; i++) 319: { 320: result = result + sep + arguments[i].getClass().getName(); 321: sep = ", "; 322: } 323: result = result + ")"; 324: return result; 325: } 326: }
GNU Classpath (0.17) |