| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MethodSignature |
|
| 0.0;0 | ||||
| MethodSignature$AnyClassType |
|
| 0.0;0 |
| 1 | package biz.xsoftware.mock; | |
| 2 | ||
| 3 | /** | |
| 4 | * This class represents class method and it's arguments | |
| 5 | * | |
| 6 | * So, for example if you have the following method: <code> | |
| 7 | * public void foo(String arg0, String arg1) | |
| 8 | * </code> | |
| 9 | * | |
| 10 | * This object will contain the name, "foo" and the arguments in an array, | |
| 11 | * {String, String} | |
| 12 | * | |
| 13 | * @author Brian Freeman | |
| 14 | * @since Aug 1, 2007 | |
| 15 | */ | |
| 16 | public final class MethodSignature { | |
| 17 | ||
| 18 | 18 | public static final Class<?> ANY_CLASS_TYPE = AnyClassType.class; |
| 19 | ||
| 20 | /** | |
| 21 | * The name of the method | |
| 22 | */ | |
| 23 | private final String name; | |
| 24 | ||
| 25 | /** | |
| 26 | * The argument class types. This attribute can be null in the intention to | |
| 27 | * obtain any method that matches just the name. If the class or classes | |
| 28 | * have multiple matches for that method name then an exception will be | |
| 29 | * thrown. | |
| 30 | */ | |
| 31 | private final Class<?>[] paramTypes; | |
| 32 | ||
| 33 | /** | |
| 34 | * Creates a new instance using the given method name and creates an | |
| 35 | * empty array for the method's arguments | |
| 36 | */ | |
| 37 | public MethodSignature(final String name) { | |
| 38 | 5 | this(name, new Class<?>[0]); |
| 39 | 5 | } |
| 40 | ||
| 41 | /** | |
| 42 | * Creates a new instance and clones the args given | |
| 43 | * | |
| 44 | * @param name The name of the method | |
| 45 | * @param args The method's arguments, individual arguments are not allowed | |
| 46 | * to be null. However, a null array is allowed for backwards | |
| 47 | * compatibility by casting it MethodSignature("methodName", | |
| 48 | * (Class<?>[]) null); | |
| 49 | */ | |
| 50 | 223 | public MethodSignature(final String name, final Class<?> ... args) { |
| 51 | 223 | this.name = name; |
| 52 | 223 | if (args != null) { |
| 53 | 346 | for (Class<?> arg : args) { |
| 54 | 160 | if (arg == null) { |
| 55 | 0 | throw new IllegalArgumentException("arguments passed are not allowed to be null"); |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | 186 | this.paramTypes = args.clone(); |
| 60 | 186 | } else { |
| 61 | 37 | this.paramTypes = null; |
| 62 | } | |
| 63 | 223 | } |
| 64 | ||
| 65 | public Class<?>[] getParamTypes() { | |
| 66 | 881 | if (paramTypes!=null) { |
| 67 | 772 | return paramTypes.clone(); |
| 68 | } else { | |
| 69 | 109 | return null; |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | public String getName() { | |
| 74 | 5058 | return name; |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Builds and returns a String representation of this obj | |
| 79 | * @return A String representation of this object | |
| 80 | */ | |
| 81 | public String toString() | |
| 82 | { | |
| 83 | 3 | StringBuilder sb = new StringBuilder(); |
| 84 | 3 | sb.append(name).append("("); |
| 85 | ||
| 86 | 3 | if (paramTypes == null) { |
| 87 | 1 | sb.append("args unknown"); |
| 88 | 1 | } else if(paramTypes != null && paramTypes.length > 0) { |
| 89 | 1 | int i = 0; |
| 90 | 1 | for(; i < paramTypes.length-1; i++) { |
| 91 | 0 | Class<?> c = paramTypes[i]; |
| 92 | 0 | sb.append(c.getSimpleName()).append(","); |
| 93 | } | |
| 94 | 1 | sb.append(paramTypes[i].getSimpleName()); |
| 95 | } | |
| 96 | ||
| 97 | 3 | sb.append(")"); |
| 98 | 3 | return sb.toString(); |
| 99 | } | |
| 100 | ||
| 101 | 0 | private static class AnyClassType {}; |
| 102 | } |