@@ -36,68 +36,65 @@ public class AntTypeDefinition {
private ClassLoader classLoader;
private ClassLoader classLoader;
/**
/**
* set the definition's name
* @param name the name of the definition
* Set the definition's name.
* @param name the name of the definition.
*/
*/
public void setName(String name) {
public void setName(String name) {
this.name = name;
this.name = name;
}
}
/**
/**
* return the definition's name
* @return the name of the definition
* Return the definition's name.
* @return the name of the definition.
*/
*/
public String getName() {
public String getName() {
return name;
return name;
}
}
/**
/**
* s et the class of the definition.
* a s a side-effect may set the classloader and classname
* @param clazz the class of this definition
* S et the class of the definition.
* A s a side-effect may set the classloader and classname.
* @param clazz the class of this definition.
*/
*/
public void setClass(Class clazz) {
public void setClass(Class clazz) {
this.clazz = clazz;
this.clazz = clazz;
if (clazz == null) {
if (clazz == null) {
return;
return;
}
}
if (classLoader == null) {
this.classLoader = clazz.getClassLoader();
}
if (className == null) {
this.className = clazz.getName();
}
this.classLoader = (classLoader == null)
? clazz.getClassLoader() : classLoader;
this.className = (className == null) ? clazz.getName() : className;
}
}
/**
/**
* set the classname of the definition
* @param className the classname of this definition
* Set the classname of the definition.
* @param className the classname of this definition.
*/
*/
public void setClassName(String className) {
public void setClassName(String className) {
this.className = className;
this.className = className;
}
}
/**
/**
* get the classname of the definition
* @return the name of the class of this definition
* Get the classname of the definition.
* @return the name of the class of this definition.
*/
*/
public String getClassName() {
public String getClassName() {
return className;
return className;
}
}
/**
/**
* s et the adapter class for this definition.
* t his class is used to adapt the definitions class if
* S et the adapter class for this definition.
* T his class is used to adapt the definitions class if
* required.
* required.
* @param adapterClass the adapterClass
* @param adapterClass the adapterClass.
*/
*/
public void setAdapterClass(Class adapterClass) {
public void setAdapterClass(Class adapterClass) {
this.adapterClass = adapterClass;
this.adapterClass = adapterClass;
}
}
/**
/**
* s et the assignable class for this definition.
* @param adaptToClass the assignable class
* S et the assignable class for this definition.
* @param adaptToClass the assignable class.
*/
*/
public void setAdaptToClass(Class adaptToClass) {
public void setAdaptToClass(Class adaptToClass) {
@@ -105,57 +102,50 @@ public class AntTypeDefinition {
}
}
/**
/**
* s et the classloader to use to create an instance
* of the definition
* @param classLoader the classLoader
* S et the classloader to use to create an instance
* of the definition.
* @param classLoader the ClassLoader.
*/
*/
public void setClassLoader(ClassLoader classLoader) {
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
this.classLoader = classLoader;
}
}
/**
/**
* get the classloader for this definition
* @return the classloader for this definition
* Get the classloader for this definition.
* @return the classloader for this definition.
*/
*/
public ClassLoader getClassLoader() {
public ClassLoader getClassLoader() {
return classLoader;
return classLoader;
}
}
/**
/**
* g et the exposed class for this
* G et the exposed class for this
* definition. This will be a proxy class
* definition. This will be a proxy class
* (adapted class) if there is an adapter
* (adapted class) if there is an adapter
* class and the definition class is not
* class and the definition class is not
* assignable from the assignable class.
* assignable from the assignable class.
* @param project the current project
* @return the exposed class
* @param project the current project.
* @return the exposed class.
*/
*/
public Class getExposedClass(Project project) {
public Class getExposedClass(Project project) {
if (adaptToClass != null) {
if (adaptToClass != null) {
Class z = getTypeClass(project);
Class z = getTypeClass(project);
if (z == null) {
return null;
}
if (adaptToClass.isAssignableFrom(z)) {
if (z == null || adaptToClass.isAssignableFrom(z)) {
return z;
return z;
}
}
}
}
if (adapterClass != null) {
return adapterClass;
}
return getTypeClass(project);
return (adapterClass == null) ? getTypeClass(project) : adapterClass;
}
}
/**
/**
* get the definition class
* @param project the current project
* @return the type of the definition
* Get the definition class.
* @param project the current project.
* @return the type of the definition.
*/
*/
public Class getTypeClass(Project project) {
public Class getTypeClass(Project project) {
if (clazz != null) {
if (clazz != null) {
return clazz;
return clazz;
}
}
try {
try {
if (classLoader == null) {
if (classLoader == null) {
clazz = Class.forName(className);
clazz = Class.forName(className);
@@ -174,10 +164,10 @@ public class AntTypeDefinition {
}
}
/**
/**
* c reate an instance of the definition.
* C reate an instance of the definition.
* The instance may be wrapped in a proxy class.
* The instance may be wrapped in a proxy class.
* @param project the current project
* @return the created object
* @param project the current project.
* @return the created object.
*/
*/
public Object create(Project project) {
public Object create(Project project) {
return icreate(project);
return icreate(project);
@@ -185,31 +175,28 @@ public class AntTypeDefinition {
/**
/**
* Create a component object based on
* Create a component object based on
* its definition
* its definition.
* @return the component as an <code>Object</code>.
*/
*/
private Object icreate(Project project) {
private Object icreate(Project project) {
Class c = getTypeClass(project);
Class c = getTypeClass(project);
if (c == null) {
if (c == null) {
return null;
return null;
}
}
Object o = createAndSet(project, c);
Object o = createAndSet(project, c);
if (o == null || adapterClass == null) {
if (o == null || adapterClass == null) {
return o;
return o;
}
}
if (adaptToClass != null) {
if (adaptToClass != null) {
if (adaptToClass.isAssignableFrom(o.getClass())) {
if (adaptToClass.isAssignableFrom(o.getClass())) {
return o;
return o;
}
}
}
}
TypeAdapter adapterObject = (TypeAdapter) createAndSet(
TypeAdapter adapterObject = (TypeAdapter) createAndSet(
project, adapterClass);
project, adapterClass);
if (adapterObject == null) {
if (adapterObject == null) {
return null;
return null;
}
}
adapterObject.setProxy(o);
adapterObject.setProxy(o);
return adapterObject;
return adapterObject;
}
}
@@ -222,7 +209,7 @@ public class AntTypeDefinition {
* <li>if the type is assignable from adapto</li>
* <li>if the type is assignable from adapto</li>
* <li>if the type can be used with the adapter class</li>
* <li>if the type can be used with the adapter class</li>
* </dl>
* </dl>
* @param project the current project
* @param project the current project.
*/
*/
public void checkClass(Project project) {
public void checkClass(Project project) {
if (clazz == null) {
if (clazz == null) {
@@ -233,26 +220,21 @@ public class AntTypeDefinition {
}
}
}
}
// check adapter
// check adapter
if (adapterClass != null) {
boolean needToCheck = true;
if (adaptToClass != null
&& adaptToClass.isAssignableFrom(clazz)) {
needToCheck = false;
}
if (needToCheck) {
TypeAdapter adapter = (TypeAdapter) createAndSet(
project, adapterClass);
if (adapter == null) {
throw new BuildException("Unable to create adapter object");
}
adapter.checkProxyClass(clazz);
if (adapterClass != null && (adaptToClass == null
|| !adaptToClass.isAssignableFrom(clazz))) {
TypeAdapter adapter = (TypeAdapter) createAndSet(
project, adapterClass);
if (adapter == null) {
throw new BuildException("Unable to create adapter object");
}
}
adapter.checkProxyClass(clazz);
}
}
}
}
/**
/**
* g et the constructor of the definition
* Get the constructor of the definition
* and invoke it.
* and invoke it.
* @return the instantiated <code>Object</code>.
*/
*/
private Object createAndSet(Project project, Class c) {
private Object createAndSet(Project project, Class c) {
try {
try {
@@ -267,13 +249,9 @@ public class AntTypeDefinition {
ctor = c.getConstructor(new Class[] {Project.class});
ctor = c.getConstructor(new Class[] {Project.class});
noArg = false;
noArg = false;
}
}
Object o = ctor.newInstance(
((noArg) ? new Object[0] : new Object[] {project}));
Object o = null;
if (noArg) {
o = ctor.newInstance(new Object[0]);
} else {
o = ctor.newInstance(new Object[] {project});
}
project.setProjectReference(o);
project.setProjectReference(o);
return o;
return o;
} catch (java.lang.reflect.InvocationTargetException ex) {
} catch (java.lang.reflect.InvocationTargetException ex) {
@@ -291,84 +269,53 @@ public class AntTypeDefinition {
}
}
/**
/**
* Equality method for this definition (assumes the names are the same)
* Equality method for this definition (assumes the names are the same).
*
*
* @param other another definition
* @param project the project the definition
* @return true if the definitions are the same
* @param other another definition.
* @param project the project the definition.
* @return true if the definitions are the same.
*/
*/
public boolean sameDefinition(AntTypeDefinition other, Project project) {
public boolean sameDefinition(AntTypeDefinition other, Project project) {
if (other == null) {
return false;
}
if (other.getClass() != this.getClass()) {
return false;
}
if (!(other.getTypeClass(project).equals(getTypeClass(project)))) {
return false;
}
if (!other.getExposedClass(project).equals(getExposedClass(project))) {
return false;
}
if (other.adapterClass != adapterClass) {
return false;
}
if (other.adaptToClass != adaptToClass) {
return false;
}
return true;
return (other != null && other.getClass() == getClass()
&& other.getTypeClass(project).equals(getTypeClass(project))
&& other.getExposedClass(project).equals(getExposedClass(project))
&& other.adapterClass == adapterClass
&& other.adaptToClass == adaptToClass);
}
}
/**
/**
* Similar definition
* Similar definition;
* used to compare two definitions defined twice with the same
* used to compare two definitions defined twice with the same
* name and the same types.
* name and the same types.
* t he classloader may be different but have the same
* The classloader may be different but have the same
* path so #sameDefinition cannot
* path so #sameDefinition cannot
* be used.
* be used.
* @param other the definition to compare to
* @param project the current project
* @return true if the definitions are the same
* @param other the definition to compare to.
* @param project the current project.
* @return true if the definitions are the same.
*/
*/
public boolean similarDefinition(AntTypeDefinition other, Project project) {
public boolean similarDefinition(AntTypeDefinition other, Project project) {
if (other == null) {
return false;
}
if (getClass() != other.getClass()) {
return false;
}
if (!getClassName().equals(other.getClassName())) {
return false;
}
if (!extractClassname(adapterClass).equals(
extractClassname(other.adapterClass))) {
return false;
}
if (!extractClassname(adaptToClass).equals(
extractClassname(other.adaptToClass))) {
if (other == null
|| getClass() != other.getClass()
|| !getClassName().equals(other.getClassName())
|| !extractClassname(adapterClass).equals(
extractClassname(other.adapterClass))
|| !extractClassname(adaptToClass).equals(
extractClassname(other.adaptToClass))) {
return false;
return false;
}
}
// all the names are the same: check if the class path of the loader
// all the names are the same: check if the class path of the loader
// is the same
// is the same
ClassLoader oldLoader = other.getClassLoader();
ClassLoader oldLoader = other.getClassLoader();
ClassLoader newLoader = this.getClassLoader();
if (oldLoader == newLoader) {
return true;
}
return
newLoader != null
&& oldLoader != null
&& oldLoader instanceof AntClassLoader
ClassLoader newLoader = getClassLoader();
return oldLoader == newLoader
|| (oldLoader instanceof AntClassLoader
&& newLoader instanceof AntClassLoader
&& newLoader instanceof AntClassLoader
&& ((AntClassLoader) oldLoader).getClasspath()
&& ((AntClassLoader) oldLoader).getClasspath()
.equals(((AntClassLoader) newLoader).getClasspath());
.equals(((AntClassLoader) newLoader).getClasspath()));
}
}
private String extractClassname(Class c) {
private String extractClassname(Class c) {
if (c == null) {
return "<null>";
} else {
return c.getClass().getName();
}
return (c == null) ? "<null>" : c.getClass().getName();
}
}
}
}