* 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 <mail.technoddy@gmail.com>
This commit is contained in:
Sam 2023-12-27 11:37:40 -05:00 committed by GitHub
parent 11c122fda5
commit 223c679535
6 changed files with 125 additions and 2 deletions

View File

@ -37,8 +37,8 @@
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

View File

@ -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<Integer> 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);
}
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.generics.classtype;
public class ContainerTypeFromReflection<T> {
private T content;
public ContainerTypeFromReflection(T content) {
this.content = content;
}
public Class<?> getClazz() {
return this.content.getClass();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.generics.classtype;
public class ContainerTypeFromTypeParameter<T> {
private Class<T> clazz;
public ContainerTypeFromTypeParameter(Class<T> clazz) {
this.clazz = clazz;
}
public Class<T> getClazz() {
return this.clazz;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.generics.classtype;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public abstract class TypeToken<T> {
private Type type;
protected TypeToken(){
Type superclass = getClass().getGenericSuperclass();
this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
}
public Type getType() {
return type;
}
}

View File

@ -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<String> 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<List<String>> {
}
var container = new ContainerTypeFromTypeToken();
ParameterizedType type = (ParameterizedType) container.getType();
Type actualTypeArgument = type.getActualTypeArguments()[0];
assertEquals(actualTypeArgument, String.class);
}
}