1 18 19 package org.apache.tools.ant; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Constructor ; 23 24 25 34 public class AntTypeDefinition { 35 private String name; 36 private Class clazz; 37 private Class adapterClass; 38 private Class adaptToClass; 39 private String className; 40 private ClassLoader classLoader; 41 42 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 54 public String getName() { 55 return name; 56 } 57 58 63 public void setClass(Class clazz) { 64 this.clazz = clazz; 65 if (clazz == null) { 66 return; 67 } 68 this.classLoader = (classLoader == null) 69 ? clazz.getClassLoader() : classLoader; 70 this.className = (className == null) ? clazz.getName() : className; 71 } 72 73 77 public void setClassName(String className) { 78 this.className = className; 79 } 80 81 85 public String getClassName() { 86 return className; 87 } 88 89 95 public void setAdapterClass(Class adapterClass) { 96 this.adapterClass = adapterClass; 97 } 98 99 103 104 public void setAdaptToClass(Class adaptToClass) { 105 this.adaptToClass = adaptToClass; 106 } 107 108 113 public void setClassLoader(ClassLoader classLoader) { 114 this.classLoader = classLoader; 115 } 116 117 121 public ClassLoader getClassLoader() { 122 return classLoader; 123 } 124 125 134 public Class getExposedClass(Project project) { 135 if (adaptToClass != null) { 136 Class z = getTypeClass(project); 137 if (z == null || adaptToClass.isAssignableFrom(z)) { 138 return z; 139 } 140 } 141 return (adapterClass == null) ? getTypeClass(project) : adapterClass; 142 } 143 144 149 public Class getTypeClass(Project project) { 150 try { 151 return innerGetTypeClass(); 152 } catch (NoClassDefFoundError ncdfe) { 153 project.log("Could not load a dependent class (" 154 + ncdfe.getMessage() + ") for type " 155 + name, Project.MSG_DEBUG); 156 } catch (ClassNotFoundException cnfe) { 157 project.log("Could not load class (" + className 158 + ") for type " + name, Project.MSG_DEBUG); 159 } 160 return null; 161 } 162 163 170 public Class innerGetTypeClass() throws ClassNotFoundException { 171 if (clazz != null) { 172 return clazz; 173 } 174 if (classLoader == null) { 175 clazz = Class.forName(className); 176 } else { 177 clazz = classLoader.loadClass(className); 178 } 179 return clazz; 180 } 181 182 188 public Object create(Project project) { 189 return icreate(project); 190 } 191 192 197 private Object icreate(Project project) { 198 Class c = getTypeClass(project); 199 if (c == null) { 200 return null; 201 } 202 Object o = createAndSet(project, c); 203 if (o == null || adapterClass == null) { 204 return o; 205 } 206 if (adaptToClass != null) { 207 if (adaptToClass.isAssignableFrom(o.getClass())) { 208 return o; 209 } 210 } 211 TypeAdapter adapterObject = (TypeAdapter) createAndSet( 212 project, adapterClass); 213 if (adapterObject == null) { 214 return null; 215 } 216 adapterObject.setProxy(o); 217 return adapterObject; 218 } 219 220 230 public void checkClass(Project project) { 231 if (clazz == null) { 232 clazz = getTypeClass(project); 233 if (clazz == null) { 234 throw new BuildException( 235 "Unable to create class for " + getName()); 236 } 237 } 238 if (adapterClass != null && (adaptToClass == null 240 || !adaptToClass.isAssignableFrom(clazz))) { 241 TypeAdapter adapter = (TypeAdapter) createAndSet( 242 project, adapterClass); 243 if (adapter == null) { 244 throw new BuildException("Unable to create adapter object"); 245 } 246 adapter.checkProxyClass(clazz); 247 } 248 } 249 250 255 private Object createAndSet(Project project, Class c) { 256 try { 257 Object o = innerCreateAndSet(c, project); 258 return o; 259 } catch (InvocationTargetException ex) { 260 Throwable t = ex.getTargetException(); 261 throw new BuildException( 262 "Could not create type " + name + " due to " + t, t); 263 } catch (NoClassDefFoundError ncdfe) { 264 String msg = "Type " + name + ": A class needed by class " 265 + c + " cannot be found: " + ncdfe.getMessage(); 266 throw new BuildException(msg, ncdfe); 267 } catch (NoSuchMethodException nsme) { 268 throw new BuildException("Could not create type " + name 269 + " as the class " + c + " has no compatible constructor"); 270 } catch (InstantiationException nsme) { 271 throw new BuildException("Could not create type " 272 + name + " as the class " + c + " is abstract"); 273 } catch (IllegalAccessException e) { 274 throw new BuildException("Could not create type " 275 + name + " as the constructor " + c + " is not accessible"); 276 } catch (Throwable t) { 277 throw new BuildException( 278 "Could not create type " + name + " due to " + t, t); 279 } 280 } 281 282 293 public Object innerCreateAndSet(Class newclass, Project project) 294 throws NoSuchMethodException , 295 InstantiationException , 296 IllegalAccessException , 297 InvocationTargetException { 298 Constructor ctor = null; 299 boolean noArg = false; 300 try { 303 ctor = newclass.getConstructor(new Class [0]); 304 noArg = true; 305 } catch (NoSuchMethodException nse) { 306 ctor = newclass.getConstructor(new Class [] {Project.class}); 308 noArg = false; 309 } 310 Object o = ctor.newInstance( 312 ((noArg) ? new Object [0] : new Object [] {project})); 313 314 project.setProjectReference(o); 316 return o; 317 } 318 319 326 public boolean sameDefinition(AntTypeDefinition other, Project project) { 327 return (other != null && other.getClass() == getClass() 328 && other.getTypeClass(project).equals(getTypeClass(project)) 329 && other.getExposedClass(project).equals(getExposedClass(project)) 330 && other.adapterClass == adapterClass 331 && other.adaptToClass == adaptToClass); 332 } 333 334 345 public boolean similarDefinition(AntTypeDefinition other, Project project) { 346 if (other == null 347 || getClass() != other.getClass() 348 || !getClassName().equals(other.getClassName()) 349 || !extractClassname(adapterClass).equals( 350 extractClassname(other.adapterClass)) 351 || !extractClassname(adaptToClass).equals( 352 extractClassname(other.adaptToClass))) { 353 return false; 354 } 355 ClassLoader oldLoader = other.getClassLoader(); 358 ClassLoader newLoader = getClassLoader(); 359 return oldLoader == newLoader 360 || (oldLoader instanceof AntClassLoader 361 && newLoader instanceof AntClassLoader 362 && ((AntClassLoader) oldLoader).getClasspath() 363 .equals(((AntClassLoader) newLoader).getClasspath())); 364 } 365 366 private String extractClassname(Class c) { 367 return (c == null) ? "<null>" : c.getClass().getName(); 368 } 369 } 370 | Popular Tags |