moved classes for BAEL-5727 to another module
This commit is contained in:
parent
74a8eff334
commit
4b8a372be5
@ -29,19 +29,4 @@
|
|||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>${commons-lang3.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.reflections</groupId>
|
|
||||||
<artifactId>reflections</artifactId>
|
|
||||||
<version>${reflections.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
46
core-java-modules/core-java-lang-6/pom.xml
Normal file
46
core-java-modules/core-java-lang-6/pom.xml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-lang-6</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-lang-6</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||||
|
<reflections.version>0.10.2</reflections.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-lang-6</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.reflections</groupId>
|
||||||
|
<artifactId>reflections</artifactId>
|
||||||
|
<version>${reflections.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -1,154 +1,154 @@
|
|||||||
package com.baeldung.checkinterface;
|
package com.baeldung.checkinterface;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.lang3.ClassUtils;
|
import org.apache.commons.lang3.ClassUtils;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.reflections.ReflectionUtils;
|
import org.reflections.ReflectionUtils;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
public class CheckInterfaceUnitTest {
|
public class CheckInterfaceUnitTest {
|
||||||
|
|
||||||
protected static Reflections reflections;
|
protected static Reflections reflections;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void initializeReflectionsLibrary() {
|
public static void initializeReflectionsLibrary() {
|
||||||
|
|
||||||
reflections = new Reflections("com.baeldung.checkinterface");
|
reflections = new Reflections("com.baeldung.checkinterface");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionGetInterfaces_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflectionGetInterfaces_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
||||||
|
|
||||||
assertEquals(1, interfaces.size());
|
assertEquals(1, interfaces.size());
|
||||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionGetInterfaces_thenParentInterfaceIsNotFound() {
|
public void whenUsingReflectionGetInterfaces_thenParentInterfaceIsNotFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
||||||
|
|
||||||
assertFalse(interfaces.contains(MasterInterface.class));
|
assertFalse(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionGetInterfacesRecursively_thenParentInterfaceIsFound() {
|
public void whenUsingReflectionGetInterfacesRecursively_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
Set<Class<?>> interfaces = getAllExtendedOrImplementedInterfacesRecursively(ChildClass2.class);
|
Set<Class<?>> interfaces = getAllExtendedOrImplementedInterfacesRecursively(ChildClass2.class);
|
||||||
|
|
||||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
assertTrue(interfaces.contains(MasterInterface.class));
|
assertTrue(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(ChildInterface2.class.isAssignableFrom(childClass2.getClass()));
|
assertTrue(ChildInterface2.class.isAssignableFrom(childClass2.getClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionIsAssignableFrom_thenParentInterfaceIsFound() {
|
public void whenUsingReflectionIsAssignableFrom_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(MasterInterface.class.isAssignableFrom(childClass2.getClass()));
|
assertTrue(MasterInterface.class.isAssignableFrom(childClass2.getClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionIsInstance_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflectionIsInstance_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(ChildInterface2.class.isInstance(childClass2));
|
assertTrue(ChildInterface2.class.isInstance(childClass2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionIsInstance_thenParentInterfaceIsFound() {
|
public void whenUsingReflectionIsInstance_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(MasterInterface.class.isInstance(childClass2));
|
assertTrue(MasterInterface.class.isInstance(childClass2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionInstanceOf_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflectionInstanceOf_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(childClass2 instanceof ChildInterface2);
|
assertTrue(childClass2 instanceof ChildInterface2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionInstanceOf_thenParentInterfaceIsFound() {
|
public void whenUsingReflectionInstanceOf_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
|
|
||||||
assertTrue(childClass2 instanceof MasterInterface);
|
assertTrue(childClass2 instanceof MasterInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingCommons_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingCommons_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
||||||
|
|
||||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingCommons_thenParentInterfaceIsFound() {
|
public void whenUsingCommons_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
List<Class<?>> interfaces = ClassUtils.getAllInterfaces(childClass2.getClass());
|
||||||
|
|
||||||
assertTrue(interfaces.contains(MasterInterface.class));
|
assertTrue(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflections_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflections_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
||||||
|
|
||||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflections_thenParentInterfaceIsFound() {
|
public void whenUsingReflections_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
Set<Class<?>> interfaces = reflections.get(ReflectionUtils.Interfaces.of(childClass2.getClass()));
|
||||||
|
|
||||||
assertTrue(interfaces.contains(MasterInterface.class));
|
assertTrue(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Set<Class<?>> getAllExtendedOrImplementedInterfacesRecursively(Class<?> clazz) {
|
static Set<Class<?>> getAllExtendedOrImplementedInterfacesRecursively(Class<?> clazz) {
|
||||||
|
|
||||||
Set<Class<?>> res = new HashSet<Class<?>>();
|
Set<Class<?>> res = new HashSet<Class<?>>();
|
||||||
Class<?>[] interfaces = clazz.getInterfaces();
|
Class<?>[] interfaces = clazz.getInterfaces();
|
||||||
|
|
||||||
if (interfaces.length > 0) {
|
if (interfaces.length > 0) {
|
||||||
res.addAll(Arrays.asList(interfaces));
|
res.addAll(Arrays.asList(interfaces));
|
||||||
for (Class<?> interfaze : interfaces) {
|
for (Class<?> interfaze : interfaces) {
|
||||||
res.addAll(getAllExtendedOrImplementedInterfacesRecursively(interfaze));
|
res.addAll(getAllExtendedOrImplementedInterfacesRecursively(interfaze));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user