BAEL-5727 - Determine If a Class Implements an Interface in Java
Data model and unit test Class for the BAEL-5727 - Determine If a Class Implements an Interface in Java
This commit is contained in:
parent
66fb7ea877
commit
28f0b329e7
|
@ -13,6 +13,7 @@
|
|||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-5</finalName>
|
||||
|
@ -24,4 +25,18 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.10.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public class ChildClass1 implements ChildInterface1 {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public class ChildClass2 implements ChildInterface2 {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public interface ChildInterface1 extends MasterInterface {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public interface ChildInterface2 extends MasterInterface {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public class MasterClass implements MasterInterface {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
public interface MasterInterface {
|
||||
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
package com.baeldung.checkInterface;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.reflections.ReflectionUtils;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
public class CheckInterfaceUnitTest {
|
||||
|
||||
protected static Reflections reflections;
|
||||
|
||||
@BeforeAll
|
||||
public static void initializeReflectionsLibrary() {
|
||||
|
||||
reflections = new Reflections("com.baeldung.checkInterface");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionGetInterfaces_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
||||
|
||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionGetInterfaces_thenParentInterfaceIsNotFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
||||
|
||||
assertFalse(interfaces.contains(MasterInterface.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(ChildInterface2.class.isAssignableFrom(childClass2.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionIsAssignableFrom_thenParentInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(MasterInterface.class.isAssignableFrom(childClass2.getClass()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionIsInstance_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(ChildInterface2.class.isInstance(childClass2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionIsInstance_thenParentInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(MasterInterface.class.isInstance(childClass2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionInstanceOf_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(childClass2 instanceof ChildInterface2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflectionInstanceOf_thenParentInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
|
||||
assertTrue(childClass2 instanceof MasterInterface);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCommons_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
||||
|
||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCommons_thenParentInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
||||
|
||||
assertTrue(interfaces.contains(MasterInterface.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflections_thenDirectlyImplementedInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
||||
|
||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingReflections_thenParentInterfaceIsFound() {
|
||||
|
||||
ChildClass2 childClass2 = new ChildClass2();
|
||||
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
||||
|
||||
assertTrue(interfaces.contains(MasterInterface.class));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue