diff --git a/common/source/java/ch/systemsx/cisd/common/exceptions/Status.java b/common/source/java/ch/systemsx/cisd/common/exceptions/Status.java index 5baa9e068e6c4bf0c5f7817dfced1d88b8d57302..a66baee9229fe450193d1128e09550e3c8d0937d 100644 --- a/common/source/java/ch/systemsx/cisd/common/exceptions/Status.java +++ b/common/source/java/ch/systemsx/cisd/common/exceptions/Status.java @@ -39,12 +39,12 @@ public class Status * * @param retriable If <code>true</code>, the error will be marked 'retriable'. */ - public static Status createError(boolean retriable) + public static Status createError(final boolean retriable) { return new Status(getErrorFlag(retriable), ""); } - public static Status createError(boolean retriable, String message) + public static Status createError(final boolean retriable, final String message) { assert message != null; @@ -56,14 +56,14 @@ public class Status return new Status(StatusFlag.ERROR, ""); } - public static Status createError(String message) + public static Status createError(final String message) { assert message != null; return new Status(StatusFlag.ERROR, message); } - public static Status createError(String messageTemplate, Object ... args) + public static Status createError(final String messageTemplate, final Object ... args) { assert messageTemplate != null; @@ -75,26 +75,26 @@ public class Status return new Status(StatusFlag.RETRIABLE_ERROR, ""); } - public static Status createRetriableError(String message) + public static Status createRetriableError(final String message) { assert message != null; return new Status(StatusFlag.RETRIABLE_ERROR, message); } - public static Status createRetriableError(String messageTemplate, Object ... args) + public static Status createRetriableError(final String messageTemplate, final Object ... args) { assert messageTemplate != null; return new Status(StatusFlag.RETRIABLE_ERROR, String.format(messageTemplate, args)); } - protected static StatusFlag getErrorFlag(boolean retriable) + protected static StatusFlag getErrorFlag(final boolean retriable) { return retriable ? StatusFlag.RETRIABLE_ERROR : StatusFlag.ERROR; } - protected Status(StatusFlag flag, String message) + protected Status(final StatusFlag flag, final String message) { assert flag != null; assert StatusFlag.OK.equals(flag) || message != null; @@ -133,7 +133,7 @@ public class Status // @Override - public boolean equals(Object obj) + public boolean equals(final Object obj) { if (obj == this) { diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java index 172f5ea84befd833e74ae2cb49ad98dbb38bfdb5..c1797dc82857f407a2d51f2c99f6832c82fec57b 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java @@ -71,60 +71,6 @@ public final class ClassUtils return org.apache.commons.lang.ClassUtils.getAllSuperclasses(object.getClass()); } - /** - * Returns the currently called <code>Method</code>. - * <p> - * Returns <code>null</code> if none could be found. - * </p> - */ - public final static Method getCurrentMethod() - { - return getMethodOnStack(2); - } - - /** - * Returns the <code>Method</code> on the stack of <var>level</var>. - * <p> - * <code>level=0</code> is this method itself, <code>level=1</code> is the method that - * called it and so forth. This method internally uses {@link Class#getMethods()} to retrieve - * the <code>Method</code> (meaning that <code>private</code> methods will not be found). - * </p> - * <p> - * IMPORTANT NOTE: You should carefully use this method in a class having more than one method - * with the same name. The internal idea used here (<code>new Throwable().getStackTrace()</code>) - * only returns a method name and does not make any other consideration. - * </p> - * - * @see StackTraceElement#getMethodName() - * @return <code>null</code> if none could be found. - */ - public final static Method getMethodOnStack(final int level) - { - final StackTraceElement[] elements = new Throwable().getStackTrace(); - if (elements.length <= level) - { - return null; - } - final StackTraceElement element = elements[level]; - final String methodName = element.getMethodName(); - try - { - final Method[] methods = Class.forName(element.getClassName()).getMethods(); - for (final Method method : methods) - { - if (method.getName().equals(methodName)) - { - return method; - } - } - // SecurityException, ClassNotFoundException - } catch (final Exception ex) - { - throw CheckedExceptionTunnel.wrapIfNecessary(ex); - } - return null; - } - /** * Creates a new instance of a class specified by its fully-qualified name. * @@ -351,4 +297,15 @@ public final class ClassUtils } return field; } + + /** + * Describes given <var>method</var> in following format: + * <code><class-name>.<method-name></code>, for instance + * <code>Object.hashCode</code>. + */ + public final static String describeMethod(final Method method) + { + assert method != null : "Unspecified method"; + return method.getDeclaringClass().getSimpleName() + "." + method.getName(); + } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java index 745c9d467257fa0b59168d2322db76dfe30de1b0..9c195c5f53351e755396a099e93bc9fe5611a540 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java @@ -25,6 +25,7 @@ import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; import java.io.IOException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -58,52 +59,6 @@ public final class ClassUtilsTest assertEquals(false, iterator.hasNext()); } - /** - * Test method for {@link ch.systemsx.cisd.common.utilities.ClassUtils#getCurrentMethod()}. - */ - @Test - public final void testGetCurrentMethod() - { - assertEquals("testGetCurrentMethod", ClassUtils.getCurrentMethod().getName()); - // Border cases - assertEquals(new SameMethodName().getMethodName(), new SameMethodName().getMethodName( - new Object(), new Object())); - } - - private final static class SameMethodName - { - - public final String getMethodName() - { - final StackTraceElement[] elements = new Throwable().getStackTrace(); - return elements[0].getMethodName(); - } - - public final String getMethodName(final Object one, final Object two) - { - final StackTraceElement[] elements = new Throwable().getStackTrace(); - return elements[0].getMethodName(); - } - } - - @Test - public final void testGetMethodOnStack() - { - assertEquals("getMethodOnStack", ClassUtils.getMethodOnStack(0).getName()); - assertEquals("testGetMethodOnStack", ClassUtils.getMethodOnStack(1).getName()); - assertNull(ClassUtils.getMethodOnStack(2)); - privateMethodOnStack(); - } - - private final void privateMethodOnStack() - { - // If <code>Class.getDeclaredMethods</code> were used instead of - // <code>Class.getDeclaredMethods</code>, - // we will have 'ch.systemsx.cisd.common.utilities.ClassUtilsTest.privateMethodOnStack()' - // here. - assertNull(ClassUtils.getMethodOnStack(1)); - } - @Test public void testCreateWithDefaultConstructor() { @@ -205,6 +160,14 @@ public final class ClassUtilsTest assertSame(object, myExtendedClass.finalObject); } + @Test + public final void testDescribeMethod() + { + final Method method = Object.class.getMethods()[0]; + final String methodDescription = ClassUtils.describeMethod(method); + assertEquals("Object.hashCode", methodDescription); + } + // // Helper Classes //