Dramatically simplified name simplification and in the process fixed the

handling of generic type arguments.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2339 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2008-07-22 13:47:20 +00:00
parent b619e1da06
commit b5069d97b3
+25 -33
View File
@@ -36,46 +36,38 @@ public class GenUtil
*/ */
public static String simpleName (Field field) public static String simpleName (Field field)
{ {
String cname; return simpleName(field.getGenericType());
Class<?> clazz = field.getType();
if (clazz.isArray()) {
if (field.getGenericType() instanceof GenericArrayType) {
GenericArrayType atype = (GenericArrayType)field.getGenericType();
cname = atype.getGenericComponentType().toString();
} else {
return simpleName(clazz.getComponentType(), field.getGenericType()) + "[]";
}
} else if (field.getGenericType() instanceof ParameterizedType) {
cname = field.getGenericType().toString();
} else {
cname = clazz.getName();
}
return simpleName(clazz, cname);
} }
/** /**
* Returns the name of the supplied class as it would likely appear in code using the class (no * Returns the name of the supplied class as it would likely appear in code using the class (no
* package prefix, arrays specified as <code>type[]</code>). * package prefix, arrays specified as <code>type[]</code>).
*/ */
public static String simpleName (Class<?> clazz, Type type) public static String simpleName (Type type)
{ {
if (clazz.isArray()) { if (type instanceof Class) {
return simpleName(clazz.getComponentType(), type) + "[]"; Class clazz = (Class)type;
} if (clazz.isArray()) {
String cname = clazz.getName(); return simpleName(clazz.getComponentType()) + "[]";
if (type instanceof ParameterizedType) { } else {
cname = type.toString(); Package pkg = clazz.getPackage();
} int offset = (pkg == null) ? 0 : pkg.getName().length()+1;
return simpleName(clazz, cname); return StringUtil.replace(clazz.getName().substring(offset), "$", ".");
} }
/** } else if (type instanceof ParameterizedType) {
* A helper function for the public simpleName methods. ParameterizedType pt = (ParameterizedType)type;
*/ StringBuilder buf = new StringBuilder();
protected static String simpleName (Class<?> clazz, String cname) for (Type arg : pt.getActualTypeArguments()) {
{ if (buf.length() > 0) {
Package pkg = clazz.getPackage(); buf.append(", ");
int offset = (pkg == null) ? 0 : pkg.getName().length()+1; }
return StringUtil.replace(cname.substring(offset), "$", "."); buf.append(simpleName(arg));
}
return simpleName(pt.getRawType()) + "<" + buf + ">";
} else {
throw new IllegalArgumentException("Can't generate simple name [type=" + type +
", tclass=" + StringUtil.shortClassName(type) + "]");
}
} }
} }