From f57a7ddaed01142142c791f3dca9314081baf028 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 3 Mar 2017 06:15:34 +0100 Subject: [PATCH] Bael 641 (#1282) * BEEL-641 guava reflection utils code * BEEL-641 fix formatting * BEEL-641 add assertion on subttype number -> integer * BEEL-641 rename class * BEEL-641 formatting * BEEL-641 add comparison using standard java reflecetion api and guava one --- .../guava/GuavaReflectionUtilsTest.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java diff --git a/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java new file mode 100644 index 0000000000..e59a682e08 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java @@ -0,0 +1,150 @@ +package org.baeldung.guava; + + +import com.google.common.collect.Lists; +import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeToken; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class GuavaReflectionUtilsTest { + + @Test + public void givenTwoGenericList_whenCheckIsAssignableFrom_thenReturnTrueDueToTypeErasure() { + //given + ArrayList stringList = Lists.newArrayList(); + ArrayList intList = Lists.newArrayList(); + + //when + boolean result = stringList.getClass().isAssignableFrom(intList.getClass()); + + //then + assertTrue(result); + } + + @Test + public void givenTypeToken_whenResolveType_thenShouldResolveProperType() { + //given + TypeToken> stringListToken = new TypeToken>() { + }; + TypeToken> integerListToken = new TypeToken>() { + }; + TypeToken> numberTypeToken = new TypeToken>() { + }; + + //then + assertFalse(stringListToken.isSubtypeOf(integerListToken)); + assertFalse(numberTypeToken.isSubtypeOf(integerListToken)); + assertTrue(integerListToken.isSubtypeOf(numberTypeToken)); + } + + @Test + public void givenCustomClass_whenCaptureGeneric_thenReturnTypeAtRuntime() { + //given + ParametrizedClass parametrizedClass = new ParametrizedClass() { + }; + + //then + assertEquals(parametrizedClass.type, TypeToken.of(String.class)); + } + + @Test + public void givenComplexType_whenGetTypeArgument_thenShouldReturnTypeAtRuntime() { + //given + TypeToken> funToken = new TypeToken>() { + }; + + //when + TypeToken funResultToken = funToken.resolveType(Function.class.getTypeParameters()[1]); + + //then + assertEquals(funResultToken, TypeToken.of(String.class)); + } + + + @Test + public void givenMapType_whenGetTypeArgumentOfEntry_thenShouldReturnTypeAtRuntime() throws NoSuchMethodException { + //given + TypeToken> mapToken = new TypeToken>() { + }; + + //when + TypeToken entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); + + //then + assertEquals(entrySetToken, new TypeToken>>() { + }); + } + + @Test + public void givenInvokable_whenCheckPublicMethod_shouldReturnTrue() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("somePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isPublicStandradJava = Modifier.isPublic(method.getModifiers()); + boolean isPublicGuava = invokable.isPublic(); + //then + assertTrue(isPublicStandradJava); + assertTrue(isPublicGuava); + } + + @Test + public void givenInvokable_whenCheckFinalMethod_shouldReturnFalseForIsOverridable() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("notOverridablePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isOverridableStandardJava = (!(Modifier.isFinal(method.getModifiers()) || Modifier.isPrivate(method.getModifiers()) + || Modifier.isStatic(method.getModifiers()) + || Modifier.isFinal(method.getDeclaringClass().getModifiers()))); + boolean isOverridableFinalGauava = invokable.isOverridable(); + + //then + assertFalse(isOverridableStandardJava); + assertFalse(isOverridableFinalGauava); + } + + @Test + public void givenListOfType_whenGetReturnRype_shouldCaptureTypeAtRuntime() throws NoSuchMethodException { + //given + Method getMethod = List.class.getMethod("get", int.class); + + //when + Invokable, ?> invokable = new TypeToken>() { + }.method(getMethod); + + //then + assertEquals(TypeToken.of(Integer.class), invokable.getReturnType()); // Not Object.class! + } + + + abstract class ParametrizedClass { + TypeToken type = new TypeToken(getClass()) { + }; + } + + class CustomClass { + public void somePublicMethod() { + } + + public final void notOverridablePublicMethod() { + + } + } +}