Skip to content

Instantly share code, notes, and snippets.

@helloIAmPau
Created April 10, 2013 19:34
Show Gist options
  • Save helloIAmPau/5357737 to your computer and use it in GitHub Desktop.
Save helloIAmPau/5357737 to your computer and use it in GitHub Desktop.
package com.helloiampau.miscs;
import java.util.HashMap;
class ClassContainer extends HashMap<String, Object> {
private final String CLASS = "class";
private final String SUPERCLASS = "superclass";
private final String INTERFACES = "interfaces";
public void putObjectClass(Class c) {
this.put(this.CLASS, c);
}
public Class getObjectClass() {
return (Class)(this.get(this.CLASS));
}
public void putObjectSuperclass(Class c) {
this.put(this.SUPERCLASS, c);
}
public Class getObjectSuperclass() {
return (Class)(this.get(this.SUPERCLASS));
}
public void putObjectInterfaces(Class[] cs) {
this.put(this.INTERFACES, cs);
}
public Class[] getObjectInterfaces() {
return (Class[])(this.get(this.INTERFACES));
}
}
public class ClassUtils {
public static ClassContainer whoExtendsAndImplements(Object o) {
ClassContainer container = new ClassContainer();
// Getting current object class
Class currentObjectClass = o.getClass();
container.putObjectClass(currentObjectClass);
// Getting current object superclass
Class currentObjectSuperclass = currentObjectClass.getSuperclass();
container.putObjectSuperclass(currentObjectSuperclass);
// Getting current object interfaces
Class[] currentObjectInterfaces = currentObjectClass.getInterfaces();
container.putObjectInterfaces(currentObjectInterfaces);
return container;
}
public static void main(String[] args) {
String testObject = new String("Cacca");
ClassContainer cc = ClassUtils.whoExtendsAndImplements(testObject);
System.out.println("This object class: " + cc.getObjectClass().toString());
System.out.println("This object superclass: " + cc.getObjectSuperclass().toString());
System.out.println("This object Interfaces: ");
for(Class c : cc.getObjectInterfaces())
System.out.println(c.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment