KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > reflect > TypeConverter


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.reflect;
9
10 import com.thoughtworks.qdox.model.Type;
11
12 /**
13  * Methods to convert Class to Java type names. Handles array types and the constructor "return" type.
14  *
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  * @author <a HREF="mailto:vta@medios.fi">Tibor Varga </a>
17  */

18 public class TypeConverter {
19     /**
20      * Converts an array of Classes to their Java language declaration equivalents.
21      *
22      * @param types is the array of <code>Class</code> objects.
23      * @return an array of Strings representing the given types. For <code>null</code> types, this method returns
24      * "void"s.
25      */

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

40     public static String JavaDoc convertTypeToJava(final Class JavaDoc type) {
41         String JavaDoc rv = null;
42
43         // constructor return type can be null
44
if (type != null) {
45             StringBuffer JavaDoc dim = new StringBuffer JavaDoc();
46             Class JavaDoc componentType = type.getComponentType();
47             for (Class JavaDoc nestedType = type; nestedType.isArray(); nestedType = nestedType.getComponentType()) {
48                 dim.append("[]");
49             }
50
51             // Found a component type => we had an array
52
if (dim.length() > 0) {
53                 rv = componentType.getName() + dim;
54             } else {
55                 rv = type.getName();
56             }
57         } else {
58             rv = "void";
59         }
60         return rv;
61     }
62
63     /**
64      * Converts a QDox type to a Java language declaration equivalent.
65      *
66      * @param type
67      * @return
68      */

69     public static String JavaDoc convertTypeToJava(final Type type) {
70         StringBuffer JavaDoc dim = new StringBuffer JavaDoc();
71         if (type.isArray()) {
72             for (int i = type.getDimensions(); i > 0; --i) {
73                 dim.append("[]");
74             }
75         }
76         return type.getValue() + dim;
77     }
78 }
Popular Tags