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
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
hs_err_pid

# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
log/
target/*
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>zygmundfelt.dan.typeinformation</groupId>
<artifactId>TypeInformation</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>TypeInformation</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package zygmundfelt.dan.typeinformation.Part1;

public class ClassImplementsInterface {

static boolean classImplementsInterface(Class implementor, Class implemented) {
Class[] interfaces = implementor.getInterfaces();
for (Class cl : interfaces) {
if (cl == implemented) {
return true;
}
}
return false;
}

static boolean classImplementsInterface(Object implementor, Class implemented) {
Class[] interfaces = implementor.getClass().getInterfaces();
for (Class cl : interfaces) {
if (cl == implemented) {
return true;
}
}
return false;
}

static boolean classImplementsInterface(String implementor, Class implemented) {
try {
Class[] interfaces = Class.forName(implementor).getInterfaces();
for (Class cl : interfaces) {
if (cl == implemented) {
return true;
}
}
} catch(Exception e) {
return false;
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package zygmundfelt.dan.typeinformation.Part1;

import java.util.ArrayList;

public class GetClassHierarchy {

static String getClassHierarchy(Object o) {
Class cl = o.getClass();
ArrayList<Class> classArrayList = new ArrayList<Class>();
String result = "";

do {
classArrayList.add(cl);
cl = cl.getSuperclass();
} while(cl != null);

int len = classArrayList.size();

for(int i = 0; i < len; i++) {
for(int j = 0; j < i; j++) {
result += " ";
}
result += classArrayList.get(len - 1 - i).getName() + "\n";
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package zygmundfelt.dan.typeinformation.Part1;

import java.util.ArrayList;
import java.util.TreeMap;

public class InstantiateClassHierarchy {

static ArrayList<Object> instantiateClassHierarchy(Object o) {
Class cl = o.getClass();
ArrayList<Object> classInstanceArrayList = new ArrayList<Object>();

do {
try{
Object object = cl.newInstance();
classInstanceArrayList.add(object);
cl = cl.getSuperclass();
}
catch (Exception e) {
cl = cl.getSuperclass();
}
} while(cl != null);

for(Object object : classInstanceArrayList) {
int modifier = object.getClass().getModifiers();
if(modifier == 0 || modifier == 2) {
classInstanceArrayList.remove(object);
}
}

return classInstanceArrayList;
}

public static void main(String[] args) {
TreeMap map = new TreeMap();
ArrayList list = new ArrayList();

ArrayList<Object> forTreeMap = instantiateClassHierarchy(map);
ArrayList<Object> forList = instantiateClassHierarchy(list);

for(Object o : forTreeMap) {
System.out.println(o.getClass());
}

System.out.println();

for(Object o : forList) {
System.out.println(o.getClass());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package zygmundfelt.dan.typeinformation.Part1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

/* The Arrays.sort methods below are adapted from
http://stackoverflow.com/questions/1097807/java-reflection-is-the-order-of-class-fields-and-methods-standardized
*/

public class ListAllMembers {

static String listAllMembers(Object o) {
String summary = "";
Class cl = o.getClass();
do {
summary += cl + "\n";
Field[] fields = cl.getDeclaredFields();
Arrays.sort(fields, new Comparator<Field>() {
public int compare(Field o1, Field o2) {
return o1.getName().compareTo(o2.getName());
}
});
for(Field f : fields) {
summary += f + "\n";
}
System.out.println("\n");
Constructor[] constructors = cl.getConstructors();
Arrays.sort(constructors, new Comparator<Constructor>() {
public int compare(Constructor o1, Constructor o2) {
return o1.getName().compareTo(o2.getName());
}
});
for(Constructor c : constructors) {
summary += c + "\n";
}
System.out.println("\n");
Method[] methods = cl.getDeclaredMethods();
Arrays.sort(methods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
return o1.getName().compareTo(o2.getName());
}
});
for(Method m : methods) {
summary += m + "\n";
}
System.out.println("\n");
summary += "\n";
cl = cl.getSuperclass();
} while (cl != null);

return summary;
}

public static void main(String[] args) {
LinkedList list = new LinkedList();
System.out.println(listAllMembers(list));
}

}
25 changes: 25 additions & 0 deletions src/main/java/zygmundfelt/dan/typeinformation/unitCorn/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zygmundfelt.dan.typeinformation.unitCorn;

public class Result {

String methodName;
String result;

Result(String methodName, String result) {
this.methodName = methodName;
this.result = result;
}

public String getResult() {
return result;
}

public String getMethodName() {
return methodName;
}

public String toString() {
return "The result of testing " + methodName + " was " + result + ".\n";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package zygmundfelt.dan.typeinformation.unitCorn;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.junit.*;


public class UnitCornTestRunner {

ArrayList<Result> results;

static Result runTest(Class cls, String methodName) {

Object o;
try {
o = cls.newInstance();
} catch(Exception e) {
return new Result(methodName, e.toString());
}

Method method = getMethod(cls, methodName);

try {
method.invoke(o);
} catch (Exception e) {
return new Result(methodName, e.toString());
}

return new Result(methodName, "success");
}

static Method getMethod(Class cls, String methodName) {
Method[] methods = cls.getDeclaredMethods();
for(Method m : methods) {
if(m.getName().equals(methodName)) {
return m;
}
}
return null;
}

/*
For testing in test class and testing in main class below
*/
@Test
static String runTests(Class cls) {
StringBuilder sb = new StringBuilder();
ArrayList<String> annotatedMethods = getJUnitAnnotatedMethods(cls);

for(String s : annotatedMethods) {
Result result = runTest(cls, s);
sb.append(result.toString());
}

return sb.toString();
}

/*
For testing in test class and testing in main class below
*/
@Test
static ArrayList<String> getJUnitAnnotatedMethods(Class cls) {
Method[] methods = cls.getDeclaredMethods();
ArrayList<String> annotatedMethods = new ArrayList<String>();

for(Method m : methods) {
Annotation[] annotations = m.getAnnotations();
for(Annotation a : annotations) {
if(a.toString().substring(0, 15).equals("@org.junit.Test")) {
annotatedMethods.add(m.getName());
}
}
}

return annotatedMethods;
}

public static void main(String[] args) {
try {
Class cls = Class.forName("zygmundfelt.dan.typeinformation.unitCorn.UnitCornTestRunner");
System.out.println(runTests(cls));
} catch (Exception e) {
System.out.println("BOOOO.");
}

}

}
Loading