001: package net.sf.mockcreator.codegeneration;
002:
003: import java.lang.reflect.GenericArrayType;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.ParameterizedType;
006: import java.lang.reflect.Type;
007: import java.lang.reflect.TypeVariable;
008: import java.lang.reflect.WildcardType;
009:
010: public class TypeUtilsJava15 {
011: public String getCanonicalTypeSpelling(Type type) {
012: if (type instanceof ParameterizedType) {
013: return getCanonicalTypeSpellingForRawType(type);
014: } else if (type instanceof GenericArrayType) {
015: GenericArrayType gat = (GenericArrayType) type;
016: return getCanonicalTypeSpelling(gat
017: .getGenericComponentType())
018: + "[]";
019: } else if (type instanceof WildcardType) {
020: return getCanonicalTypeSpellingForWildCardType(type);
021: } else if (type instanceof TypeVariable) {
022: return getCanonicalTypeSpellingForTypeVariable(type);
023: } else if (type instanceof Class) {
024: return getCanonicalTypeSpelling((Class) type);
025: }
026:
027: throw new IllegalArgumentException("" + type);
028: }
029:
030: public String getCanonicalTypeSpelling(Class cls) {
031: String rc = "";
032: while (cls.isArray()) {
033: rc += "[]";
034: cls = cls.getComponentType();
035: }
036:
037: rc = cls.getName() + rc;
038: return rc.replace('$', '.');
039: }
040:
041: private String getCanonicalTypeSpellingForTypeVariable(Type type) {
042: TypeVariable tv = (TypeVariable) type;
043: return tv.getName();
044: }
045:
046: public String getFullCanonicalTypeSpellingForTypeVariable(Type type) {
047: TypeVariable tv = (TypeVariable) type;
048: Type[] bounds = tv.getBounds();
049:
050: StringBuffer sb = new StringBuffer();
051: sb.append(tv.getName());
052: for (int i = 0; i < bounds.length; ++i) {
053: sb.append(" extends ");
054: sb.append(getCanonicalTypeSpelling(bounds[i]));
055: }
056: return sb.toString();
057: }
058:
059: private String getCanonicalTypeSpellingForWildCardType(Type type) {
060: WildcardType wc = (WildcardType) type;
061:
062: Type lower[] = wc.getLowerBounds();
063: Type upper[] = wc.getUpperBounds();
064:
065: StringBuffer sb = new StringBuffer();
066: sb.append("?");
067:
068: if (lower.length > 0) {
069: String canon = getCanonicalTypeSpelling(lower[0]);
070: sb.append(" super ");
071: sb.append(canon);
072: }
073:
074: if (upper.length > 0) {
075: String canon = getCanonicalTypeSpelling(upper[0]);
076: if (!"java.lang.Object".equals(canon)) {
077: sb.append(" extends ");
078: sb.append(canon);
079: }
080: }
081:
082: return sb.toString();
083: }
084:
085: private String getCanonicalTypeSpellingForRawType(Type type) {
086: ParameterizedType pt = (ParameterizedType) type;
087: String rawType = getCanonicalTypeSpelling(pt.getRawType());
088: Type[] params = pt.getActualTypeArguments();
089:
090: StringBuffer sb = new StringBuffer();
091: for (int i = 0; i < params.length; i++) {
092: if (sb.length() > 0)
093: sb.append(',');
094: sb.append(getCanonicalTypeSpelling(params[i]));
095: }
096: return rawType + "<" + sb.toString() + ">";
097: }
098:
099: public String getCanonicalMethodParameters(Method method) {
100: TypeVariable<Method>[] tvs = method.getTypeParameters();
101: if (tvs.length == 0)
102: return "";
103:
104: StringBuffer sb = new StringBuffer("<");
105: for (int i = 0; i < tvs.length; ++i) {
106: if (i != 0)
107: sb.append(',');
108: sb
109: .append(getFullCanonicalTypeSpellingForTypeVariable(tvs[i]));
110: }
111: sb.append(">");
112: return sb.toString();
113: }
114: }
|