| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.assertion; |
| 12 | |
| 13 | import java.lang.reflect.Constructor; |
| 14 | import java.lang.reflect.Field; |
| 15 | import java.lang.reflect.Method; |
| 16 | |
| 17 | /** |
| 18 | * Provides the ability to make assertions on Java reflection types. |
| 19 | * |
| 20 | * @author %javadoc.author.tag% |
| 21 | * @version %version%<br/> |
| 22 | * <i>Build Number %build.number%</i><br/> |
| 23 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
| 24 | */ |
| 25 | public final class Reflection |
| 26 | { |
| 27 | private Reflection() |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Asserts that the given class has a constructor with the given parameter types. |
| 34 | * |
| 35 | * @see Class#getConstructor(Class[]) |
| 36 | * @param c The class to assert has the given constructor. |
| 37 | * @param parameterTypes The parameter types of the constructor. |
| 38 | * @param message The assertion message. |
| 39 | * @return The constructor that the given class has. |
| 40 | * @throws AssertionException If the given class does not have a constructor with the given parameter types. |
| 41 | */ |
| 42 | public static Constructor<?> assertHasConstructor(final Class<?> c, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 43 | { |
| 44 | try |
| 45 | { |
| 46 | return c.getConstructor(parameterTypes); |
| 47 | } |
| 48 | catch(NoSuchMethodException e) |
| 49 | { |
| 50 | throw new AssertionException(e, message); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Asserts that the given class does not have a constructor with the given parameter types. |
| 56 | * |
| 57 | * @see Class#getConstructor(Class[]) |
| 58 | * @param c The class to assert does not have the given constructor. |
| 59 | * @param parameterTypes The parameter types of the constructor. |
| 60 | * @param message The assertion message. |
| 61 | * @throws AssertionException If the given class has a constructor with the given parameter types. |
| 62 | */ |
| 63 | public static void assertNotHasConstructor(final Class<?> c, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 64 | { |
| 65 | try |
| 66 | { |
| 67 | c.getConstructor(parameterTypes); |
| 68 | throw new AssertionException(message); |
| 69 | } |
| 70 | catch(NoSuchMethodException e) |
| 71 | { |
| 72 | // test passed |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Asserts that the given class has a declared constructor with the given parameter types. |
| 78 | * |
| 79 | * @see Class#getDeclaredConstructor(Class[]) |
| 80 | * @param c The class to assert has the given declared constructor. |
| 81 | * @param parameterTypes The parameter types of the declared constructor. |
| 82 | * @param message The assertion message. |
| 83 | * @return The declared constructor that the given class has. |
| 84 | * @throws AssertionException If the given class does not have a declared constructor with the given parameter |
| 85 | * types. |
| 86 | */ |
| 87 | public static Constructor<?> assertHasDeclaredConstructor(final Class<?> c, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 88 | { |
| 89 | try |
| 90 | { |
| 91 | return c.getDeclaredConstructor(parameterTypes); |
| 92 | } |
| 93 | catch(NoSuchMethodException e) |
| 94 | { |
| 95 | throw new AssertionException(e, message); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Asserts that the given class does not have a declared constructor with the given parameter types. |
| 101 | * |
| 102 | * @see Class#getDeclaredConstructor(Class[]) |
| 103 | * @param c The class to assert does not have the given declared constructor. |
| 104 | * @param parameterTypes The parameter types of the declared constructor. |
| 105 | * @param message The assertion message. |
| 106 | * @throws AssertionException If the given class has a declared constructor with the given parameter |
| 107 | * types. |
| 108 | */ |
| 109 | public static void assertNotHasDeclaredConstructor(final Class<?> c, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 110 | { |
| 111 | try |
| 112 | { |
| 113 | c.getDeclaredConstructor(parameterTypes); |
| 114 | throw new AssertionException(message); |
| 115 | } |
| 116 | catch(NoSuchMethodException e) |
| 117 | { |
| 118 | // test passed |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Asserts that the given class has a method with the given name and parameter types. |
| 124 | * |
| 125 | * @see Class#getMethod(String, Class[]) |
| 126 | * @param c The class to assert has the given method. |
| 127 | * @param name The name of the method. |
| 128 | * @param parameterTypes The parameter types of the method. |
| 129 | * @param message The assertion message. |
| 130 | * @return The method that the given class has. |
| 131 | * @throws AssertionException If the given class does not have a method with the given name and parameter types. |
| 132 | */ |
| 133 | public static Method assertHasMethod(final Class<?> c, final String name, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 134 | { |
| 135 | try |
| 136 | { |
| 137 | return c.getMethod(name, parameterTypes); |
| 138 | } |
| 139 | catch(NoSuchMethodException e) |
| 140 | { |
| 141 | throw new AssertionException(e, message); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Asserts that the given class does not have a method with the given name and parameter types. |
| 147 | * |
| 148 | * @see Class#getMethod(String, Class[]) |
| 149 | * @param c The class to assert does not have the given method. |
| 150 | * @param name The name of the method. |
| 151 | * @param parameterTypes The parameter types of the method. |
| 152 | * @param message The assertion message. |
| 153 | * @throws AssertionException If the given class has a method with the given name and parameter types. |
| 154 | */ |
| 155 | public static void assertNotHasMethod(final Class<?> c, final String name, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 156 | { |
| 157 | try |
| 158 | { |
| 159 | c.getMethod(name, parameterTypes); |
| 160 | throw new AssertionException(message); |
| 161 | } |
| 162 | catch(NoSuchMethodException e) |
| 163 | { |
| 164 | // test passed |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Asserts that the given class has a declared method with the given name and parameter types. |
| 170 | * |
| 171 | * @see Class#getDeclaredMethod(String, Class[]) |
| 172 | * @param c The class to assert has the given method. |
| 173 | * @param name The name of the method. |
| 174 | * @param parameterTypes The parameter types of the method. |
| 175 | * @param message The assertion message. |
| 176 | * @return The method that the given class has. |
| 177 | * @throws AssertionException If the given class does not have a declared method with the given name and parameter |
| 178 | * types. |
| 179 | */ |
| 180 | public static Method assertHasDeclaredMethod(final Class<?> c, final String name, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 181 | { |
| 182 | try |
| 183 | { |
| 184 | return c.getDeclaredMethod(name, parameterTypes); |
| 185 | } |
| 186 | catch(NoSuchMethodException e) |
| 187 | { |
| 188 | throw new AssertionException(e, message); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Asserts that the given class does not have a declared method with the given name and parameter types. |
| 194 | * |
| 195 | * @see Class#getDeclaredMethod(String, Class[]) |
| 196 | * @param c The class to assert does not have the given method. |
| 197 | * @param name The name of the method. |
| 198 | * @param parameterTypes The parameter types of the method. |
| 199 | * @param message The assertion message. |
| 200 | * @throws AssertionException If the given class has a declared method with the given name and parameter types. |
| 201 | */ |
| 202 | public static void assertNotHasDeclaredMethod(final Class<?> c, final String name, final Class<?>[] parameterTypes, final Object... message) throws AssertionException |
| 203 | { |
| 204 | try |
| 205 | { |
| 206 | c.getDeclaredMethod(name, parameterTypes); |
| 207 | throw new AssertionException(message); |
| 208 | } |
| 209 | catch(NoSuchMethodException e) |
| 210 | { |
| 211 | // test passed |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Asserts that the given class has a field with the given name. |
| 217 | * |
| 218 | * @see Class#getField(String) |
| 219 | * @param c The class to assert has the given field. |
| 220 | * @param name The name of the field. |
| 221 | * @param message The assertion message. |
| 222 | * @return The field that the given class has. |
| 223 | * @throws AssertionException If the given class does not have a field with the given name. |
| 224 | */ |
| 225 | public static Field assertHasField(final Class<?> c, final String name, final Object... message) throws AssertionException |
| 226 | { |
| 227 | try |
| 228 | { |
| 229 | return c.getField(name); |
| 230 | } |
| 231 | catch(NoSuchFieldException e) |
| 232 | { |
| 233 | throw new AssertionException(e, message); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Asserts that the given class does not have a field with the given name. |
| 239 | * |
| 240 | * @see Class#getField(String) |
| 241 | * @param c The class to assert does not have the given field. |
| 242 | * @param name The name of the field. |
| 243 | * @param message The assertion message. |
| 244 | * @throws AssertionException If the given class has a field with the given name. |
| 245 | */ |
| 246 | public static void assertNotHasField(final Class<?> c, final String name, final Object... message) throws AssertionException |
| 247 | { |
| 248 | try |
| 249 | { |
| 250 | c.getField(name); |
| 251 | throw new AssertionException(message); |
| 252 | } |
| 253 | catch(NoSuchFieldException e) |
| 254 | { |
| 255 | // test passed |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Asserts that the given class has a declared field with the given name. |
| 261 | * |
| 262 | * @see Class#getDeclaredField(String) |
| 263 | * @param c The class to assert has the given field. |
| 264 | * @param name The name of the field. |
| 265 | * @param message The assertion message. |
| 266 | * @return The field that the given class has. |
| 267 | * @throws AssertionException If the given class does not have a declared field with the given name. |
| 268 | */ |
| 269 | public static Field assertHasDeclaredField(final Class<?> c, final String name, final Object... message) throws AssertionException |
| 270 | { |
| 271 | try |
| 272 | { |
| 273 | return c.getDeclaredField(name); |
| 274 | } |
| 275 | catch(NoSuchFieldException e) |
| 276 | { |
| 277 | throw new AssertionException(e, message); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Asserts that the given class does not have a declared field with the given name. |
| 283 | * |
| 284 | * @see Class#getDeclaredField(String) |
| 285 | * @param c The class to assert does not have the given field. |
| 286 | * @param name The name of the field. |
| 287 | * @param message The assertion message. |
| 288 | * @throws AssertionException If the given class has a declared field with the given name. |
| 289 | */ |
| 290 | public static void assertNotHasDeclaredField(final Class<?> c, final String name, final Object... message) throws AssertionException |
| 291 | { |
| 292 | try |
| 293 | { |
| 294 | c.getDeclaredField(name); |
| 295 | throw new AssertionException(message); |
| 296 | } |
| 297 | catch(NoSuchFieldException e) |
| 298 | { |
| 299 | // test passed |
| 300 | } |
| 301 | } |
| 302 | } |