|
| 1 | +package ru.mipt.diht.samples.reflection; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.junit.Test; |
| 5 | +import ru.mipt.diht.samples.model.Car; |
| 6 | + |
| 7 | +import java.lang.reflect.Constructor; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collection; |
| 12 | + |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author s.chebotarev |
| 17 | + * @since 05.11.2015 |
| 18 | + */ |
| 19 | +@Slf4j |
| 20 | +public class SimpleReflectionTest { |
| 21 | + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") |
| 22 | + private Collection<Car> carsHolder = new ArrayList<>(); |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testGeneratedMethods() throws Exception { |
| 26 | + Class<?> enrichedType = Car.class; |
| 27 | + Method[] methods = enrichedType.getDeclaredMethods(); |
| 28 | + for (Method method : methods) { |
| 29 | + log.info("{}({}): {}", method.getName(), method.getParameterTypes(), method.getReturnType().getSimpleName()); |
| 30 | + } |
| 31 | + assertTrue("there are generated methods", methods.length > 0); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testFields() { |
| 36 | + Field[] fields = Car.class.getDeclaredFields(); |
| 37 | + for (Field field : fields) { |
| 38 | + log.info("{}: {}", field.getName(), field.getType().getSimpleName()); |
| 39 | + } |
| 40 | + assertTrue("there are some fields", fields.length > 0); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testConstuctors() { |
| 45 | + Constructor[] constructors = Car.class.getDeclaredConstructors(); |
| 46 | + for (Constructor constructor : constructors) { |
| 47 | + log.info("{}", constructor); |
| 48 | + } |
| 49 | + assertTrue("there are some constructors", constructors.length > 0); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testDynamicObjectClass() { |
| 54 | + Object obj = new Car(); |
| 55 | + assertEquals("car class", Car.class, obj.getClass()); |
| 56 | + assertSame("car class", Car.class, obj.getClass()); |
| 57 | + assertEquals("string class", String.class, "123".getClass()); |
| 58 | + assertSame("string class", String.class, "123".getClass()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testParentClasses() { |
| 63 | + Class type = Car.class; |
| 64 | + Class parent = type.getSuperclass(); |
| 65 | + assertNotNull("Car.super", parent); |
| 66 | + |
| 67 | + log.info("inspecting Car.class superclasses"); |
| 68 | + for (; type != null; type = type.getSuperclass()) { |
| 69 | + log.info("=> {}", type.getSimpleName()); |
| 70 | + } |
| 71 | + |
| 72 | + log.info("inspecting Class.class superclasses"); |
| 73 | + type = Class.class; |
| 74 | + for (; type != null; type = type.getSuperclass()) { |
| 75 | + log.info("=> {}", type.getSimpleName()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testGenericsInfo() throws Exception { |
| 81 | + log.info("{}", getClass().getDeclaredField("carsHolder").getGenericType()); |
| 82 | + log.info("{}", carsHolder.getClass().getTypeParameters()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testPublicAccess() throws Exception { |
| 87 | + Car car = new Car(); |
| 88 | + car.setId("id123"); |
| 89 | + Object res = Car.class.getMethod("getId").invoke(car); |
| 90 | + log.info("call res: {}", res); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expected = IllegalAccessException.class) |
| 94 | + public void testIllegalPrivateAccess() throws Exception { |
| 95 | + Car car = new Car(); |
| 96 | + car.setId("id123"); |
| 97 | + Object res = Car.class.getDeclaredField("id").get(car); |
| 98 | + log.info("field access res: {}", res); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testSetAccessible() throws Exception { |
| 103 | + Car car = new Car(); |
| 104 | + car.setId("id123"); |
| 105 | + Field field = Car.class.getDeclaredField("id"); |
| 106 | + field.setAccessible(true); // only for this instance |
| 107 | + Object res = field.get(car); |
| 108 | + log.info("field access res: {}", res); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testConstructor() throws Exception { |
| 113 | + Car car1 = new Car(); |
| 114 | + Car car2 = Car.class.newInstance(); |
| 115 | + assertNotNull(car2); |
| 116 | + assertEquals(car1, car2); |
| 117 | + assertNotSame(car1, car2); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testAnnotation() throws Exception { |
| 122 | + Test annotation = getClass().getMethod("testIllegalPrivateAccess").getAnnotation(Test.class); |
| 123 | + assertNotNull(annotation); |
| 124 | + assertEquals("expected exception", IllegalAccessException.class, annotation.expected()); |
| 125 | + assertEquals("timeout", 0, annotation.timeout()); |
| 126 | + assertEquals("timeout", 0L, annotation.annotationType().getMethod("timeout").getDefaultValue()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testClassForName() throws Exception { |
| 131 | + assertEquals("Car class found by name", Car.class, Class.forName("ru.mipt.diht.samples.model.Car")); |
| 132 | + } |
| 133 | +} |
0 commit comments