1 22 23 24 package com.mchange.v1.lang; 25 26 import java.util.*; 27 import com.mchange.v1.jvm.*; 28 29 public final class ClassUtils 30 { 31 final static String [] EMPTY_SA = new String [0]; 32 33 static Map primitivesToClasses; 34 35 static 36 { 37 HashMap tmp = new HashMap(); 38 tmp.put( "boolean", boolean.class ); 39 tmp.put( "int", int.class ); 40 tmp.put( "char", char.class ); 41 tmp.put( "short", short.class ); 42 tmp.put( "int", int.class ); 43 tmp.put( "long", long.class ); 44 tmp.put( "float", float.class ); 45 tmp.put( "double", double.class ); 46 tmp.put( "void", void.class ); 47 48 primitivesToClasses = Collections.unmodifiableMap( tmp ); 49 } 50 51 public static Set allAssignableFrom(Class type) 52 { 53 Set out = new HashSet(); 54 55 for (Class cl = type; cl != null; cl = cl.getSuperclass()) 57 out.add( cl ); 58 59 addSuperInterfacesToSet( type, out ); 61 return out; 62 } 63 64 public static String simpleClassName(Class cl) 65 { 66 String scn; 67 int array_level = 0; 68 while (cl.isArray()) 69 { 70 ++array_level; 71 cl = cl.getComponentType(); 72 } 73 scn = simpleClassName( cl.getName() ); 74 if ( array_level > 0 ) 75 { 76 StringBuffer sb = new StringBuffer (16); 77 sb.append( scn ); 78 for( int i = 0; i < array_level; ++i) 79 sb.append("[]"); 80 return sb.toString(); 81 } 82 else 83 return scn; 84 } 85 86 private static String simpleClassName(String fqcn) 87 { 88 int pkgdot = fqcn.lastIndexOf('.'); 89 if (pkgdot < 0) 90 return fqcn; 91 String scn = fqcn.substring(pkgdot + 1); 92 if (scn.indexOf('$') >= 0) 93 { 94 StringBuffer sb = new StringBuffer (scn); 95 for (int i = 0, len = sb.length(); i < len; ++i) 96 { 97 if (sb.charAt(i) == '$') 98 sb.setCharAt(i, '.'); 99 } 100 return sb.toString(); 101 } 102 else 103 return scn; 104 } 105 106 public static boolean isPrimitive(String typeStr) 107 { return (primitivesToClasses.get( typeStr ) != null); } 108 109 public static Class classForPrimitive(String typeStr) 110 { return (Class ) primitivesToClasses.get( typeStr ); } 111 112 public static Class forName(String fqcnOrPrimitive ) throws ClassNotFoundException 113 { 114 Class out = classForPrimitive( fqcnOrPrimitive ); 115 if (out == null) 116 out = Class.forName( fqcnOrPrimitive ); 117 return out; 118 } 119 120 public static Class forName( String fqOrSimple, String [] importPkgs, String [] importClasses ) 121 throws AmbiguousClassNameException, ClassNotFoundException 122 { 123 try 124 { return Class.forName( fqOrSimple ); } 125 catch ( ClassNotFoundException e ) 126 { return classForSimpleName( fqOrSimple, importPkgs, importClasses ); } 127 } 128 129 public static Class classForSimpleName( String simpleName, String [] importPkgs, String [] importClasses ) 130 throws AmbiguousClassNameException, ClassNotFoundException 131 { 132 Set checkSet = new HashSet(); 133 Class out = classForPrimitive( simpleName ); 134 135 if (out == null) 136 { 137 if (importPkgs == null) 138 importPkgs = EMPTY_SA; 139 140 if (importClasses == null) 141 importClasses = EMPTY_SA; 142 143 for (int i = 0, len = importClasses.length; i < len; ++i) 144 { 145 String importSimpleName = fqcnLastElement( importClasses[i] ); 146 if (! checkSet.add( importSimpleName ) ) 147 throw new IllegalArgumentException ("Duplicate imported classes: " + 148 importSimpleName); 149 if ( simpleName.equals( importSimpleName ) ) 150 out = Class.forName( importClasses[i] ); 152 } 153 if (out == null) 154 { 155 try { out = Class.forName("java.lang." + simpleName); } 156 catch (ClassNotFoundException e) 157 { } 158 159 for (int i = 0, len = importPkgs.length; i < len; ++i) 160 { 161 try 162 { 163 String tryClass = importPkgs[i] + '.' + simpleName; 164 Class test = Class.forName( tryClass ); 165 if ( out == null ) 166 out = test; 167 else 168 throw new AmbiguousClassNameException( simpleName, out, test ); 169 } 170 catch (ClassNotFoundException e) 171 { } 172 } 173 } 174 } 175 if (out == null) 176 throw new ClassNotFoundException ( "Could not find a class whose unqualified name is \042" + 177 simpleName + "\042 with the imports supplied. Import packages are " + 178 Arrays.asList( importPkgs ) + "; class imports are " + 179 Arrays.asList( importClasses ) ); 180 else 181 return out; 182 } 183 184 public static String resolvableTypeName( Class type, String [] importPkgs, String [] importClasses ) 185 throws ClassNotFoundException 186 { 187 String simpleName = simpleClassName( type ); 188 try 189 { classForSimpleName( simpleName, importPkgs, importClasses ); } 190 catch ( AmbiguousClassNameException e ) 191 { return type.getName(); } 192 return simpleName; 193 } 194 195 public static String fqcnLastElement(String fqcn) 196 { 197 int pkgdot = fqcn.lastIndexOf('.'); 198 if (pkgdot < 0) 199 return fqcn; 200 return fqcn.substring(pkgdot + 1); 201 } 202 203 204 205 private static void addSuperInterfacesToSet(Class type, Set set) 206 { 207 Class [] ifaces = type.getInterfaces(); 208 for (int i = 0, len = ifaces.length; i < len; ++i) 209 { 210 set.add( ifaces[i] ); 211 addSuperInterfacesToSet( ifaces[i], set ); 212 } 213 } 214 215 private ClassUtils() 216 {} 217 } 218 | Popular Tags |