KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > internal > lang > reflect > StringToType


1 /* *******************************************************************
2  * Copyright (c) 2005 Contributors.
3  * All rights reserved.
4  * This program and the accompanying materials are made available
5  * under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution and is available at
7  * http://eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Adrian Colyer Initial implementation
11  * ******************************************************************/

12 package org.aspectj.internal.lang.reflect;
13
14 import java.lang.reflect.ParameterizedType JavaDoc;
15 import java.lang.reflect.Type JavaDoc;
16 import java.lang.reflect.TypeVariable JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 import org.aspectj.lang.reflect.AjTypeSystem;
20
21 /**
22  * @author colyer
23  * Helper class for converting type representations in Strings into java.lang.reflect.Types.
24  */

25 public class StringToType {
26
27     public static Type JavaDoc[] commaSeparatedListToTypeArray(String JavaDoc typeNames, Class JavaDoc classScope)
28     throws ClassNotFoundException JavaDoc {
29         StringTokenizer JavaDoc strTok = new StringTokenizer JavaDoc(typeNames,",");
30         Type JavaDoc[] ret = new Type JavaDoc[strTok.countTokens()];
31         int index = 0;
32         //outer:
33
while (strTok.hasMoreTokens()) {
34             String JavaDoc typeName = strTok.nextToken().trim();
35             ret[index++] = stringToType(typeName, classScope);
36         }
37         return ret;
38     }
39     
40     public static Type JavaDoc stringToType(String JavaDoc typeName, Class JavaDoc classScope)
41     throws ClassNotFoundException JavaDoc {
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 JavaDoc e) {
49             // could be a type variable
50
TypeVariable JavaDoc[] 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 JavaDoc(typeName);
57         }
58     }
59     
60     private static Type JavaDoc makeParameterizedType(String JavaDoc typeName, Class JavaDoc classScope)
61     throws ClassNotFoundException JavaDoc {
62         int paramStart = typeName.indexOf('<');
63         String JavaDoc baseName = typeName.substring(0, paramStart);
64         final Class JavaDoc baseClass = Class.forName(baseName,false,classScope.getClassLoader());
65         int paramEnd = typeName.lastIndexOf('>');
66         String JavaDoc params = typeName.substring(paramStart+1,paramEnd);
67         final Type JavaDoc[] typeParams = commaSeparatedListToTypeArray(params,classScope);
68         return new ParameterizedType JavaDoc() {
69
70             public Type JavaDoc[] getActualTypeArguments() {
71                 return typeParams;
72             }
73
74             public Type JavaDoc getRawType() {
75                 return baseClass;
76             }
77
78             public Type JavaDoc getOwnerType() {
79                 return baseClass.getEnclosingClass();
80             }
81         };
82     }
83 }
84
Popular Tags