Skip to content

Didn't get very far into part 2 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@

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


# Intellij
.idea/
*.iml
*.iws

# Mac
.DS_Store

# Maven
log/
target/
31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<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>kim.christopher</groupId>
<artifactId>UnitCornTestRunner</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

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

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

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

</project>
15 changes: 15 additions & 0 deletions src/main/java/kim/christopher/partOne/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kim.christopher.partOne;

import java.lang.reflect.Method;

public class App {

public static void main(String[] args) {
Method[] methodArray = Double.class.getMethods();
for(Method m: methodArray){
System.out.println(m.toString());
}
}


}
127 changes: 127 additions & 0 deletions src/main/java/kim/christopher/partOne/ClassInspector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package kim.christopher.partOne;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.lang.Class;
import java.util.Collections;
import java.util.List;

public class ClassInspector {


//Take two arguments: 1) a class, specified by an object, Class object, or class name, and 2) an interface name.
// Return true if the specified class implements the specified interface
public boolean classImplementsInterface(Class c, String s){
Class[] interfaceArray = c.getInterfaces();
for(Class i: interfaceArray){
if (i.getSimpleName().equals(s)) return true;
}
return false;
}

//Take an object and list all declared members (Fields, Constructors, and Methods) of its class, as well as those of each superclass, all the way to Object.
//This method should return a string containing each declared member listed on a separate line as follows:
//The member name, preceded with the name of the declaring class, and any modifiers (static, private etc.)
//Within each class, members should be listed in the order: Fields, Constructors, Methods
//Each group of members (fields, constructors, or methods) should be listed alphabetically
//The first line should start with the members declared by the given object type; the last line should be the last method defined by Object
public String listAllMembers(Object object){
System.out.println("Hello");
StringBuilder sb = new StringBuilder();
sb.append("Fields:\n" + listFields(object) + "\n");
sb.append("Constructors\n" + listConstructors(object) + "\n");
sb.append("Methods\n" + listMethods(object));
return sb.toString();
}

public String listFields(Object object){
StringBuilder sb = new StringBuilder();
Class classOfObject = object.getClass();
Field[] fields = classOfObject.getFields();
for(Field f: fields){
sb.append(f.getDeclaringClass().getSimpleName()+ ": " + f.toString() + "\n");
}
return sb.toString();
}

public String listConstructors(Object object){
StringBuilder sb = new StringBuilder();
Class classOfObject = object.getClass();
Constructor[] constructors = classOfObject.getConstructors();
for(Constructor c: constructors){
sb.append(c.getDeclaringClass().getSimpleName() + ": " + c.toString() + "\n");
}
return sb.toString();
}

public String listMethods(Object object){
StringBuilder sb = new StringBuilder();
Class classOfObject = object.getClass();
Method[] methods = object.getClass().getMethods();
for(Method m: methods){
sb.append(m.getDeclaringClass().getSimpleName() + ": " + m.toString() + "\n");
}
return sb.toString();
}

// Take an object and produce an indented class hierarchy with one class per line.
// Each line should be indented two spaces more than the previous one.
// The first line should be java.lang.Object
public String getClassHierarchy(Object obj){
StringBuilder sb = new StringBuilder();
ArrayList<String> list = new ArrayList<String>();
Class classOfObject = obj.getClass();
boolean flag = true;
list.add(classOfObject.getSimpleName());
while(flag){
list.add(classOfObject.getSuperclass().getSimpleName());
classOfObject = classOfObject.getSuperclass();
if(classOfObject.getSimpleName().equals("Object"))
flag = false;
}
//counter for number of spaces to add at the beginning of the line
int count = 1;

for(int i = list.size() - 1; i >= 0; i--){
sb.append(list.get(i) + "\n");
//loop that adds two spaces each time it runs
for(int j = 0; j < count; j++){
sb.append(" ");
}
count++;
}

return sb.toString();
}


//take an object and return a List containing instances of every concrete class in its hierarchy.
// Handle classes without a no-argument constructor gracefully (your program should not crash, but may not be able to instantiate these classes).
// Note: Your test should confirm that each list item is an instance of a different class.
public ArrayList<Object> instantiateClassHierarchy(Object obj){

Class c = obj.getClass();
ArrayList<Class> list = new ArrayList<Class>();
while (c != null) {
list.add(c);
c = c.getSuperclass();
}

Collections.reverse(list);

ArrayList<Object> objList = new ArrayList<Object>();
for(int i = 0; i < list.size(); i++){
try{
objList.add(list.get(i).newInstance());
} catch (InstantiationException e){
System.out.println(list.get(i).getSimpleName() + " could not be instantiated");
} catch (IllegalAccessException e){
System.out.println(list.get(i).getSimpleName() + " was not able to be accessed");
}
}
return objList;
}
}
35 changes: 35 additions & 0 deletions src/main/java/kim/christopher/unitcorn/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kim.christopher.unitcorn;

public class Result {

private boolean successful;
private String message;


public Result(){
successful = false;
message = "";
}

public Result(boolean success, String message){
this.successful = success;
this.message = message;
}


public boolean isSuccessful() {
return successful;
}

public void setSuccessful(boolean b){
this.successful = b;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
53 changes: 53 additions & 0 deletions src/main/java/kim/christopher/unitcorn/UnitCornTestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package kim.christopher.unitcorn;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class UnitCornTestRunner {

public Result runTest(Class c, String methodName){
Method m = getMethod(c, methodName);
Object o = instantiateClass(c);
Result r = new Result();
try{
Object returnedObject = m.invoke(o);
r.setSuccessful(true);
r.setMessage(returnedObject.getClass().getName());
} catch (IllegalAccessException e){
r.setSuccessful(false);
r.setMessage(e.getMessage());
} catch (InvocationTargetException f){
r.setSuccessful(false);
r.setMessage(f.getMessage());
}

return r;
}

//used in runTest
public Method getMethod(Class c, String methodName){
Method[] methods = c.getMethods();
for(Method method: methods){
if(methodName.toString().equals(method.getName()))
return method;
}
return null;
}

public Object instantiateClass(Class c){
try {
Object obj = c.newInstance();
return obj;
} catch (InstantiationException e) {
System.out.println(c.getSimpleName() + ": could not instantiate!");
} catch (IllegalAccessException e) {
System.out.println(e);
}
return null;
}

public String runTests(Class c){
return "";
}

}
Loading