|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.collect.Lists; |
| 5 | +import com.google.common.reflect.Invokable; |
| 6 | +import com.google.common.reflect.TypeToken; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.lang.reflect.Modifier; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.function.Function; |
| 16 | + |
| 17 | +import static junit.framework.TestCase.assertEquals; |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +public class GuavaReflectionUtilsTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void givenTwoGenericList_whenCheckIsAssignableFrom_thenReturnTrueDueToTypeErasure() { |
| 25 | + //given |
| 26 | + ArrayList<String> stringList = Lists.newArrayList(); |
| 27 | + ArrayList<Integer> intList = Lists.newArrayList(); |
| 28 | + |
| 29 | + //when |
| 30 | + boolean result = stringList.getClass().isAssignableFrom(intList.getClass()); |
| 31 | + |
| 32 | + //then |
| 33 | + assertTrue(result); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenTypeToken_whenResolveType_thenShouldResolveProperType() { |
| 38 | + //given |
| 39 | + TypeToken<List<String>> stringListToken = new TypeToken<List<String>>() { |
| 40 | + }; |
| 41 | + TypeToken<List<Integer>> integerListToken = new TypeToken<List<Integer>>() { |
| 42 | + }; |
| 43 | + TypeToken<List<? extends Number>> numberTypeToken = new TypeToken<List<? extends Number>>() { |
| 44 | + }; |
| 45 | + |
| 46 | + //then |
| 47 | + assertFalse(stringListToken.isSubtypeOf(integerListToken)); |
| 48 | + assertFalse(numberTypeToken.isSubtypeOf(integerListToken)); |
| 49 | + assertTrue(integerListToken.isSubtypeOf(numberTypeToken)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenCustomClass_whenCaptureGeneric_thenReturnTypeAtRuntime() { |
| 54 | + //given |
| 55 | + ParametrizedClass<String> parametrizedClass = new ParametrizedClass<String>() { |
| 56 | + }; |
| 57 | + |
| 58 | + //then |
| 59 | + assertEquals(parametrizedClass.type, TypeToken.of(String.class)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenComplexType_whenGetTypeArgument_thenShouldReturnTypeAtRuntime() { |
| 64 | + //given |
| 65 | + TypeToken<Function<Integer, String>> funToken = new TypeToken<Function<Integer, String>>() { |
| 66 | + }; |
| 67 | + |
| 68 | + //when |
| 69 | + TypeToken<?> funResultToken = funToken.resolveType(Function.class.getTypeParameters()[1]); |
| 70 | + |
| 71 | + //then |
| 72 | + assertEquals(funResultToken, TypeToken.of(String.class)); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenMapType_whenGetTypeArgumentOfEntry_thenShouldReturnTypeAtRuntime() throws NoSuchMethodException { |
| 78 | + //given |
| 79 | + TypeToken<Map<String, Integer>> mapToken = new TypeToken<Map<String, Integer>>() { |
| 80 | + }; |
| 81 | + |
| 82 | + //when |
| 83 | + TypeToken<?> entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); |
| 84 | + |
| 85 | + //then |
| 86 | + assertEquals(entrySetToken, new TypeToken<Set<Map.Entry<String, Integer>>>() { |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void givenInvokable_whenCheckPublicMethod_shouldReturnTrue() throws NoSuchMethodException { |
| 92 | + //given |
| 93 | + Method method = CustomClass.class.getMethod("somePublicMethod"); |
| 94 | + Invokable<CustomClass, ?> invokable = new TypeToken<CustomClass>() { |
| 95 | + }.method(method); |
| 96 | + |
| 97 | + //when |
| 98 | + boolean isPublicStandradJava = Modifier.isPublic(method.getModifiers()); |
| 99 | + boolean isPublicGuava = invokable.isPublic(); |
| 100 | + //then |
| 101 | + assertTrue(isPublicStandradJava); |
| 102 | + assertTrue(isPublicGuava); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenInvokable_whenCheckFinalMethod_shouldReturnFalseForIsOverridable() throws NoSuchMethodException { |
| 107 | + //given |
| 108 | + Method method = CustomClass.class.getMethod("notOverridablePublicMethod"); |
| 109 | + Invokable<CustomClass, ?> invokable = new TypeToken<CustomClass>() { |
| 110 | + }.method(method); |
| 111 | + |
| 112 | + //when |
| 113 | + boolean isOverridableStandardJava = (!(Modifier.isFinal(method.getModifiers()) || Modifier.isPrivate(method.getModifiers()) |
| 114 | + || Modifier.isStatic(method.getModifiers()) |
| 115 | + || Modifier.isFinal(method.getDeclaringClass().getModifiers()))); |
| 116 | + boolean isOverridableFinalGauava = invokable.isOverridable(); |
| 117 | + |
| 118 | + //then |
| 119 | + assertFalse(isOverridableStandardJava); |
| 120 | + assertFalse(isOverridableFinalGauava); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void givenListOfType_whenGetReturnRype_shouldCaptureTypeAtRuntime() throws NoSuchMethodException { |
| 125 | + //given |
| 126 | + Method getMethod = List.class.getMethod("get", int.class); |
| 127 | + |
| 128 | + //when |
| 129 | + Invokable<List<Integer>, ?> invokable = new TypeToken<List<Integer>>() { |
| 130 | + }.method(getMethod); |
| 131 | + |
| 132 | + //then |
| 133 | + assertEquals(TypeToken.of(Integer.class), invokable.getReturnType()); // Not Object.class! |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + abstract class ParametrizedClass<T> { |
| 138 | + TypeToken<T> type = new TypeToken<T>(getClass()) { |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + class CustomClass { |
| 143 | + public void somePublicMethod() { |
| 144 | + } |
| 145 | + |
| 146 | + public final void notOverridablePublicMethod() { |
| 147 | + |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments