X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjunit%2Fextensions%2FPA.java;h=e878653e6f455a327fa16b928f56c6042bc43f48;hb=a7169b1c72607f3c9357195b4999869650a2a891;hp=57c873fc67e6452add0df06998420a150f1734b3;hpb=c17287513835107ee932196055aef39364b02e92;p=jalview.git diff --git a/test/junit/extensions/PA.java b/test/junit/extensions/PA.java index 57c873f..e878653 100644 --- a/test/junit/extensions/PA.java +++ b/test/junit/extensions/PA.java @@ -18,8 +18,10 @@ package junit.extensions; import java.util.Collection; /** - * This class is used to access a method or field of an object no matter what the access modifier of the method or field. The syntax - * for accessing fields and methods is out of the ordinary because this class uses reflection to peel away protection. + * This class is used to access a method or field of an object no matter what + * the access modifier of the method or field. The syntax for accessing fields + * and methods is out of the ordinary because this class uses reflection to peel + * away protection. *
* a.k.a. The "ObjectMolester" *
@@ -27,18 +29,21 @@ import java.util.Collection;
* Given the following class MyClass
:
*
*
- * public class MyClass { - * private String name; // private attribute + * public class MyClass + * { + * private String name; // private attribute * - * // private constructor - * private MyClass() { - * super(); - * } + * // private constructor + * private MyClass() + * { + * super(); + * } * - * // private method - * private void setName(String newName) { - * this.name = newName; - * } + * // private method + * private void setName(String newName) + * { + * this.name = newName; + * } * } ** @@ -50,7 +55,8 @@ import java.util.Collection; * String name = PA.getValue(myObj, "name"); * * - * This class extends {@link PrivilegedAccessor} by re-throwing checked {@link Exception}s as {@link RuntimeException}s. + * This class extends {@link PrivilegedAccessor} by re-throwing checked + * {@link Exception}s as {@link RuntimeException}s. * * * @see PrivilegedAccessor @@ -58,274 +64,386 @@ import java.util.Collection; * @author Sebastian Dietrich (sebastian.dietrich@e-movimento.com) * @author Lubos Bistak (lubos@bistak.sk) */ -public class PA { - private final Object instanceOrClass; +public class PA +{ + private final Object instanceOrClass; - /** - * Private constructor to make it impossible to instantiate this class from outside of PA. - * - * @param instanceOrClass - */ - private PA(Object instanceOrClass) { - this.instanceOrClass = instanceOrClass; - } + /** + * Private constructor to make it impossible to instantiate this class from + * outside of PA. + * + * @param instanceOrClass + */ + private PA(Object instanceOrClass) + { + this.instanceOrClass = instanceOrClass; + } - /** - * Returns a string representation of the given object. The string has the following format: "
methodSignature
could be found; or if
- * an argument couldn't be converted to match the expected type
- *
- * @see PrivilegedAccessor#invokeMethod(Object,String,Object[])
- */
- public static Object invokeMethod(final Object instanceOrClass, final String methodSignature, final Object... arguments) {
- try {
- return PrivilegedAccessor.invokeMethod(instanceOrClass, methodSignature, correctVarargs(arguments));
- } catch (Exception e) {
- throw new IllegalArgumentException("Can't invoke method " + methodSignature + " on " + instanceOrClass + " with arguments "
- + arguments, e);
- }
- }
+ /**
+ * Calls a method on the given object instance with the given arguments.
+ * Arguments can be object types or representations for primitives.
+ *
+ * @param instanceOrClass
+ * the instance or class to invoke the method on
+ * @param methodSignature
+ * the name of the method and the parameters methodSignature
could be found; or if an argument
+ * couldn't be converted to match the expected type
+ *
+ * @see PrivilegedAccessor#invokeMethod(Object,String,Object[])
+ */
+ public static Object invokeMethod(final Object instanceOrClass,
+ final String methodSignature, final Object... arguments)
+ {
+ try
+ {
+ return PrivilegedAccessor.invokeMethod(instanceOrClass,
+ methodSignature, correctVarargs(arguments));
+ } catch (Exception e)
+ {
+ throw new IllegalArgumentException(
+ "Can't invoke method " + methodSignature + " on "
+ + instanceOrClass + " with arguments " + arguments,
+ e);
+ }
+ }
- /**
- * Calls a method with the given arguments. Arguments can be object types or representations for primitives.
- *
- * @param methodSignature the name of the method and the parameters methodSignature
could be found; or if
- * an argument couldn't be converted to match the expected type
- * @see PA#invokeMethod(Object, String, Object...)
- */
- public Object invokeMethod(final String methodSignature, final Object... arguments) {
- return PA.invokeMethod(instanceOrClass, methodSignature, arguments);
- }
+ /**
+ * Calls a method with the given arguments. Arguments can be object types or
+ * representations for primitives.
+ *
+ * @param methodSignature
+ * the name of the method and the parameters methodSignature
could be found; or if an argument
+ * couldn't be converted to match the expected type
+ * @see PA#invokeMethod(Object, String, Object...)
+ */
+ public Object invokeMethod(final String methodSignature,
+ final Object... arguments)
+ {
+ return PA.invokeMethod(instanceOrClass, methodSignature, arguments);
+ }
- /**
- * Corrects varargs to their initial form. If you call a method with an object-array as last argument the Java varargs mechanism
- * converts this array in single arguments. This method returns an object array if the arguments are all of the same type.
- *
- * @param arguments the possibly converted arguments of a vararg method
- * @return arguments possibly converted
- */
- private static Object[] correctVarargs(final Object... arguments) {
- if ((arguments == null) || changedByVararg(arguments)) return new Object[] {arguments};
- return arguments;
- }
+ /**
+ * Corrects varargs to their initial form. If you call a method with an
+ * object-array as last argument the Java varargs mechanism converts this
+ * array in single arguments. This method returns an object array if the
+ * arguments are all of the same type.
+ *
+ * @param arguments
+ * the possibly converted arguments of a vararg method
+ * @return arguments possibly converted
+ */
+ private static Object[] correctVarargs(final Object... arguments)
+ {
+ if ((arguments == null) || changedByVararg(arguments))
+ return new Object[] { arguments };
+ return arguments;
+ }
- /**
- * Tests if the arguments were changed by vararg. Arguments are changed by vararg if they are of a non primitive array type. E.g.
- * arguments[] = Object[String[]] is converted to String[] while e.g. arguments[] = Object[int[]] is not converted and stays
- * Object[int[]]
- *
- * Unfortunately we can't detect the difference for arg = Object[primitive] since arguments[] = Object[Object[primitive]] which is
- * converted to Object[primitive] and arguments[] = Object[primitive] which stays Object[primitive]
- *
- * and we can't detect the difference for arg = Object[non primitive] since arguments[] = Object[Object[non primitive]] is converted
- * to Object[non primitive] and arguments[] = Object[non primitive] stays Object[non primitive]
- *
- * @param parameters the parameters
- * @return true if parameters were changes by varargs, false otherwise
- */
- private static boolean changedByVararg(final Object[] parameters) {
- if ((parameters.length == 0) || (parameters[0] == null)) return false;
+ /**
+ * Tests if the arguments were changed by vararg. Arguments are changed by
+ * vararg if they are of a non primitive array type. E.g. arguments[] =
+ * Object[String[]] is converted to String[] while e.g. arguments[] =
+ * Object[int[]] is not converted and stays Object[int[]]
+ *
+ * Unfortunately we can't detect the difference for arg = Object[primitive]
+ * since arguments[] = Object[Object[primitive]] which is converted to
+ * Object[primitive] and arguments[] = Object[primitive] which stays
+ * Object[primitive]
+ *
+ * and we can't detect the difference for arg = Object[non primitive] since
+ * arguments[] = Object[Object[non primitive]] is converted to Object[non
+ * primitive] and arguments[] = Object[non primitive] stays Object[non
+ * primitive]
+ *
+ * @param parameters
+ * the parameters
+ * @return true if parameters were changes by varargs, false otherwise
+ */
+ private static boolean changedByVararg(final Object[] parameters)
+ {
+ if ((parameters.length == 0) || (parameters[0] == null))
+ return false;
- if (parameters.getClass() == Object[].class) return false;
+ if (parameters.getClass() == Object[].class)
+ return false;
- return true;
- }
+ return true;
+ }
- /**
- * Sets the value of the named field. If fieldName denotes a static field, provide a class, otherwise provide an instance. If the
- * fieldName denotes a final field, this method could fail with an IllegalAccessException, since setting the value of final fields
- * at other times than instantiation can have unpredictable effects.
- * String myString = "Test";
- *
- * //setting the private field value
- * PrivilegedAccessor.setValue(myString, "value", new char[] {'T', 'e', 's', 't'});
- *
- * //setting the static final field serialVersionUID - MIGHT FAIL
- * PrivilegedAccessor.setValue(myString.getClass(), "serialVersionUID", 1);
- *
- *
- *
- * @param instanceOrClass the instance or class to set the field
- * @param fieldName the name of the field
- * @param value the new value of the field
- * @throws IllegalArgumentException if the value could not be set. This could be the case if no field with the given
- * fieldName
can be found; or if the field was final
- *
- * @see PrivilegedAccessor.setValue(Object,String,Object)
- */
- public static PA setValue(final Object instanceOrClass, final String fieldName, final Object value) {
- try {
- PrivilegedAccessor.setValue(instanceOrClass, fieldName, value);
- } catch (Exception e) {
- throw new IllegalArgumentException("Can't set value " + value + " at " + fieldName + " in " + instanceOrClass, e);
- }
- return new PA(instanceOrClass);
- }
+ /**
+ * Sets the value of the named field. If fieldName denotes a static field,
+ * provide a class, otherwise provide an instance. If the fieldName denotes a
+ * final field, this method could fail with an IllegalAccessException, since
+ * setting the value of final fields at other times than instantiation can
+ * have unpredictable effects.
+ * String myString = "Test";
+ *
+ * //setting the private field value
+ * PrivilegedAccessor.setValue(myString, "value", new char[] {'T', 'e', 's', 't'});
+ *
+ * //setting the static final field serialVersionUID - MIGHT FAIL
+ * PrivilegedAccessor.setValue(myString.getClass(), "serialVersionUID", 1);
+ *
+ *
+ *
+ * @param instanceOrClass
+ * the instance or class to set the field
+ * @param fieldName
+ * the name of the field
+ * @param value
+ * the new value of the field
+ * @throws IllegalArgumentException
+ * if the value could not be set. This could be the case if no field
+ * with the given fieldName
can be found; or if the
+ * field was final
+ *
+ * @see PrivilegedAccessor.setValue(Object,String,Object)
+ */
+ public static PA setValue(final Object instanceOrClass,
+ final String fieldName, final Object value)
+ {
+ try
+ {
+ PrivilegedAccessor.setValue(instanceOrClass, fieldName, value);
+ } catch (Exception e)
+ {
+ throw new IllegalArgumentException("Can't set value " + value + " at "
+ + fieldName + " in " + instanceOrClass, e);
+ }
+ return new PA(instanceOrClass);
+ }
- /**
- * Sets the value of the named field. If fieldName denotes a static field, provide a class, otherwise provide an instance. If the
- * fieldName denotes a final field, this method could fail with an IllegalAccessException, since setting the value of final fields
- * at other times than instantiation can have unpredictable effects.
- * String myString = "Test";
- *
- * //setting the private field value
- * PrivilegedAccessor.setValue(myString, "value", new char[] {'T', 'e', 's', 't'});
- *
- * //setting the static final field serialVersionUID - MIGHT FAIL
- * PrivilegedAccessor.setValue(myString.getClass(), "serialVersionUID", 1);
- *
- *
- *
- * @param fieldName the name of the field
- * @param value the new value of the field
- * @throws IllegalArgumentException if the value could not be set. This could be the case if no field with the given
- * fieldName
can be found; or if the field was final
- *
- * @see PA.setValue(Object,String,Object)
- */
- public PA setValue(final String fieldName, final Object value) {
- PA.setValue(instanceOrClass, fieldName, value);
- return this;
- }
+ /**
+ * Sets the value of the named field. If fieldName denotes a static field,
+ * provide a class, otherwise provide an instance. If the fieldName denotes a
+ * final field, this method could fail with an IllegalAccessException, since
+ * setting the value of final fields at other times than instantiation can
+ * have unpredictable effects.
+ * String myString = "Test";
+ *
+ * //setting the private field value
+ * PrivilegedAccessor.setValue(myString, "value", new char[] {'T', 'e', 's', 't'});
+ *
+ * //setting the static final field serialVersionUID - MIGHT FAIL
+ * PrivilegedAccessor.setValue(myString.getClass(), "serialVersionUID", 1);
+ *
+ *
+ *
+ * @param fieldName
+ * the name of the field
+ * @param value
+ * the new value of the field
+ * @throws IllegalArgumentException
+ * if the value could not be set. This could be the case if no field
+ * with the given fieldName
can be found; or if the
+ * field was final
+ *
+ * @see PA.setValue(Object,String,Object)
+ */
+ public PA setValue(final String fieldName, final Object value)
+ {
+ PA.setValue(instanceOrClass, fieldName, value);
+ return this;
+ }
}