[CJ-dev] commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection ImplFinder.jav
Brought to you by:
johnqueso
From: John C. <joh...@co...> - 2004-04-20 22:52:53
|
Update of /cvsroot/commonjava/commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13812/src/java/org/commonjava/reflection Modified Files: ImplFinder.java Reflector.java Log Message: added invokeStatic(Class, String, Object[]): Object to Reflector. Index: ImplFinder.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection/ImplFinder.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- ImplFinder.java 22 Oct 2003 04:23:10 -0000 1.3 +++ ImplFinder.java 20 Apr 2004 22:52:02 -0000 1.4 @@ -533,7 +533,7 @@ *@throws ConfigurationManagerException in the case of an invalid configuration file */ private static Class discoverClass(Class ifc, String sProp, String cFile, Class defImpl) - throws ClassNotFoundException, IOException + throws ClassNotFoundException, IOException, FinderException { String ifcName = ifc.getName(); ClassLoader cloader = ImplFinder.class.getClassLoader(); @@ -584,7 +584,12 @@ // If all else fails, then return the default implementation supplied. if(classFile == null){ - return defImpl; + if(defImpl == null){ + throw new FinderException("Error finding implementation of class: " + ifc.getName()); + } + else{ + return defImpl; + } } // Otherwise, load the class... else{ Index: Reflector.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-reflection/src/java/org/commonjava/reflection/Reflector.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Reflector.java 27 Feb 2004 06:43:59 -0000 1.2 +++ Reflector.java 20 Apr 2004 22:52:05 -0000 1.3 @@ -161,6 +161,49 @@ } } + /** Invoke the specified static method with the specified params... + * + * @param targetClass The target class of the invocation + * @param methodName The method name to invoke + * @param params The parameters to pass to the method invocation + * @return The result of the method call + * @throws ReflectorException In case of an error looking up or invoking the method. + */ + public static Object invokeStatic(Class targetClass, String methodName, Object[] params) + throws ReflectorException + { + if(params == null){params = new Object[0];} + + Class[] paramTypes = new Class[params.length]; + for(int i=0, len=params.length; i<len; i++){ + paramTypes[i] = params[i].getClass(); + } + + try{ + Method method = getMethod(targetClass, methodName, paramTypes); + if(method == null){ + StringBuffer buffer = new StringBuffer(); + buffer.append("Singleton-producing method named \'" + methodName + "\' not found with specified parameter classes: "); + + for (int i = 0; i < paramTypes.length; i++) { + buffer.append(paramTypes[i].getName()); + buffer.append(','); + } + buffer.setLength(buffer.length() -1); + + throw new ReflectorException(buffer.toString()); + } + + return method.invoke(null, params); + } + catch(InvocationTargetException ex){ + throw new ReflectorException(ex); + } + catch(IllegalAccessException ex){ + throw new ReflectorException(ex); + } + } + /** Return the constructor, checking the cache first and storing in cache if not * already there.. * |