FileDocCategorySizeDatePackage
DataType.javaAPI DocApache Ant 1.7011782Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types

DataType

public abstract class DataType extends org.apache.tools.ant.ProjectComponent implements Cloneable
Base class for those classes that can appear inside the build file as stand alone data types.

This class handles the common description attribute and provides a default implementation for reference handling and checking for circular references that is appropriate for types that can not be nested inside elements of the same type (i.e. <patternset> but not <path>).

Fields Summary
protected Reference
ref
Value to the refid attribute.
protected boolean
checked
Are we sure we don't hold circular references?

Subclasses are responsible for setting this value to false if we'd need to investigate this condition (usually because a child element has been added that is a subclass of DataType).

Constructors Summary
Methods Summary
protected voidcheckAttributesAllowed()
check that it is ok to set attributes, i.e that no reference is defined

since
Ant 1.6
throws
BuildException if not allowed

        if (isReference()) {
            throw tooManyAttributes();
        }
    
protected voidcheckChildrenAllowed()
check that it is ok to add children, i.e that no reference is defined

since
Ant 1.6
throws
BuildException if not allowed

        if (isReference()) {
            throw noChildrenAllowed();
        }
    
protected org.apache.tools.ant.BuildExceptioncircularReference()
Creates an exception that indicates the user has generated a loop of data types referencing each other.

return
the exception to throw

        return new BuildException("This data type contains a circular "
            + "reference.");
    
public java.lang.Objectclone()

since
Ant 1.7
return
a shallow copy of this DataType.
throws
CloneNotSupportedException if there is a problem.

        DataType dt = (DataType) super.clone();
        dt.setDescription(getDescription());
        if (getRefid() != null) {
            dt.setRefid(getRefid());
        }
        dt.setChecked(isChecked());
        return dt;
    
protected voiddieOnCircularReference()
Convenience method.

since
Ant 1.7

        dieOnCircularReference(getProject());
    
protected voiddieOnCircularReference(org.apache.tools.ant.Project p)
Convenience method.

param
p the Ant Project instance against which to resolve references.
since
Ant 1.7

        if (checked || !isReference()) {
            return;
        }
        dieOnCircularReference(new IdentityStack(this), p);
    
protected voiddieOnCircularReference(java.util.Stack stack, org.apache.tools.ant.Project project)
Check to see whether any DataType we hold references to is included in the Stack (which holds all DataType instances that directly or indirectly reference this instance, including this instance itself).

If one is included, throw a BuildException created by {@link #circularReference circularReference}.

This implementation is appropriate only for a DataType that cannot hold other DataTypes as children.

The general contract of this method is that it shouldn't do anything if {@link #checked checked} is true and set it to true on exit.

param
stack the stack of references to check.
param
project the project to use to dereference the references.
throws
BuildException on error.


        if (checked || !isReference()) {
            return;
        }
        Object o = ref.getReferencedObject(project);

        if (o instanceof DataType) {
            IdentityStack id = IdentityStack.getInstance(stack);

            if (id.contains(o)) {
                throw circularReference();
            } else {
                id.push(o);
                ((DataType) o).dieOnCircularReference(id, project);
                id.pop();
            }
        }
        checked = true;
    
protected java.lang.ObjectgetCheckedRef(java.lang.Class requiredClass, java.lang.String dataTypeName)
Performs the check for circular references and returns the referenced object.

param
requiredClass the class that this reference should be a subclass of.
param
dataTypeName the name of the datatype that the reference should be (error message use only).
return
the dereferenced object.
throws
BuildException if the reference is invalid (circular ref, wrong class, etc).

        return getCheckedRef(requiredClass, dataTypeName, getProject());
    
protected java.lang.ObjectgetCheckedRef(java.lang.Class requiredClass, java.lang.String dataTypeName, org.apache.tools.ant.Project project)
Performs the check for circular references and returns the referenced object. This version allows the fallback Project instance to be specified.

param
requiredClass the class that this reference should be a subclass of.
param
dataTypeName the name of the datatype that the reference should be (error message use only).
param
project the fallback Project instance for dereferencing.
return
the dereferenced object.
throws
BuildException if the reference is invalid (circular ref, wrong class, etc), or if project is null.
since
Ant 1.7

        if (project == null) {
            throw new BuildException("No Project specified");
        }
        dieOnCircularReference(project);
        Object o = ref.getReferencedObject(project);
        if (!(requiredClass.isAssignableFrom(o.getClass()))) {
            log("Class " + o.getClass() + " is not a subclass of " + requiredClass,
                    Project.MSG_VERBOSE);
            String msg = ref.getRefId() + " doesn\'t denote a " + dataTypeName;
            throw new BuildException(msg);
        }
        return o;
    
protected java.lang.ObjectgetCheckedRef()
Performs the check for circular references and returns the referenced object.

return
the dereferenced object.
throws
BuildException if the reference is invalid (circular ref, wrong class, etc).
since
Ant 1.7

        return getCheckedRef(getProject());
    
protected java.lang.ObjectgetCheckedRef(org.apache.tools.ant.Project p)
Performs the check for circular references and returns the referenced object.

param
p the Ant Project instance against which to resolve references.
return
the dereferenced object.
throws
BuildException if the reference is invalid (circular ref, wrong class, etc).
since
Ant 1.7

        return getCheckedRef(getClass(), getDataTypeName(), p);
    
protected java.lang.StringgetDataTypeName()
Gets as descriptive as possible a name used for this datatype instance.

return
String name.

        return ComponentHelper.getElementName(getProject(), this, true);
    
public ReferencegetRefid()
get the reference set on this object

return
the reference or null

        return ref;
    
public static voidinvokeCircularReferenceCheck(org.apache.tools.ant.types.DataType dt, java.util.Stack stk, org.apache.tools.ant.Project p)
Allow DataTypes outside org.apache.tools.ant.types to indirectly call dieOnCircularReference on nested DataTypes.

param
dt the DataType to check.
param
stk the stack of references to check.
param
p the project to use to dereference the references.
throws
BuildException on error.
since
Ant 1.7

        dt.dieOnCircularReference(stk, p);
    
protected booleanisChecked()
The flag that is used to indicate that circular references have been checked.

return
true if circular references have been checked

        return checked;
    
public booleanisReference()
Has the refid attribute of this element been set?

return
true if the refid attribute has been set

    // CheckStyle:VisibilityModifier ON

                           
       
        return ref != null;
    
protected org.apache.tools.ant.BuildExceptionnoChildrenAllowed()
Creates an exception that indicates that this XML element must not have child elements if the refid attribute is set.

return
the exception to throw

        return new BuildException("You must not specify nested elements "
            + "when using refid");
    
protected voidsetChecked(boolean checked)
Set the flag that is used to indicate that circular references have been checked.

param
checked if true, if circular references have been checked

        this.checked = checked;
    
public voidsetRefid(Reference ref)
Set the value of the refid attribute.

Subclasses may need to check whether any other attributes have been set as well or child elements have been created and thus override this method. if they do the must call super.setRefid.

param
ref the reference to use

        this.ref = ref;
        checked = false;
    
public java.lang.StringtoString()
Basic DataType toString().

return
this DataType formatted as a String.

        String d = getDescription();
        return d == null ? getDataTypeName() : getDataTypeName() + " " + d;
    
protected org.apache.tools.ant.BuildExceptiontooManyAttributes()
Creates an exception that indicates that refid has to be the only attribute if it is set.

return
the exception to throw

        return new BuildException("You must not specify more than one "
            + "attribute when using refid");