001: package com.mockrunner.util.common;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006:
007: public class ClassUtil {
008: private final static String[] KEYWORDS = new String[] { "abstract",
009: "assert", "boolean", "break", "byte", "case", "catch",
010: "char", "class", "const", "continue", "default", "do",
011: "double", "else", "enum", "extends", "final", "finally",
012: "float", "for", "goto", "if", "implements", "import",
013: "instanceof", "int", "interface", "long", "native", "new",
014: "package", "private", "protected", "public", "return",
015: "short", "static", "strictFP", "super", "switch",
016: "synchronized", "this", "throw", "throws", "transient",
017: "try", "void", "volatile", "while" };
018:
019: /**
020: * Returns the name of the package of the specified class.
021: * If the class has no package, an empty String will be
022: * returned.
023: * @param clazz the Class
024: * @return the package name
025: */
026: public static String getPackageName(Class clazz) {
027: Package classPackage = clazz.getPackage();
028: if (null == classPackage)
029: return "";
030: return classPackage.getName();
031: }
032:
033: /**
034: * Returns the name of the specified class. This method
035: * only returns the class name without package information.
036: * If the specified class represents a primitive type, the
037: * name of the primitive type will be returned. If the
038: * specified class is an array, <code>[]</code> will be
039: * appended to the name (once for each dimension).
040: * @param clazz the Class
041: * @return the class name
042: */
043: public static String getClassName(Class clazz) {
044: String dimensions = "";
045: while (clazz.isArray()) {
046: clazz = clazz.getComponentType();
047: dimensions += "[]";
048: }
049: String classPackage = getPackageName(clazz);
050: if (classPackage.length() == 0) {
051: return clazz.getName() + dimensions;
052: } else {
053: return clazz.getName().substring(classPackage.length() + 1)
054: + dimensions;
055: }
056: }
057:
058: /**
059: * Returns the inheritance hierarchy of the specified class.
060: * The returned array includes all superclasses of the specified class
061: * starting with the most general superclass, which is
062: * <code>java.lang.Object</code>. The returned array also
063: * includes the class itself as the last element. Implemented
064: * interfaces are not included.
065: * @param clazz the Class
066: * @return all superclasses, most general superclass first
067: */
068: public static Class[] getInheritanceHierarchy(Class clazz) {
069: List classes = new ArrayList();
070: Class currentClass = clazz;
071: while (null != currentClass) {
072: classes.add(currentClass);
073: currentClass = currentClass.getSuperclass();
074: }
075: Collections.reverse(classes);
076: return (Class[]) classes.toArray(new Class[classes.size()]);
077: }
078:
079: /**
080: * Returns if the specified string is a Java language
081: * keyword.
082: * @param name the string
083: * @return <code>true</code> if it is a keyword,
084: * <code>false</code> otherwise
085: */
086: public static boolean isKeyword(String name) {
087: for (int ii = 0; ii < KEYWORDS.length; ii++) {
088: if (KEYWORDS[ii].equals(name))
089: return true;
090: }
091: return false;
092: }
093:
094: /**
095: * Returns a suitable argument name for arguments
096: * of type <code>argumentType</code>. Simply takes
097: * the class name and converts the starting characters
098: * to lower case (by preserving one upper case character).
099: * E.g. the result of <code>JMSTestModule</code> is
100: * <code>jmsTestModule</code>.
101: * If the specified <code>argumentType</code> is an array,
102: * an <code>"s"</code> is appended to the string.
103: * If the resulting string is a Java keyword, <code>"Value"</code>
104: * is appended to the string (which is always the case with
105: * primitive types).
106: * @param argumentType the argument type
107: * @return a suitable mixed case argument name
108: */
109: public static String getArgumentName(Class argumentType) {
110: String dimensions = "";
111: while (argumentType.isArray()) {
112: argumentType = argumentType.getComponentType();
113: dimensions = "s";
114: }
115: String name = getClassName(argumentType);
116: int index = 0;
117: while (index < name.length() - 1
118: && Character.isUpperCase(name.charAt(index))
119: && Character.isUpperCase(name.charAt(index + 1))) {
120: index++;
121: }
122: if (index == name.length() - 1) {
123: index++;
124: }
125: name = StringUtil.lowerCase(name, 0, index);
126: if (isKeyword(name)) {
127: name += "Value";
128: }
129: name += dimensions;
130: return name;
131: }
132: }
|