1 12 package org.aspectj.internal.lang.reflect; 13 14 import java.lang.reflect.ParameterizedType ; 15 import java.lang.reflect.Type ; 16 import java.lang.reflect.TypeVariable ; 17 import java.util.StringTokenizer ; 18 19 import org.aspectj.lang.reflect.AjTypeSystem; 20 21 25 public class StringToType { 26 27 public static Type [] commaSeparatedListToTypeArray(String typeNames, Class classScope) 28 throws ClassNotFoundException { 29 StringTokenizer strTok = new StringTokenizer (typeNames,","); 30 Type [] ret = new Type [strTok.countTokens()]; 31 int index = 0; 32 while (strTok.hasMoreTokens()) { 34 String typeName = strTok.nextToken().trim(); 35 ret[index++] = stringToType(typeName, classScope); 36 } 37 return ret; 38 } 39 40 public static Type stringToType(String typeName, Class classScope) 41 throws ClassNotFoundException { 42 try { 43 if (typeName.indexOf("<") == -1) { 44 return AjTypeSystem.getAjType(Class.forName(typeName,false,classScope.getClassLoader())); 45 } else { 46 return makeParameterizedType(typeName,classScope); 47 } 48 } catch (ClassNotFoundException e) { 49 TypeVariable [] tVars = classScope.getTypeParameters(); 51 for (int i = 0; i < tVars.length; i++) { 52 if (tVars[i].getName().equals(typeName)) { 53 return tVars[i]; 54 } 55 } 56 throw new ClassNotFoundException (typeName); 57 } 58 } 59 60 private static Type makeParameterizedType(String typeName, Class classScope) 61 throws ClassNotFoundException { 62 int paramStart = typeName.indexOf('<'); 63 String baseName = typeName.substring(0, paramStart); 64 final Class baseClass = Class.forName(baseName,false,classScope.getClassLoader()); 65 int paramEnd = typeName.lastIndexOf('>'); 66 String params = typeName.substring(paramStart+1,paramEnd); 67 final Type [] typeParams = commaSeparatedListToTypeArray(params,classScope); 68 return new ParameterizedType () { 69 70 public Type [] getActualTypeArguments() { 71 return typeParams; 72 } 73 74 public Type getRawType() { 75 return baseClass; 76 } 77 78 public Type getOwnerType() { 79 return baseClass.getEnclosingClass(); 80 } 81 }; 82 } 83 } 84 | Popular Tags |