KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > reflect > TypeConverter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.reflect;
5
6 /**
7  * Methods to convert Class to Java type names. Handles array types and the constructor "return" type.
8  *
9  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
10  * @author <a HREF="mailto:vta@medios.fi">Tibor Varga </a>
11  */

12 public class TypeConverter {
13   /**
14    * Converts an array of Classes to their Java language declaration equivalents.
15    *
16    * @param types is the array of <code>Class</code> objects.
17    * @return an array of Strings representing the given types. For <code>null</code> types, this method returns
18    * "void"s.
19    */

20   public static String JavaDoc[] convertTypeToJava(final Class JavaDoc[] types) {
21     String JavaDoc[] parameterTypeNames = new String JavaDoc[types.length];
22     for (int i = 0; i < types.length; i++) {
23       parameterTypeNames[i] = convertTypeToJava(types[i]);
24     }
25     return parameterTypeNames;
26   }
27
28   /**
29    * Converts a Class to its Java language declaration equivalent.
30    *
31    * @param type is the <code>Class</code> object.
32    * @return a Strings representing the given types. For <code>null</code> type, this method returns "void".
33    */

34   public static String JavaDoc convertTypeToJava(final Class JavaDoc type) {
35     String JavaDoc rv = null;
36
37     // constructor return type can be null
38
if (type != null) {
39       StringBuffer JavaDoc dim = new StringBuffer JavaDoc();
40       Class JavaDoc componentType = type.getComponentType();
41       for (Class JavaDoc nestedType = type; nestedType.isArray(); nestedType = nestedType.getComponentType()) {
42         dim.append("[]");
43       }
44
45       // Found a component type => we had an array
46
if (dim.length() > 0) {
47         rv = componentType.getName() + dim;
48       } else {
49         rv = type.getName();
50       }
51     } else {
52       rv = "void";
53     }
54     return rv;
55   }
56 }
Popular Tags