最后贴一段代码,某高人写的,卡卡
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.List;
/*******************************************************************************
*
*
* @作者:XXX
* @日期:2007-3-11
* @版权:北京易迈普技术有限公司版权所有
******************************************************************************/
public class GenericsTypeScanner {
private Writer writer;
public GenericsTypeScanner() {
}
/**
* scan a generic class,output the originate declaration to the destination
* writer。
*
* @param writer
* @param clazz
*/
public void 扫描(Writer writer, Class clazz) {
this.writer = writer;
try {
printClass(clazz);
for (Method method : clazz.getDeclaredMethods())
printMethod(method);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
private void printClass(Class clazz) throws IOException {
writer.write(clazz.getName());
printTypes(clazz.getTypeParameters(), "</span><span>",
"</span><span class=\"string\">",
"</span><span>, </span><span class=\"string\">");
// scan the super class
Type superClazz = clazz.getGenericSuperclass();
if (superClazz != null) {
writer.write(" extends ");
printType(superClazz);
}
// scan the interfaces
printTypes(clazz.getGenericInterfaces(), " implements ", ",", "");
writer.write("\n");
}
private void printMethod(Method method) throws IOException {
String name = method.getName();
writer.write(Modifier.toString(method.getModifiers()));
writer.write(" ");
printTypes(method.getTypeParameters(), "</span><span>",
"</span><span class=\"string\">",
"</span><span>, </span><span class=\"string\">");
printType(method.getGenericReturnType());
writer.write(" ");
writer.write(name);
writer.write("(");
printTypes(method.getGenericParameterTypes(), "", ", ", "");
writer.write(")\n");
}
private void printTypes(Type[] types, String prefix, String seperator,
String suffix) throws IOException {
if (types.length > 0)
writer.write(prefix);
for (int i = 0; i < types.length; i++) {
if (i > 0)
writer.write(seperator);
printType(types[i]);
}
if (types.length > 0)
writer.write(suffix);
}
private void printType(Type type) throws IOException {
if (type instanceof Class) {
Class t = (Class) type;
writer.write(t.getName());
} else if (type instanceof TypeVariable) {
TypeVariable t = (TypeVariable) type;
writer.write(t.getName());
printTypes(t.getBounds(), " extends ", ",", "");
} else if (type instanceof WildcardType) {
WildcardType t = (WildcardType) type;
writer.write("?");
printTypes(t.getLowerBounds(), " extends ", " & ", "");
printTypes(t.getUpperBounds(), " super ", " & ", "");
} else if (type instanceof ParameterizedType) {
ParameterizedType t = (ParameterizedType) type;
Type owner = t.getOwnerType();
if (owner != null) {
printType(owner);
writer.write(".");
}
printType(t.getRawType());
printTypes(t.getActualTypeArguments(), "</span><span>",
"</span><span class=\"string\">",
"</span><span>, </span><span class=\"string\">");
} else if (type instanceof GenericArrayType) {
GenericArrayType t = (GenericArrayType) type;
printType(t.getGenericComponentType());
writer.write("[]");
}
}
}