here we go again...
Index: ConstructorUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConstructorUtils.java,v retrieving revision 1.2 diff -u -r1.2 ConstructorUtils.java --- ConstructorUtils.java 15 Jan 2003 21:59:38 -0000 1.2 +++ ConstructorUtils.java 3 Feb 2003 14:38:06 -0000 @@ -63,6 +63,21 @@ /** * @version $Revision: 1.2 $ $Date: 2003/01/15 21:59:38 $ + * + * <p> Utility reflection methods focussed on constructors, modelled after {@link +MethodUtils}. </p> + * + * <h3>Known Limitations</h3> + * <h4>Accessing Public Constructors In A Default Access Superclass</h4> + * <p>There is an issue when invoking public constructors contained in a default +access superclass. + * Reflection locates these constructors fine and correctly assigns them as public. + * However, an <code>IllegalAccessException</code> is thrown if the constructors is +invoked.</p> + * + * <p><code>ConstructorUtils</code> contains a workaround for this situation. + * It will attempt to call <code>setAccessible</code> on this constructor. + * If this call succeeds, then the method can be invoked as normal. + * This call will only succeed when the application has sufficient security +privilages. + * If this call fails then a warning will be logged and the method may fail.</p> + * * @author Craig R. McClanahan * @author Ralph Schaer * @author Chris Audley @@ -82,6 +97,19 @@ // --------------------------------------------------------- Public Methods + /** + * <p>Convenience method returning new instance of <code>klazz</code> using a +single argument constructor. + * The formal parameter type is inferred from the actual values of +<code>arg</code>. + * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more +details.</p> + * + * <p>The signatures should be assignment compatible.</p> + * + * @param klass the class to be constructed. + * @param arg the actual argument + * @return new instance of <code>klazz</code> + * + * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) + */ public static Object invokeConstructor(Class klass, Object arg) throws NoSuchMethodException, @@ -94,6 +122,19 @@ } + /** + * <p>Returns new instance of <code>klazz</code> created using the actual +arguments <code>args</code>. + * The formal parameter types are inferred from the actual values of +<code>args</code>. + * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more +details.</p> + * + * <p>The signatures should be assignment compatible.</p> + * + * @param klass the class to be constructed. + * @param args actual argument array + * @return new instance of <code>klazz</code> + * + * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) + */ public static Object invokeConstructor(Class klass, Object[] args) throws NoSuchMethodException, @@ -113,6 +154,23 @@ } + /** + * <p>Returns new instance of <code>klazz</code> created using constructor + * with signature <code>parameterTypes</code> and actual arguments +<code>args</code>.</p> + * + * <p>The signatures should be assignment compatible.</p> + * + * @param klass the class to be constructed. + * @param args actual argument array + * @param parameterTypes parameter types array + * @return new instance of <code>klazz</code> + * + * @throws NoSuchMethodException if matching constructor cannot be found + * @throws IllegalAccessException thrown on the constructor's invocation + * @throws InvocationTargetException thrown on the constructor's invocation + * @throws InstantiationException thrown on the constructor's invocation + * @see Constructor#newInstance + */ public static Object invokeConstructor( Class klass, Object[] args, @@ -139,6 +197,20 @@ return ctor.newInstance(args); } + + /** + * <p>Convenience method returning new instance of <code>klazz</code> using a +single argument constructor. + * The formal parameter type is inferred from the actual values of +<code>arg</code>. + * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more +details.</p> + * + * <p>The signatures should match exactly.</p> + * + * @param klass the class to be constructed. + * @param arg the actual argument + * @return new instance of <code>klazz</code> + * + * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], +java.lang.Class[]) + */ public static Object invokeExactConstructor(Class klass, Object arg) throws NoSuchMethodException, @@ -150,6 +222,20 @@ return invokeExactConstructor(klass, args); } + + /** + * <p>Returns new instance of <code>klazz</code> created using the actual +arguments <code>args</code>. + * The formal parameter types are inferred from the actual values of +<code>args</code>. + * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more +details.</p> + * + * <p>The signatures should match exactly.</p> + * + * @param klass the class to be constructed. + * @param args actual argument array + * @return new instance of <code>klazz</code> + * + * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], +java.lang.Class[]) + */ public static Object invokeExactConstructor(Class klass, Object[] args) throws NoSuchMethodException, @@ -168,6 +254,24 @@ } + /** + * <p>Returns new instance of <code>klazz</code> created using constructor + * with signature <code>parameterTypes</code> and actual arguments + * <code>args</code>.</p> + * + * <p>The signatures should match exactly.</p> + * + * @param klass the class to be constructed. + * @param args actual argument array + * @param parameterTypes parameter types array + * @return new instance of <code>klazz</code> + * + * @throws NoSuchMethodException if matching constructor cannot be found + * @throws IllegalAccessException thrown on the constructor's invocation + * @throws InvocationTargetException thrown on the constructor's invocation + * @throws InstantiationException thrown on the constructor's invocation + * @see Constructor#newInstance + */ public static Object invokeExactConstructor( Class klass, Object[] args, @@ -195,6 +299,13 @@ } + /** + * Returns a constructor with single argument. + * @param klass the class to be constructed + * @return null if matching accessible constructor can not be found. + * @see Class#getConstructor + * @see #getAccessibleConstructor(java.lang.reflect.Constructor) + */ public static Constructor getAccessibleConstructor( Class klass, Class parameterType) { @@ -204,6 +315,14 @@ } + /** + * Returns a constructor given a class and signature. + * @param klass the class to be constructed + * @param parameterTypes the parameter array + * @return null if matching accessible constructor can not be found + * @see Class#getConstructor + * @see #getAccessibleConstructor(java.lang.reflect.Constructor) + */ public static Constructor getAccessibleConstructor( Class klass, Class[] parameterTypes) { @@ -217,6 +336,12 @@ } + /** + * Returns accessible version of the given constructor. + * @param ctor prototype constructor object. + * @return <code>null</code> if accessible constructor can not be found. + * @see java.lang.SecurityManager + */ public static Constructor getAccessibleConstructor(Constructor ctor) { // Make sure we have a method to check @@ -241,7 +366,21 @@ } // -------------------------------------------------------- Private Methods - + /** + * <p>Find an accessible constructor with compatible parameters. + * Compatible parameters mean that every method parameter is assignable from + * the given parameters. In other words, it finds constructor that will take + * the parameters given.</p> + * + * <p>First it checks if there is constructor matching the exact signature. + * If no such, all the constructors of the class are tested if their signatures + * are assignment compatible with the parameter types. + * The first matching constructor is returned.</p> + * + * @param clazz find constructor for this class + * @param parameterTypes find method with compatible parameters + * @return a valid Constructor object. If there's no matching constructor, +returns <code>null</code>. + */ private static Constructor getMatchingAccessibleConstructor( Class clazz, Class[] parameterTypes) {
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]