From 428e8dac62b901208872dde784e7eb9379cb369d Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Thu, 17 Sep 2020 00:20:42 +0200 Subject: [PATCH] [BAEL-4532]a.getClass() vs A.class --- .../com/baeldung/getclassobject/Animal.java | 5 ++ .../com/baeldung/getclassobject/Monkey.java | 4 ++ .../getclassobject/SomeAbstractClass.java | 4 ++ .../getclassobject/SomeInterface.java | 4 ++ .../baeldung/getclassobject/SomeUtils.java | 9 +++ .../GetClassObjectUnitTest.java | 55 +++++++++++++++++++ 6 files changed, 81 insertions(+) create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Animal.java create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Monkey.java create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeAbstractClass.java create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeInterface.java create mode 100644 core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeUtils.java create mode 100644 core-java-modules/core-java-lang-3/src/test/java/com/baeldung/getclassobject/GetClassObjectUnitTest.java diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Animal.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Animal.java new file mode 100644 index 0000000000..426f1403af --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Animal.java @@ -0,0 +1,5 @@ +package com.baeldung.getclassobject; + +public class Animal { + protected int numberOfEyes; +} diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Monkey.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Monkey.java new file mode 100644 index 0000000000..76ad8a96e3 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/Monkey.java @@ -0,0 +1,4 @@ +package com.baeldung.getclassobject; + +public class Monkey extends Animal { +} diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeAbstractClass.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeAbstractClass.java new file mode 100644 index 0000000000..7ee34cf62a --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeAbstractClass.java @@ -0,0 +1,4 @@ +package com.baeldung.getclassobject; + +public abstract class SomeAbstractClass { +} diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeInterface.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeInterface.java new file mode 100644 index 0000000000..eec8048481 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeInterface.java @@ -0,0 +1,4 @@ +package com.baeldung.getclassobject; + +interface SomeInterface { +} diff --git a/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeUtils.java b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeUtils.java new file mode 100644 index 0000000000..294ef5bc9e --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/main/java/com/baeldung/getclassobject/SomeUtils.java @@ -0,0 +1,9 @@ +package com.baeldung.getclassobject; + +public class SomeUtils { + private SomeUtils() { + throw new RuntimeException("This Util class is not allowed to be instantiated!"); + } + // public static utilMethods + // ... +} diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/getclassobject/GetClassObjectUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/getclassobject/GetClassObjectUnitTest.java new file mode 100644 index 0000000000..20b5b8e0c8 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/getclassobject/GetClassObjectUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.getclassobject; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class GetClassObjectUnitTest { + @Test + public void givenObjectAndType_whenGettingClassObject_thenTwoMethodsHaveTheSameResult() { + String str = "I am an object of the String class"; + Class fromStrObject = str.getClass(); + Class clazz = String.class; + assertSame(fromStrObject, clazz); + } + + @Test + public void givenClassInheritance_whenGettingRuntimeTypeAndStaticType_thenGetDifferentResult() { + Animal animal = new Monkey(); + Class runtimeType = animal.getClass(); + Class staticType = Animal.class; + //Not equals + assertNotEquals(staticType, runtimeType); + + Class monkeyClass = Monkey.class; + assertSame(runtimeType, monkeyClass); + } + + @Test + public void givenPrimitiveType_whenGettingClassObject_thenOnlyStaticTypeWorks() { + int number = 7; + // Class numberClass = number.getClass(); <-- compilation error + Class intType = int.class; + + assertNotNull(intType); + assertEquals("int", intType.getName()); + assertTrue(intType.isPrimitive()); + } + + @Test + public void givenTypeCannotInstantiate_whenGetTypeStatically_thenGetTypesSuccefully() { + Class interfaceType = SomeInterface.class; + Class abstractClassType = SomeAbstractClass.class; + Class utilClassType = SomeUtils.class; + + assertNotNull(interfaceType); + assertTrue(interfaceType.isInterface()); + assertEquals("SomeInterface", interfaceType.getSimpleName()); + + assertNotNull(abstractClassType); + assertEquals("SomeAbstractClass", abstractClassType.getSimpleName()); + + assertNotNull(utilClassType); + assertEquals("SomeUtils", utilClassType.getSimpleName()); + } +}