My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.transylvania.jug.espresso.shots.d20110124;

import org.apache.commons.lang.ObjectUtils;
import org.junit.*;
import static org.junit.Assert.*;
import java.lang.reflect.*;

public class TestForToString {
@Test
public void testReflectionMethod() {
assertFalse(hasToStringViaReflection(NoToString.class));
assertTrue(hasToStringViaReflection(HasToString.class));
}

@Test
public void testInvocationMethod() {
assertFalse(hasToStringViaInvocation(new NoToString()));
assertTrue(hasToStringViaInvocation(new HasToString()));
}

boolean hasToStringViaInvocation(Object o) {
return !ObjectUtils.identityToString(o).equals(o.toString());
}

boolean hasToStringViaReflection(Class<?> clazz) {
Method toString;
try { toString = clazz.getDeclaredMethod("toString"); }
catch (NoSuchMethodException ex) { return false; }
if (!String.class.equals(toString.getReturnType())) { return false; }
return true;
}

private final static class NoToString {
@SuppressWarnings("unused")
public void toString(boolean p) {
// fake method
}
}

private final static class HasToString {
@Override
public String toString() {
return "HasToString";
}
}
}

Change log

r52 by dify.ltd on Jan 24, 2011   Diff
shots for 2011-01-24
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1236 bytes, 46 lines
Powered by Google Project Hosting