diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-07-17 21:58:22 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-07-17 21:58:22 +0000 |
commit | 93f1b01bc4395211903b6d16267bf2c590c35006 (patch) | |
tree | 17f350f6a2ee54b87b368e40a712fa619f27583e /src/jvm/clojure | |
parent | 1b727af895074b180b9de6a9e65f99af2595361f (diff) |
peel off InvocationTargetException in reflective calls, when possible
Diffstat (limited to 'src/jvm/clojure')
-rw-r--r-- | src/jvm/clojure/lang/Reflector.java | 90 |
1 files changed, 59 insertions, 31 deletions
diff --git a/src/jvm/clojure/lang/Reflector.java b/src/jvm/clojure/lang/Reflector.java index 47dc1b41..da9edf5f 100644 --- a/src/jvm/clojure/lang/Reflector.java +++ b/src/jvm/clojure/lang/Reflector.java @@ -21,9 +21,18 @@ import java.util.Arrays; public class Reflector{ public static Object invokeInstanceMethod(Object target, String methodName, Object[] args) throws Exception{ - Class c = target.getClass(); - List methods = getMethods(c, args.length, methodName, false); - return invokeMatchingMethod(methodName, methods, target, args); + try + { + Class c = target.getClass(); + List methods = getMethods(c, args.length, methodName, false); + return invokeMatchingMethod(methodName, methods, target, args); + } + catch(InvocationTargetException e) + { + if(e.getCause() instanceof Exception) + throw (Exception) e.getCause(); + throw e; + } } static Object invokeMatchingMethod(String methodName, List methods, Object target, Object[] args) @@ -49,7 +58,7 @@ static Object invokeMatchingMethod(String methodName, List methods, Object targe Class[] params = m.getParameterTypes(); if(isCongruent(params, args)) { - if(foundm == null || Compiler.subsumes(params,foundm.getParameterTypes())) + if(foundm == null || Compiler.subsumes(params, foundm.getParameterTypes())) { foundm = m; boxedArgs = boxArgs(params, args); @@ -99,36 +108,45 @@ public static Method getAsMethodOfPublicBase(Class c, Method m){ } public static Object invokeConstructor(Class c, Object[] args) throws Exception{ - Constructor[] allctors = c.getConstructors(); - ArrayList ctors = new ArrayList(); - for(int i = 0; i < allctors.length; i++) - { - Constructor ctor = allctors[i]; - if(ctor.getParameterTypes().length == args.length) - ctors.add(ctor); - } - if(ctors.isEmpty()) + try { - throw new IllegalArgumentException("No matching ctor found"); - } - else if(ctors.size() == 1) - { - Constructor ctor = (Constructor) ctors.get(0); - return ctor.newInstance(boxArgs(ctor.getParameterTypes(), args)); - } - else //overloaded w/same arity - { - for(Iterator iterator = ctors.iterator(); iterator.hasNext();) + Constructor[] allctors = c.getConstructors(); + ArrayList ctors = new ArrayList(); + for(int i = 0; i < allctors.length; i++) { - Constructor ctor = (Constructor) iterator.next(); - Class[] params = ctor.getParameterTypes(); - if(isCongruent(params, args)) + Constructor ctor = allctors[i]; + if(ctor.getParameterTypes().length == args.length) + ctors.add(ctor); + } + if(ctors.isEmpty()) + { + throw new IllegalArgumentException("No matching ctor found"); + } + else if(ctors.size() == 1) + { + Constructor ctor = (Constructor) ctors.get(0); + return ctor.newInstance(boxArgs(ctor.getParameterTypes(), args)); + } + else //overloaded w/same arity + { + for(Iterator iterator = ctors.iterator(); iterator.hasNext();) { - Object[] boxedArgs = boxArgs(params, args); - return ctor.newInstance(boxedArgs); + Constructor ctor = (Constructor) iterator.next(); + Class[] params = ctor.getParameterTypes(); + if(isCongruent(params, args)) + { + Object[] boxedArgs = boxArgs(params, args); + return ctor.newInstance(boxedArgs); + } } + throw new IllegalArgumentException("No matching ctor found"); } - throw new IllegalArgumentException("No matching ctor found"); + } + catch(InvocationTargetException e) + { + if(e.getCause() instanceof Exception) + throw (Exception) e.getCause(); + throw e; } } @@ -139,7 +157,16 @@ public static Object invokeStaticMethodVariadic(String className, String methodN public static Object invokeStaticMethod(String className, String methodName, Object[] args) throws Exception{ Class c = RT.classForName(className); - return invokeStaticMethod(c, methodName, args); + try + { + return invokeStaticMethod(c, methodName, args); + } + catch(InvocationTargetException e) + { + if(e.getCause() instanceof Exception) + throw (Exception) e.getCause(); + throw e; + } } public static Object invokeStaticMethod(Class c, String methodName, Object[] args) throws Exception{ @@ -164,6 +191,7 @@ public static Object getStaticField(Class c, String fieldName) throws Exception{ } throw new IllegalArgumentException("No matching field found: " + fieldName); } + public static Object setStaticField(String className, String fieldName, Object val) throws Exception{ Class c = RT.classForName(className); return setStaticField(c, fieldName, val); @@ -202,7 +230,7 @@ public static Object setInstanceField(Object target, String fieldName, Object va public static Object invokeNoArgInstanceMember(Object target, String name) throws Exception{ //favor method over field - List meths = getMethods(target.getClass(),0,name,false); + List meths = getMethods(target.getClass(), 0, name, false); if(meths.size() > 0) return invokeMatchingMethod(name, meths, target, RT.EMPTY_ARRAY); else |