diff --git a/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java b/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java new file mode 100644 index 0000000..2d65e07 --- /dev/null +++ b/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java @@ -0,0 +1,51 @@ +package weisser.sarah.type_inheritance; + +import java.util.Arrays; + +/** + * Created by sarahweisser on 5/6/17. + */ +public class ClassInvestigator { + + private Class cl; + private Object objectToInvestigate; + + public void setCl(Object objectToInvestigate) { + this.cl = objectToInvestigate.getClass(); + } + + public void setObjectToInvestigate(Object objectToInvestigate) { + this.objectToInvestigate = objectToInvestigate; + } + + public Object getObjectToInvestigate() { + return objectToInvestigate; + } + + public ClassInvestigator(Object objectToInvestigate) { + this.objectToInvestigate = objectToInvestigate; + setCl(objectToInvestigate); + } + + public String[] getObjectInterfaces() { + + String[] objectInterfaces = new String[cl.getInterfaces().length]; + for(int i = 0; i < objectInterfaces.length; i++) { + objectInterfaces[i] = cl.getInterfaces()[i].toString(); + } + return objectInterfaces; + } + + + public boolean classImplementsInterface(Object objectToInvestigate, String interfaceName) { + + return Arrays.asList(getObjectInterfaces()).contains(interfaceName); + + } + + public boolean classImplementsInterface(Class cl, String interfaceName) { + + return Arrays.asList(getObjectInterfaces()).contains(interfaceName); + + } +} diff --git a/src/main/java/weisser/sarah/type_inheritance/Main.java b/src/main/java/weisser/sarah/type_inheritance/Main.java new file mode 100644 index 0000000..8484b6f --- /dev/null +++ b/src/main/java/weisser/sarah/type_inheritance/Main.java @@ -0,0 +1,26 @@ +package weisser.sarah.type_inheritance; + +import java.util.ArrayList; +import java.lang.reflect.*; + +/** + * Created by sarahweisser on 5/6/17. + */ +public class Main { + + public static void main(String[] args) { + + String string = "I want tacos."; + try { + Class cl = Class.forName("java.lang.String"); + System.out.println(cl); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } +// ClassInvestigator investigator = new ClassInvestigator(string); +// System.out.println(investigator.classImplementsInterface(string, "interface java.io.Serializable")); +// + System.out.println(string.getClass().getName()); + + } +} diff --git a/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java b/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java new file mode 100644 index 0000000..611e67b --- /dev/null +++ b/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java @@ -0,0 +1,85 @@ +package weisser.sarah.type_information; + +import org.junit.Assert; +import org.junit.Test; +import weisser.sarah.type_inheritance.ClassInvestigator; + +/** + * Created by sarahweisser on 5/6/17. + */ +public class TestClassInvestigator { + + @Test + public void testCreateArrayOfInterfaces() { + + //given + Character letter = new Character('j'); + ClassInvestigator investigator = new ClassInvestigator(letter); + String expectedResult = "interface java.io.Serializable"; + + //when + String[] objectInterfaces = investigator.getObjectInterfaces(); + String actualResult = objectInterfaces[0]; + + //then + Assert.assertEquals(expectedResult, actualResult); + + } + + @Test + public void testClassImplementsInterface() { + + //given + Character letter = new Character('j'); + ClassInvestigator investigator = new ClassInvestigator(letter); + String[] objectInterfaces = investigator.getObjectInterfaces(); + boolean expectedResult = true; + + //when + boolean actualResult = investigator.classImplementsInterface(letter, "interface java.io.Serializable"); + + //then + Assert.assertEquals(expectedResult, actualResult); + + } + + @Test + public void testClassImplementsInterfaceWithUnrecognizedInterfaceName() { + + //given + Character letter = new Character('j'); + ClassInvestigator investigator = new ClassInvestigator(letter); + String[] objectInterfaces = investigator.getObjectInterfaces(); + boolean expectedResult = false; + + //when + boolean actualResult = investigator.classImplementsInterface(letter, "interface java.lang.Appendable"); + + //then + Assert.assertEquals(expectedResult, actualResult); + } + + @Test + public void testClassImplementsInterfaceWithClassObject() { + + //given + try { + Class cl = Class.forName("java.lang.String"); + ClassInvestigator investigator = new ClassInvestigator(cl); + String[] objectInterfaces = investigator.getObjectInterfaces(); + boolean expectedResult = true; + + //when + boolean actualResult = investigator.classImplementsInterface(cl, "interface java.io.Serializable"); + + //then + Assert.assertEquals(expectedResult, actualResult); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + + +}