From 223c6795358004aa11cbd98c6662f9584f8bf033 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 27 Dec 2023 11:37:40 -0500 Subject: [PATCH] Bael 7184 (#15325) * implementing BAEL-6833 * implementation for BAEL-7115 * feedback fixes * Moving code folder as per feedback * renaming logic class folder name * code implementation --------- Co-authored-by: technoddy --- .../core-java-lang-oop-generics/pom.xml | 4 +- .../CollectionWithAndWithoutGenerics.java | 33 +++++++++++++ .../ContainerTypeFromReflection.java | 13 +++++ .../ContainerTypeFromTypeParameter.java | 13 +++++ .../generics/classtype/TypeToken.java | 17 +++++++ .../classtype/GenericClassTypeUnitTest.java | 47 +++++++++++++++++++ 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/CollectionWithAndWithoutGenerics.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromReflection.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromTypeParameter.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/TypeToken.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/classtype/GenericClassTypeUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-generics/pom.xml b/core-java-modules/core-java-lang-oop-generics/pom.xml index fe6b5a9f36..9b01a97918 100644 --- a/core-java-modules/core-java-lang-oop-generics/pom.xml +++ b/core-java-modules/core-java-lang-oop-generics/pom.xml @@ -37,8 +37,8 @@ - 1.8 - 1.8 + 17 + 17 \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/CollectionWithAndWithoutGenerics.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/CollectionWithAndWithoutGenerics.java new file mode 100644 index 0000000000..f0435559a7 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/CollectionWithAndWithoutGenerics.java @@ -0,0 +1,33 @@ +package com.baeldung.generics.classtype; + +import java.util.ArrayList; +import java.util.List; + +public class CollectionWithAndWithoutGenerics { + + public static void main(String[] args) { + withoutGenerics(); + } + + private static void withoutGenerics() { + List container = new ArrayList(); + container.add(1); + container.add("2"); + container.add("string"); + + for (int i = 0; i < container.size(); i++) { + int val = (int) container.get(i); + } + } + + public static void withGenerics() { + List container = new ArrayList(); + container.add(1); + container.add(2); + container.add(3); + + for (int i = 0; i < container.size(); i++) { + int val = container.get(i); + } + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromReflection.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromReflection.java new file mode 100644 index 0000000000..bca47748d3 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromReflection.java @@ -0,0 +1,13 @@ +package com.baeldung.generics.classtype; + +public class ContainerTypeFromReflection { + private T content; + + public ContainerTypeFromReflection(T content) { + this.content = content; + } + + public Class getClazz() { + return this.content.getClass(); + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromTypeParameter.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromTypeParameter.java new file mode 100644 index 0000000000..dc28ccf13a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/ContainerTypeFromTypeParameter.java @@ -0,0 +1,13 @@ +package com.baeldung.generics.classtype; + +public class ContainerTypeFromTypeParameter { + private Class clazz; + + public ContainerTypeFromTypeParameter(Class clazz) { + this.clazz = clazz; + } + + public Class getClazz() { + return this.clazz; + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/TypeToken.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/TypeToken.java new file mode 100644 index 0000000000..3a539ba652 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/classtype/TypeToken.java @@ -0,0 +1,17 @@ +package com.baeldung.generics.classtype; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public abstract class TypeToken { + private Type type; + + protected TypeToken(){ + Type superclass = getClass().getGenericSuperclass(); + this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; + } + + public Type getType() { + return type; + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/classtype/GenericClassTypeUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/classtype/GenericClassTypeUnitTest.java new file mode 100644 index 0000000000..85026e53de --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/classtype/GenericClassTypeUnitTest.java @@ -0,0 +1,47 @@ +package com.baeldung.classtype; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.baeldung.generics.classtype.ContainerTypeFromReflection; +import com.baeldung.generics.classtype.ContainerTypeFromTypeParameter; +import com.baeldung.generics.classtype.TypeToken; + +public class GenericClassTypeUnitTest { + + @Test + public void givenContainerClassWithGenericType_whenTypeParameterUsed_thenReturnsClassType() { + var stringContainer = new ContainerTypeFromTypeParameter<>(String.class); + Class containerClass = stringContainer.getClazz(); + + assertEquals(containerClass, String.class); + } + + @Test + public void givenContainerClassWithGenericType_whenReflectionUsed_thenReturnsClassType() { + var stringContainer = new ContainerTypeFromReflection<>("Hello Java"); + Class stringClazz = stringContainer.getClazz(); + assertEquals(stringClazz, String.class); + + var integerContainer = new ContainerTypeFromReflection<>(1); + Class integerClazz = integerContainer.getClazz(); + assertEquals(integerClazz, Integer.class); + } + + @Test + public void giveContainerClassWithGenericType_whenTypeTokenUsed_thenReturnsClassType() { + class ContainerTypeFromTypeToken extends TypeToken> { + } + + var container = new ContainerTypeFromTypeToken(); + ParameterizedType type = (ParameterizedType) container.getType(); + Type actualTypeArgument = type.getActualTypeArguments()[0]; + + assertEquals(actualTypeArgument, String.class); + } +}