|
Ant example source code file (DataType.java)
The DataType.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.types;
import java.util.Stack;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ComponentHelper;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.util.IdentityStack;
/**
* Base class for those classes that can appear inside the build file
* as stand alone data types.
*
* <p>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>).</p>
*
*/
public abstract class DataType extends ProjectComponent implements Cloneable {
// CheckStyle:VisibilityModifier OFF
/**
* Value to the refid attribute.
*
* @deprecated since 1.7.
* The user should not be directly referencing
* variable. Please use {@link #getRefid} instead.
*/
protected Reference ref;
/**
* Are we sure we don't hold circular references?
*
* <p>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).</p>
*
* @deprecated since 1.7.
* The user should not be directly referencing
* variable. Please use {@link #setChecked} or
* {@link #isChecked} instead.
*/
protected boolean checked = true;
// CheckStyle:VisibilityModifier ON
/**
* Has the refid attribute of this element been set?
* @return true if the refid attribute has been set
*/
public boolean isReference() {
return ref != null;
}
/**
* Set the value of the refid attribute.
*
* <p>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
* <code>super.setRefid.
* @param ref the reference to use
*/
public void setRefid(final Reference ref) {
this.ref = ref;
checked = false;
}
/**
* Gets as descriptive as possible a name used for this datatype instance.
* @return <code>String name.
*/
protected String getDataTypeName() {
return ComponentHelper.getElementName(getProject(), this, true);
}
/**
* Convenience method.
* @since Ant 1.7
*/
protected void dieOnCircularReference() {
dieOnCircularReference(getProject());
}
/**
* Convenience method.
* @param p the Ant Project instance against which to resolve references.
* @since Ant 1.7
*/
protected void dieOnCircularReference(Project p) {
if (checked || !isReference()) {
return;
}
dieOnCircularReference(new IdentityStack(this), p);
}
/**
* 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).
*
* <p>If one is included, throw a BuildException created by {@link
* #circularReference circularReference}.</p>
*
* <p>This implementation is appropriate only for a DataType that
* cannot hold other DataTypes as children.</p>
*
* <p>The general contract of this method is that it shouldn't do
* anything if {@link #checked <code>checked} is true and
* set it to true on exit.</p>
* @param stack the stack of references to check.
* @param project the project to use to dereference the references.
* @throws BuildException on error.
*/
protected void dieOnCircularReference(final Stack stack,
final Project project)
throws BuildException {
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;
}
/**
* 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
*/
public static void invokeCircularReferenceCheck(DataType dt, Stack stk,
Project p) {
dt.dieOnCircularReference(stk, p);
}
/**
* 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
*/
protected Object getCheckedRef() {
return getCheckedRef(getProject());
}
/**
* 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
*/
protected Object getCheckedRef(Project p) {
return getCheckedRef(getClass(), getDataTypeName(), p);
}
/**
* 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).
*/
protected Object getCheckedRef(final Class requiredClass,
final String dataTypeName) {
return getCheckedRef(requiredClass, dataTypeName, getProject());
}
/**
* 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 <code>project is
Other Ant examples (source code examples)Here is a short list of links related to this Ant DataType.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.