|
41 | 41 | import java.io.IOException; |
42 | 42 | import java.io.InputStream; |
43 | 43 | import java.io.OutputStream; |
| 44 | +import java.io.Serializable; |
| 45 | +import java.lang.reflect.Field; |
| 46 | +import java.lang.reflect.Type; |
44 | 47 | import java.net.URL; |
45 | 48 | import java.net.URLClassLoader; |
| 49 | +import java.util.List; |
46 | 50 | import java.util.jar.JarOutputStream; |
47 | 51 | import java.util.zip.ZipEntry; |
48 | 52 |
|
@@ -126,6 +130,51 @@ public void testGetArrayClass() { |
126 | 130 | assertNull(ClassUtils.getArrayClass(void.class)); |
127 | 131 | } |
128 | 132 |
|
| 133 | + /** Tests {@link ClassUtils#getTypes(java.lang.reflect.Field, Class)}. */ |
| 134 | + @Test |
| 135 | + public void testGetTypes() { |
| 136 | + final Field field = ClassUtils.getField(Thing.class, "thing"); |
| 137 | + |
| 138 | + // T |
| 139 | + final Type tType = ClassUtils.getGenericType(field, Thing.class); |
| 140 | + assertEquals("capture of ?", tType.toString()); |
| 141 | + |
| 142 | + // N extends Number |
| 143 | + final Type nType = ClassUtils.getGenericType(field, NumberThing.class); |
| 144 | + assertEquals("capture of ?", nType.toString()); |
| 145 | + |
| 146 | + // Integer |
| 147 | + final Type iType = ClassUtils.getGenericType(field, IntegerThing.class); |
| 148 | + assertSame(Integer.class, iType); |
| 149 | + } |
| 150 | + |
| 151 | + /** Tests {@link ClassUtils#getGenericType}. */ |
| 152 | + @Test |
| 153 | + public void testGetGenericType() { |
| 154 | + final Field field = ClassUtils.getField(Thing.class, "thing"); |
| 155 | + |
| 156 | + // Object |
| 157 | + final List<Class<?>> tTypes = ClassUtils.getTypes(field, Thing.class); |
| 158 | + assertEquals(1, tTypes.size()); |
| 159 | + assertSame(Object.class, tTypes.get(0)); |
| 160 | + |
| 161 | + // N extends Number |
| 162 | + final List<Class<?>> nTypes = ClassUtils.getTypes(field, NumberThing.class); |
| 163 | + assertEquals(1, nTypes.size()); |
| 164 | + assertEquals(Number.class, nTypes.get(0)); |
| 165 | + |
| 166 | + // Integer |
| 167 | + final List<Class<?>> iTypes = ClassUtils.getTypes(field, IntegerThing.class); |
| 168 | + assertEquals(1, iTypes.size()); |
| 169 | + assertSame(Integer.class, iTypes.get(0)); |
| 170 | + |
| 171 | + // Serializable & Cloneable |
| 172 | + final List<Class<?>> cTypes = ClassUtils.getTypes(field, ComplexThing.class); |
| 173 | + assertEquals(2, cTypes.size()); |
| 174 | + assertSame(Serializable.class, cTypes.get(0)); |
| 175 | + assertSame(Cloneable.class, cTypes.get(1)); |
| 176 | + } |
| 177 | + |
129 | 178 | @Test |
130 | 179 | public void testUnpackedClass() throws IOException { |
131 | 180 | final File tmpDir = FileUtils.createTemporaryDirectory("class-utils-test", ""); |
@@ -184,4 +233,24 @@ private void assertLoaded(final Class<?> c, final String name) { |
184 | 233 | assertSame(c, ClassUtils.loadClass(name)); |
185 | 234 | } |
186 | 235 |
|
| 236 | + // -- Helper classes -- |
| 237 | + |
| 238 | + public static class Thing<T> { |
| 239 | + public T thing; |
| 240 | + } |
| 241 | + |
| 242 | + public static class NumberThing<N extends Number> extends Thing<N> { |
| 243 | + // NB: No implementation needed. |
| 244 | + } |
| 245 | + |
| 246 | + public static class IntegerThing extends NumberThing<Integer> { |
| 247 | + // NB: No implementation needed. |
| 248 | + } |
| 249 | + |
| 250 | + public static class ComplexThing<T extends Serializable & Cloneable> extends |
| 251 | + Thing<T> |
| 252 | + { |
| 253 | + // NB: No implementation needed. |
| 254 | + } |
| 255 | + |
187 | 256 | } |
0 commit comments