FileDocCategorySizeDatePackage
ReflectionDemo.javaAPI DocExample3308Fri Nov 03 11:08:02 GMT 2006None

ReflectionDemo

public class ReflectionDemo extends Object

Fields Summary
private static final PrintStream
out
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

	for (String name : args) {
	    Class<?> c = Class.forName(name);
	    printClass(c);
	}
    
public static voidprintBounds(java.lang.reflect.Type[] bounds)

	if (bounds != null && bounds.length > 0
	    && !(bounds.length==1 && bounds[0]==Object.class)) {
	    out.print(" extends ");
	    int i = 0;
	    for (Type bound : bounds) {
		if (i++ > 0) out.print("&");
		printType(bound);
	    }
	}
    
public static voidprintClass(java.lang.Class c)

	out.print("class ");
	out.print(c.getName());
	printTypeParameters(c.getTypeParameters());
	out.println();
	printSuperclass(c.getGenericSuperclass());
	printInterfaces(c.getGenericInterfaces());
	/*
	out.println("{");
	for (Field f : c.getFields()) {
	    out.println("  "+f.toGenericString()+";");
	}
	for (Constructor k : c.getConstructors()) {
	    out.println("  "+k.toGenericString()+";");
	}
	for (Method m : c.getMethods()) {
	    out.println("  "+m.toGenericString()+";");
	}
	out.println("}");
	*/
    
public static voidprintInterfaces(java.lang.reflect.Type[] impls)

	if (impls != null && impls.length > 0) {
	    out.print("implements ");
	    int i = 0;
	    for (Type impl : impls) {
		if (i++ > 0) out.print(",");
		printType(impl);
	    }
	    out.println();
	}
    
public static voidprintParams(java.lang.reflect.Type[] types)

	if (types != null && types.length > 0) {
	    out.print("<");
	    int i = 0;
	    for (Type type : types) {
		if (i++ > 0) out.print(",");
		printType(type);
	    }
	    out.print(">");
	}
    
public static voidprintSuperclass(java.lang.reflect.Type sup)

          
	if (sup != null && !sup.equals(Object.class)) {
	    out.print("extends ");
	    printType(sup);
  	    out.println();
	}
    
public static voidprintType(java.lang.reflect.Type type)

	if (type instanceof Class) {
	    Class<?> c = (Class)type;
	    out.print(c.getName());
	} else if (type instanceof ParameterizedType) {
	    ParameterizedType p = (ParameterizedType)type;
	    Class c = (Class)p.getRawType();
	    Type o = p.getOwnerType();
	    if (o != null) { printType(o); out.print("."); }
	    out.print(c.getName());
	    printParams(p.getActualTypeArguments());
	} else if (type instanceof TypeVariable<?>) {
	    TypeVariable<?> v = (TypeVariable<?>)type;
	    out.print(v.getName());
	} else if (type instanceof GenericArrayType) {
	    GenericArrayType a = (GenericArrayType)type;
	    printType(a.getGenericComponentType());
	    out.print("[]");
	} else if (type instanceof WildcardType) {
	    WildcardType w = (WildcardType)type;
	    Type[] upper = w.getUpperBounds();
	    Type[] lower = w.getLowerBounds();
	    if (upper.length==1 && lower.length==0) {
		out.print("? extends ");
		printType(upper[0]);
	    } else if (upper.length==0 && lower.length==1) {
		out.print("? super ");
		printType(lower[0]);
	    } else assert false;
	}
    
public static voidprintTypeParameters(java.lang.reflect.TypeVariable[] vars)

	if (vars != null && vars.length > 0) {
	    out.print("<");
	    int i = 0;
	    for (TypeVariable<?> var : vars) {
		if (i++ > 0) out.print(",");
		out.print(var.getName());
		printBounds(var.getBounds());
	    }
	    out.print(">");
	}