001 package com.mockrunner.util.common; 002 003 public class ClassUtil 004 { 005 private final static String[] KEYWORDS = new String[] 006 { 007 "abstract", "assert", "boolean", "break", "byte", 008 "case", "catch", "char", "class", "const", 009 "continue", "default", "do", "double", "else", 010 "extends", "final", "finally", "float", "for", 011 "goto", "if", "implements", "import", "instanceof", 012 "int", "interface", "long", "native", "new", 013 "package", "private", "protected", "public", "return", 014 "short", "static", "strictFP", "super", "switch", 015 "synchronized", "this", "throw", "throws", "transient", 016 "try", "void", "volatile", "while" 017 }; 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 { 028 Package classPackage = clazz.getPackage(); 029 if(null == classPackage) 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 { 045 String dimensions = ""; 046 while(clazz.isArray()) 047 { 048 clazz = clazz.getComponentType(); 049 dimensions += "[]"; 050 } 051 String classPackage = getPackageName(clazz); 052 if(classPackage.length() == 0) 053 { 054 return clazz.getName() + dimensions; 055 } 056 else 057 { 058 return clazz.getName().substring(classPackage.length() + 1) + dimensions; 059 } 060 } 061 062 /** 063 * Returns if the specified string is a Java language 064 * keyword. 065 * @param name the string 066 * @return <code>true</code> if it is a keyword, 067 * <code>false</code> otherwise 068 */ 069 public static boolean isKeyword(String name) 070 { 071 for(int ii = 0; ii < KEYWORDS.length; ii++) 072 { 073 if(KEYWORDS[ii].equals(name)) return true; 074 } 075 return false; 076 } 077 078 /** 079 * Returns a suitable argument name for arguments 080 * of type <code>argumentType</code>. Simply takes 081 * the class name and converts the starting characters 082 * to lower case (by preserving one upper case character). 083 * E.g. the result of <code>JMSTestModule</code> is 084 * <code>jmsTestModule</code>. 085 * If the specified <code>argumentType</code> is an array, 086 * an <code>"s"</code> is appended to the string. 087 * If the resulting string is a Java keyword, <code>"Value"</code> 088 * is appended to the string (what is always the case with 089 * primitive types). 090 * @param argumentType the argument type 091 * @return a suitable mixed case argument name 092 */ 093 public static String getArgumentName(Class argumentType) 094 { 095 String dimensions = ""; 096 while(argumentType.isArray()) 097 { 098 argumentType = argumentType.getComponentType(); 099 dimensions = "s"; 100 } 101 String name = getClassName(argumentType); 102 int index = 0; 103 while(index < name.length() - 1 && Character.isUpperCase(name.charAt(index)) && Character.isUpperCase(name.charAt(index + 1))) 104 { 105 index++; 106 } 107 if(index == name.length() - 1) 108 { 109 index++; 110 } 111 name = StringUtil.lowerCase(name, 0, index); 112 if(isKeyword(name)) 113 { 114 name += "Value"; 115 } 116 name += dimensions; 117 return name; 118 } 119 }