01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package org.codehaus.aspectwerkz.reflect;
08:
09: import com.thoughtworks.qdox.model.Type;
10:
11: /**
12: * Methods to convert Class to Java type names. Handles array types and the constructor "return" type.
13: *
14: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15: * @author <a href="mailto:vta@medios.fi">Tibor Varga </a>
16: */
17: public class TypeConverter {
18: /**
19: * Converts an array of Classes to their Java language declaration equivalents.
20: *
21: * @param types is the array of <code>Class</code> objects.
22: * @return an array of Strings representing the given types. For <code>null</code> types, this method returns
23: * "void"s.
24: */
25: public static String[] convertTypeToJava(final Class[] types) {
26: String[] parameterTypeNames = new String[types.length];
27: for (int i = 0; i < types.length; i++) {
28: parameterTypeNames[i] = convertTypeToJava(types[i]);
29: }
30: return parameterTypeNames;
31: }
32:
33: /**
34: * Converts a Class to its Java language declaration equivalent.
35: *
36: * @param type is the <code>Class</code> object.
37: * @return a Strings representing the given types. For <code>null</code> type, this method returns "void".
38: */
39: public static String convertTypeToJava(final Class type) {
40: String rv = null;
41:
42: // constructor return type can be null
43: if (type != null) {
44: StringBuffer dim = new StringBuffer();
45: Class componentType = type.getComponentType();
46: for (Class nestedType = type; nestedType.isArray(); nestedType = nestedType
47: .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 convertTypeToJava(final Type type) {
70: StringBuffer dim = new StringBuffer();
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: }
|