Hi, I have 2 objects coming in to the socket, I have to see what class is it, whether it is Vector or Alarms class..how do i do this? If I have read the object into Object obj variable.
if (o == null) {
System.out.println("This object is null");
return;
}
Class c = o.getClass();
printHierarchy(c);
printInterfaces(c);
printClassLoader(c);
}
static void printHierarchy(Class c) {
System.out.println(c.getName());
while ((c = c.getSuperclass()) != null) {
System.out.println("extends " + c.getName());
}
}
static void printInterfaces(Class c) {
Class[] ci = c.getInterfaces();
if (ci.length > 0) {
if (c.isInterface()) {
for (int i = 0; i < ci.length; i++) {
System.out.println("extends" + ci.getName());
}
}
else {
System.out.println("implements ");
for (int i = 0; i < ci.length; i++) {
System.out.println(ci.getName() + ",");
}
}
}
}
static void printClassLoader(Class c) {
ClassLoader cl = c.getClassLoader();
if (cl != null) System.out.println("This object was loaded by " + cl);
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.