Skip to content
Snippets Groups Projects
Commit 9b9157f3 authored by ribeaudc's avatar ribeaudc
Browse files

remove:

- TODO by adding an 'IMPORTANT NOTE'.

SVN: 570
parent bdb26369
No related branches found
No related tags found
No related merge requests found
......@@ -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();
......
......@@ -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));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment