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 423fb3e1c52d87696fdb40909e346cc14e5d1f56..1dc886c0e430bda91f7811a3701053236298e9dd 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClassUtils.java @@ -78,16 +78,19 @@ public final class ClassUtils { 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. - * @return <code>null</code> if none could be found. * </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. + * + * @see StackTraceElement#getMethodName() + * @return <code>null</code> if none could be found. */ - // TODO 2007-06-14, Christian Ribeaud: 'method.getName()' is not specific enough. You have to used kind of - // or part of 'Method.toGenericString()'. public final static Method getMethodOnStack(int level) { StackTraceElement[] elements = new Throwable().getStackTrace(); 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 2a65473aaad310effeb811b0b73e01fde09a2d75..55378ce94b82115d6adf3bafab597ff04cf0ef03 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClassUtilsTest.java @@ -39,13 +39,32 @@ public class ClassUtilsTest 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() + { + StackTraceElement[] elements = new Throwable().getStackTrace(); + return elements[0].getMethodName(); + } + + public final String getMethodName(Object one, Object two) + { + StackTraceElement[] elements = new Throwable().getStackTrace(); + return elements[0].getMethodName(); + } + } + private static class SimpleBean { private final int number; + private final String string; - + SimpleBean(int number, String string) { this.number = number; @@ -61,18 +80,18 @@ public class ClassUtilsTest { return string; } - + String getIgnoreThisBecauseItIsNotPublic() { AssertJUnit.fail("Should be ignore because not public"); return null; } } - + @Test public void testCheckGettersForNullOK() { - final SimpleBean bean = new SimpleBean(1, ""); + final SimpleBean bean = new SimpleBean(1, ""); assert ClassUtils.checkGettersNotNull(bean) == bean; } @@ -85,14 +104,14 @@ public class ClassUtilsTest @Test(expectedExceptions = IllegalStateException.class) public void testCheckGettersForNullStringNull() { - final SimpleBean bean = new SimpleBean(1, null); + final SimpleBean bean = new SimpleBean(1, null); ClassUtils.checkGettersNotNull(bean); } @Test(expectedExceptions = IllegalStateException.class) public void testCheckGettersForNullInt0() { - final SimpleBean bean = new SimpleBean(0, "test"); + final SimpleBean bean = new SimpleBean(0, "test"); ClassUtils.checkGettersNotNull(bean); } @@ -105,8 +124,8 @@ public class ClassUtilsTest @Test public void testCheckGettersForNullListOK() { - final SimpleBean bean1 = new SimpleBean(1, "test"); - final SimpleBean bean2 = new SimpleBean(5, "test2"); + final SimpleBean bean1 = new SimpleBean(1, "test"); + final SimpleBean bean2 = new SimpleBean(5, "test2"); final List<SimpleBean> beanList = Arrays.asList(bean1, bean2); assert ClassUtils.checkGettersNotNull(beanList) == beanList; } @@ -114,8 +133,8 @@ public class ClassUtilsTest @Test(expectedExceptions = IllegalStateException.class) public void testCheckGettersForNullListInt0() { - final SimpleBean bean1 = new SimpleBean(1, "test"); - final SimpleBean bean2 = new SimpleBean(0, "test2"); + final SimpleBean bean1 = new SimpleBean(1, "test"); + final SimpleBean bean2 = new SimpleBean(0, "test2"); ClassUtils.checkGettersNotNull(Arrays.asList(bean1, bean2)); }