Review and cleanup Javadoc in ConstructorUtils

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1143735 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2011-07-07 09:52:00 +00:00
parent d70baf1370
commit 6fd8000cce

View File

@ -58,33 +58,27 @@ public ConstructorUtils() {
} }
/** /**
* <p>Returns a new instance of <code>cls</code> created using the actual * <p>Returns a new instance of the specified class inferring the right constructor
* arguments <code>args</code>. The formal parameter types are inferred from * from the types of the arguments.</p>
* the actual values of <code>args</code>. See *
* {@link #invokeExactConstructor(Class, Object[], Class[])} for more * <p>This locates and calls a constructor.
* details.</p> * The constructor signature must match the argument types by assignment compatibility.</p>
*
* <p>The signatures should be assignment compatible.</p>
* *
* @param <T> the type to be constructed * @param <T> the type to be constructed
* @param cls the class to be constructed. * @param cls the class to be constructed, not null
* @param args actual argument array * @param args the array of arguments, null treated as empty
* @return new instance of <code>cls</code> * @return new instance of <code>cls</code>, not null
* *
* @throws NoSuchMethodException If the constructor cannot be found * @throws NoSuchMethodException if a matching constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the * @throws IllegalAccessException if invocation is not permitted by security
* constructor * @throws InvocationTargetException if an error occurs on invocation
* @throws InvocationTargetException If an error occurs invoking the * @throws InstantiationException if an error occurs on instantiation
* constructor * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeConstructor(java.lang.Class, java.lang.Object[],
* java.lang.Class[])
*/ */
public static <T> T invokeConstructor(Class<T> cls, Object... args) public static <T> T invokeConstructor(Class<T> cls, Object... args)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
InstantiationException { InstantiationException {
if (null == args) { if (args == null) {
args = ArrayUtils.EMPTY_OBJECT_ARRAY; args = ArrayUtils.EMPTY_OBJECT_ARRAY;
} }
Class<?> parameterTypes[] = new Class[args.length]; Class<?> parameterTypes[] = new Class[args.length];
@ -95,22 +89,22 @@ public static <T> T invokeConstructor(Class<T> cls, Object... args)
} }
/** /**
* <p>Returns a new instance of <code>cls</code> created using constructor * <p>Returns a new instance of the specified class choosing the right constructor
* with signature <code>parameterTypes</code> and actual arguments * from the list of parameter types.</p>
* <code>args</code>.</p> *
* * <p>This locates and calls a constructor.
* <p>The signatures should be assignment compatible.</p> * The constructor signature must match the parameter types by assignment compatibility.</p>
* *
* @param <T> the type to be constructed * @param <T> the type to be constructed
* @param cls the class to be constructed. * @param cls the class to be constructed, not null
* @param args actual argument array * @param args the array of arguments, null treated as empty
* @param parameterTypes parameter types array * @param parameterTypes the array of parameter types, null treated as empty
* @return new instance of <code>cls</code> * @return new instance of <code>cls</code>, not null
* *
* @throws NoSuchMethodException if matching constructor cannot be found * @throws NoSuchMethodException if a matching constructor cannot be found
* @throws IllegalAccessException thrown on the constructor's invocation * @throws IllegalAccessException if invocation is not permitted by security
* @throws InvocationTargetException thrown on the constructor's invocation * @throws InvocationTargetException if an error occurs on invocation
* @throws InstantiationException thrown on the constructor's invocation * @throws InstantiationException if an error occurs on instantiation
* @see Constructor#newInstance * @see Constructor#newInstance
*/ */
public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes)
@ -123,41 +117,35 @@ public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] pa
args = ArrayUtils.EMPTY_OBJECT_ARRAY; args = ArrayUtils.EMPTY_OBJECT_ARRAY;
} }
Constructor<T> ctor = getMatchingAccessibleConstructor(cls, parameterTypes); Constructor<T> ctor = getMatchingAccessibleConstructor(cls, parameterTypes);
if (null == ctor) { if (ctor == null) {
throw new NoSuchMethodException("No such accessible constructor on object: " throw new NoSuchMethodException(
+ cls.getName()); "No such accessible constructor on object: " + cls.getName());
} }
return ctor.newInstance(args); return ctor.newInstance(args);
} }
/** /**
* <p>Returns a new instance of <code>cls</code> created using the actual * <p>Returns a new instance of the specified class inferring the right constructor
* arguments <code>args</code>. The formal parameter types are inferred from * from the types of the arguments.</p>
* the actual values of <code>args</code>. See
* {@link #invokeExactConstructor(Class, Object[], Class[])} for more
* details.</p>
* *
* <p>The signatures should match exactly.</p> * <p>This locates and calls a constructor.
* The constructor signature must match the argument types exactly.</p>
* *
* @param <T> the type to be constructed * @param <T> the type to be constructed
* @param cls the class to be constructed. * @param cls the class to be constructed, not null
* @param args actual argument array * @param args the array of arguments, null treated as empty
* @return new instance of <code>cls</code> * @return new instance of <code>cls</code>, not null
* *
* @throws NoSuchMethodException If the constructor cannot be found * @throws NoSuchMethodException if a matching constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the * @throws IllegalAccessException if invocation is not permitted by security
* constructor * @throws InvocationTargetException if an error occurs on invocation
* @throws InvocationTargetException If an error occurs invoking the * @throws InstantiationException if an error occurs on instantiation
* constructor * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeExactConstructor(java.lang.Class, java.lang.Object[],
* java.lang.Class[])
*/ */
public static <T> T invokeExactConstructor(Class<T> cls, Object... args) public static <T> T invokeExactConstructor(Class<T> cls, Object... args)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
InstantiationException { InstantiationException {
if (null == args) { if (args == null) {
args = ArrayUtils.EMPTY_OBJECT_ARRAY; args = ArrayUtils.EMPTY_OBJECT_ARRAY;
} }
int arguments = args.length; int arguments = args.length;
@ -169,22 +157,22 @@ public static <T> T invokeExactConstructor(Class<T> cls, Object... args)
} }
/** /**
* <p>Returns a new instance of <code>cls</code> created using constructor * <p>Returns a new instance of the specified class choosing the right constructor
* with signature <code>parameterTypes</code> and actual arguments * from the list of parameter types.</p>
* <code>args</code>.</p>
* *
* <p>The signatures should match exactly.</p> * <p>This locates and calls a constructor.
* The constructor signature must match the parameter types exactly.</p>
* *
* @param <T> the type to be constructed * @param <T> the type to be constructed
* @param cls the class to be constructed. * @param cls the class to be constructed, not null
* @param args actual argument array * @param args the array of arguments, null treated as empty
* @param parameterTypes parameter types array * @param parameterTypes the array of parameter types, null treated as empty
* @return new instance of <code>cls</code> * @return new instance of <code>cls</code>, not null
* *
* @throws NoSuchMethodException if matching constructor cannot be found * @throws NoSuchMethodException if a matching constructor cannot be found
* @throws IllegalAccessException thrown on the constructor's invocation * @throws IllegalAccessException if invocation is not permitted by security
* @throws InvocationTargetException thrown on the constructor's invocation * @throws InvocationTargetException if an error occurs on invocation
* @throws InstantiationException thrown on the constructor's invocation * @throws InstantiationException if an error occurs on instantiation
* @see Constructor#newInstance * @see Constructor#newInstance
*/ */
public static <T> T invokeExactConstructor(Class<T> cls, Object[] args, public static <T> T invokeExactConstructor(Class<T> cls, Object[] args,
@ -197,20 +185,24 @@ public static <T> T invokeExactConstructor(Class<T> cls, Object[] args,
parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY; parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
} }
Constructor<T> ctor = getAccessibleConstructor(cls, parameterTypes); Constructor<T> ctor = getAccessibleConstructor(cls, parameterTypes);
if (null == ctor) { if (ctor == null) {
throw new NoSuchMethodException("No such accessible constructor on object: " throw new NoSuchMethodException(
+ cls.getName()); "No such accessible constructor on object: "+ cls.getName());
} }
return ctor.newInstance(args); return ctor.newInstance(args);
} }
//-----------------------------------------------------------------------
/** /**
* Returns a constructor given a class and signature. * <p>Finds a constructor given a class and signature, checking accessibiilty.</p>
*
* <p>This finds the constructor and ensures that it is accessible.
* The constructor signature must match the parameter types exactly.</p>
* *
* @param <T> the type to be constructed * @param <T> the constructor type
* @param cls the class to be constructed * @param cls the class to find a constructor for, not null
* @param parameterTypes the parameter array * @param parameterTypes the array of parameter types, null treated as empty
* @return null if matching accessible constructor can not be found * @return the constructor, null if no matching accessible constructor found
* @see Class#getConstructor * @see Class#getConstructor
* @see #getAccessibleConstructor(java.lang.reflect.Constructor) * @see #getAccessibleConstructor(java.lang.reflect.Constructor)
*/ */
@ -224,11 +216,13 @@ public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls,
} }
/** /**
* Returns accessible version of the given constructor. * <p>Checks if the specified constructor is accessible.</p>
*
* <p>This simply ensures that the constructor is accessible.</p>
* *
* @param <T> the type to be constructed * @param <T> the constructor type
* @param ctor prototype constructor object. * @param ctor the prototype constructor object, not null
* @return <code>null</code> if accessible constructor can not be found. * @return the constructor, null if no matching accessible constructor found
* @see java.lang.SecurityManager * @see java.lang.SecurityManager
*/ */
public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) { public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) {
@ -237,21 +231,21 @@ public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) {
} }
/** /**
* <p>Finds an accessible constructor with compatible parameters. Compatible * <p>Finds an accessible constructor with compatible parameters.</p>
* parameters mean that every method parameter is assignable from the given *
* parameters. In other words, it finds constructor that will take the * <p>This checks all the constructor and finds one with compatible parameters
* parameters given.</p> * This requires that every parameter is assignable from the given parameter types.
* This is a more flexible search than the normal exact matching algorithm.</p>
* *
* <p>First it checks if there is constructor matching the exact signature. * <p>First it checks if there is a constructor matching the exact signature.
* If no such, all the constructors of the class are tested if their * If not then all the constructors of the class are checked to see if their
* signatures are assignment compatible with the parameter types. The first * signatures are assignment compatible with the parameter types.
* matching constructor is returned.</p> * The first assignment compatible matching constructor is returned.</p>
* *
* @param <T> the type to be constructed * @param <T> the constructor type
* @param cls find constructor for this class * @param cls the class to find a constructor for, not null
* @param parameterTypes find method with compatible parameters * @param parameterTypes find method with compatible parameters
* @return a valid Constructor object. If there's no matching constructor, * @return the constructor, null if no matching accessible constructor found
* returns <code>null</code>.
*/ */
public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls,
Class<?>... parameterTypes) { Class<?>... parameterTypes) {