diff --git a/.gitignore b/.gitignore index 32858aa..79c5951 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.class - +.idea/ # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/TypeInformation.iml b/TypeInformation.iml new file mode 100644 index 0000000..9c26698 --- /dev/null +++ b/TypeInformation.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a419514 --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + banuelos.aurora + TypeInformation + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + junit + junit + 4.12 + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..fff3afc --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,21 @@ +import java.io.Serializable; +import java.lang.reflect.Array; +import java.util.AbstractCollection; +import java.util.ArrayList; +import java.util.Collections; + +/** + * Created by aurorabanuelos on 5/5/17. + */ +public class Main { + + public static void main(String[] args) { + Part1 tst = new Part1(); + + System.out.println(tst.classImplementsInterface(ArrayList.class, Serializable.class)); + + tst.getClassHierarchy(ArrayList.class); + + tst.listAllMembers(ArrayList.class); + } +} diff --git a/src/main/java/Part1.java b/src/main/java/Part1.java new file mode 100644 index 0000000..b85ac0f --- /dev/null +++ b/src/main/java/Part1.java @@ -0,0 +1,77 @@ +import java.lang.reflect.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; + +/** + * Created by aurorabanuelos on 5/4/17. + */ +public class Part1{ + + boolean x; + + + public boolean classImplementsInterface(Class cls, Class inter) { + + Class[] interfaceList = cls.getInterfaces(); + + for (Class e : interfaceList) + if (e.equals(inter)) { + x = true; + } else { + x = false; + } + + return x; + + + } + + public String getClassHierarchy(Class cls) { + + ArrayList supercls = new ArrayList(); + + Class superClass = cls.getSuperclass(); + while (superClass != null) { + supercls.add(superClass.getName()); + superClass = superClass.getSuperclass(); + } + + Collections.reverse(supercls); + + for(Object e: supercls) { + System.out.printf("\n %s", e); + } + + return supercls.toString(); + + } + + public String listAllMembers(Class cls){ + Constructor [] ctors = cls.getDeclaredConstructors(); + Field[] flds = cls.getDeclaredFields(); + Method[] mthds = cls.getDeclaredMethods(); + + ArrayList members = new ArrayList(); + + for (Field f: flds){ + members.add(cls + " " + Modifier.toString(f.getModifiers()) + " Field: " + f.getName() ); + } + + for (Constructor c: ctors){ + members.add(cls + " " + Modifier.toString(c.getModifiers()) + " Constructor: " + c.getName() ); + } + + for (Method m: mthds){ + members.add(cls + " " + Modifier.toString(m.getModifiers()) + " Method: " + m.getName() ); + } + + for(Object e: members) { + System.out.println(e); + } + + return members.toString(); + + } +} \ No newline at end of file diff --git a/src/test/java/Part1Test.java b/src/test/java/Part1Test.java new file mode 100644 index 0000000..8b5649a --- /dev/null +++ b/src/test/java/Part1Test.java @@ -0,0 +1,60 @@ +import com.sun.org.apache.xpath.internal.operations.Bool; +import org.junit.Assert; +import org.junit.Test; + +import java.io.Serializable; +import java.lang.reflect.Array; +import java.util.AbstractCollection; +import java.util.ArrayList; + + +/** + * Created by aurorabanuelos on 5/4/17. + */ +public class Part1Test { + + @Test + public void classImplementsInterface(){ + + //Given + Part1 part1 = new Part1(); + Class cls = ArrayList.class; + Class inter = Serializable.class; + + //When + boolean actual = part1.classImplementsInterface(cls, inter); + + //Then + Assert.assertEquals(true, actual); + } + + @Test + public void getClassHierarchyTest() { + //Given + Part1 part1 = new Part1(); + Class cls = AbstractCollection.class; + + //When + String actual = part1.getClassHierarchy(cls); + + //Then + Assert.assertEquals("[java.lang.Object]", actual); + + } + + @Test + public void listAllMembersTest(){ + + //Given + Part1 part1 = new Part1(); + Class cls = Object.class; + + //When + String actualResult = part1.listAllMembers(cls); + + //Then + Assert.assertEquals("", actualResult); + + } + +}