Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);

}
}
26 changes: 26 additions & 0 deletions src/main/java/weisser/sarah/type_inheritance/Main.java
Original file line number Diff line number Diff line change
@@ -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());

}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}



}