|
| |
Given these classes and interfaces, here is an example
that uses Class
to obtain information about them:
package reflection;
public class ClassTestClass
{
public static void main(String[] args)
{
Class c;
try
{
c = Class.forName("java.lang.String");
printInfo(c);
}
catch(ClassNotFoundException e)
{
System.err.println("Class java.lang.String not found!");
}
c = int.class;
printInfo(c);
c = Double[].class;
printInfo(c);
c = int[].class;
printInfo(c);
short[] shortArray = new short[] {5, 10, 15};
c = shortArray.getClass();
printInfo(c);
c = Person.class;
printInfo(c);
c = Employee.class;
printInfo(c);
c = Manager.class;
printInfo(c);
c = Hirer.class;
printInfo(c);
c = HirerFirer.class;
printInfo(c);
}
private static void printInfo(Class c)
{
System.out.println("toString(): " + c.toString());
if (c.isPrimitive())
printPrimitiveInfo(c);
else if (c.isArray())
printArrayInfo(c);
else
printClassInfo(c);
}
private static void printPrimitiveInfo(Class c)
{
String typeName = c.getName();
System.out.println("Type " + typeName);
}
private static void printArrayInfo(Class c)
{
Class component = c.getComponentType();
System.out.println("Array of " + component.getName());
}
private static void printClassInfo(Class c)
{
String className = c.getName();
int index = className.lastIndexOf('.');
String packageName;
if (index != -1)
{
packageName = className.substring(0, index);
System.out.println("package " + packageName + ";");
className = className.substring(index+1, className.length());
}
String type = (c.isInterface())?"interface":"class";
System.out.println(type + " " + className);
Class superClass = c.getSuperclass();
if (superClass != null)
System.out.println(" extends " + superClass.getName());
Class[] interfaces = c.getInterfaces();
if ((interfaces != null) && (interfaces.length > 0))
{
if (c.isInterface())
System.out.print(" extends ");
else
System.out.print(" implements ");
for (int i = 0; i < interfaces.length; i++)
{
if (i > 0)
System.out.print(", ");
System.out.print(interfaces[i].getName());
}
System.out.println();
}
System.out.println("{");
System.out.println("}");
}
} |
This outputs the following (I've bolded the toString(): line
of each element output to make it easier to identify them
visually):
toString(): class java.lang.String
package java.lang;
class String
extends java.lang.Object
implements java.io.Serializable
{
}
toString(): int
Type int
toString(): class [Ljava.lang.Double;
Array of java.lang.Double
toString(): class [I
Array of int
toString(): class [S
Array of short
toString(): class reflection.Person
package reflection;
class Person
extends java.lang.Object
{
}
toString(): class reflection.Employee
package reflection;
class Employee
extends reflection.Person
{
}
toString(): class reflection.Manager
package reflection;
class Manager
extends reflection.Employee
implements reflection.Hirer, reflection.Firer
{
}
toString(): interface reflection.Hirer
package reflection;
interface Hirer
{
}
toString(): interface reflection.HirerFirer
package reflection;
interface HirerFirer
extends reflection.Hirer, reflection.Firer
{
}
|