From e67d844fabcab45878684f6b07bc2a298c695bc5 Mon Sep 17 00:00:00 2001 From: Sarah Weisser Date: Sat, 6 May 2017 18:35:21 -0400 Subject: [PATCH 1/2] added part 1 of lab and tests --- .../type_inheritance/ClassInvestigator.java | 39 +++++++++++ .../weisser/sarah/type_inheritance/Main.java | 18 ++++++ .../TestClassInvestigator.java | 64 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java create mode 100644 src/main/java/weisser/sarah/type_inheritance/Main.java create mode 100644 src/test/java/weisser/sarah/type_information/TestClassInvestigator.java 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..72ae107 --- /dev/null +++ b/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java @@ -0,0 +1,39 @@ +package weisser.sarah.type_inheritance; + +import java.util.Arrays; + +/** + * Created by sarahweisser on 5/6/17. + */ +public class ClassInvestigator { + + private Object objectToInvestigate; + + public void setObjectToInvestigate(Object objectToInvestigate) { + this.objectToInvestigate = objectToInvestigate; + } + + public Object getObjectToInvestigate() { + return objectToInvestigate; + } + + public ClassInvestigator(Object objectToInvestigate) { + this.objectToInvestigate = objectToInvestigate; + } + + public String[] getObjectInterfaces() { + + String[] objectInterfaces = new String[objectToInvestigate.getClass().getInterfaces().length]; + for(int i = 0; i < objectInterfaces.getClass().getInterfaces().length; i++) { + objectInterfaces[i] = objectToInvestigate.getClass().getInterfaces()[i].toString(); + } + return objectInterfaces; + } + + + public boolean classImplementsInterface(Object objectToInvestigate, 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..8a79015 --- /dev/null +++ b/src/main/java/weisser/sarah/type_inheritance/Main.java @@ -0,0 +1,18 @@ +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."; + ClassInvestigator investigator = new ClassInvestigator(string); + System.out.println(investigator.classImplementsInterface(string, "interface java.io.Serializable")); + + } +} 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..85d5f10 --- /dev/null +++ b/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java @@ -0,0 +1,64 @@ +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); + } + + + +} From 46a877c9b4213b7371b82a55a367ade50363a368 Mon Sep 17 00:00:00 2001 From: Sarah Weisser Date: Sat, 6 May 2017 19:18:59 -0400 Subject: [PATCH 2/2] overloaded method to take a class object --- .../type_inheritance/ClassInvestigator.java | 18 +++++++++++++--- .../weisser/sarah/type_inheritance/Main.java | 12 +++++++++-- .../TestClassInvestigator.java | 21 +++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java b/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java index 72ae107..2d65e07 100644 --- a/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java +++ b/src/main/java/weisser/sarah/type_inheritance/ClassInvestigator.java @@ -7,8 +7,13 @@ */ 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; } @@ -19,13 +24,14 @@ public Object getObjectToInvestigate() { public ClassInvestigator(Object objectToInvestigate) { this.objectToInvestigate = objectToInvestigate; + setCl(objectToInvestigate); } public String[] getObjectInterfaces() { - String[] objectInterfaces = new String[objectToInvestigate.getClass().getInterfaces().length]; - for(int i = 0; i < objectInterfaces.getClass().getInterfaces().length; i++) { - objectInterfaces[i] = objectToInvestigate.getClass().getInterfaces()[i].toString(); + String[] objectInterfaces = new String[cl.getInterfaces().length]; + for(int i = 0; i < objectInterfaces.length; i++) { + objectInterfaces[i] = cl.getInterfaces()[i].toString(); } return objectInterfaces; } @@ -36,4 +42,10 @@ public boolean classImplementsInterface(Object objectToInvestigate, String inter 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 index 8a79015..8484b6f 100644 --- a/src/main/java/weisser/sarah/type_inheritance/Main.java +++ b/src/main/java/weisser/sarah/type_inheritance/Main.java @@ -11,8 +11,16 @@ public class Main { public static void main(String[] args) { String string = "I want tacos."; - ClassInvestigator investigator = new ClassInvestigator(string); - System.out.println(investigator.classImplementsInterface(string, "interface java.io.Serializable")); + 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 index 85d5f10..611e67b 100644 --- a/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java +++ b/src/test/java/weisser/sarah/type_information/TestClassInvestigator.java @@ -59,6 +59,27 @@ public void testClassImplementsInterfaceWithUnrecognizedInterfaceName() { 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(); + } + } + }