Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 240   Methods: 12
NCLOC: 78   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DataType.java 80% 86.7% 83.3% 84.6%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.types;
 56   
 
 57   
 
 58   
 import java.util.Stack;
 59   
 import org.apache.tools.ant.BuildException;
 60   
 import org.apache.tools.ant.Project;
 61   
 import org.apache.tools.ant.ProjectComponent;
 62   
 
 63   
 /**
 64   
  * Base class for those classes that can appear inside the build file
 65   
  * as stand alone data types.  
 66   
  *
 67   
  * <p>This class handles the common description attribute and provides
 68   
  * a default implementation for reference handling and checking for
 69   
  * circular references that is appropriate for types that can not be
 70   
  * nested inside elements of the same type (i.e. &lt;patternset&gt;
 71   
  * but not &lt;path&gt;).</p>
 72   
  *
 73   
  * @author Stefan Bodewig 
 74   
  */
 75   
 public abstract class DataType extends ProjectComponent {
 76   
     /**
 77   
      * The description the user has set.
 78   
      *
 79   
      * @deprecated The user should not be directly referencing
 80   
      *   variable. Please use {@link #setDescription} or
 81   
      *   {@link #getDescription} instead.
 82   
      */
 83   
     protected String description;
 84   
 
 85   
     /**
 86   
      * Value to the refid attribute.
 87   
      *
 88   
      * @deprecated The user should not be directly referencing
 89   
      *   variable. Please use {@link #getRefid} instead.
 90   
      */
 91   
     protected Reference ref;
 92   
 
 93   
     /**
 94   
      * Are we sure we don't hold circular references?
 95   
      *
 96   
      * <p>Subclasses are responsible for setting this value to false
 97   
      * if we'd need to investigate this condition (usually because a
 98   
      * child element has been added that is a subclass of
 99   
      * DataType).</p>
 100   
      *
 101   
      * @deprecated The user should not be directly referencing
 102   
      *   variable. Please use {@link #setChecked} or
 103   
      *   {@link #isChecked} instead.
 104   
      */
 105   
     protected boolean checked = true;
 106   
     
 107   
     /** 
 108   
      * Sets a description of the current data type. It will be useful
 109   
      * in commenting what we are doing.  
 110   
      */
 111  0
     public void setDescription( final String desc ) {
 112  0
         description = desc;
 113   
     }
 114   
 
 115   
     /**
 116   
      * Return the description for the current data type.
 117   
      */
 118  0
     public String getDescription() {
 119  0
         return description;
 120   
     }
 121   
 
 122   
     /**
 123   
      * Has the refid attribute of this element been set?
 124   
      */
 125  713728
     public boolean isReference() {
 126  713728
         return ref != null;
 127   
     }
 128   
 
 129   
     /**
 130   
      * Set the value of the refid attribute.
 131   
      *
 132   
      * <p>Subclasses may need to check whether any other attributes
 133   
      * have been set as well or child elements have been created and
 134   
      * thus override this method. if they do the must call
 135   
      * <code>super.setRefid</code>.</p> 
 136   
      */
 137  164
     public void setRefid( final Reference ref ) {
 138  164
         this.ref = ref;
 139  164
         checked = false;
 140   
     }
 141   
 
 142   
     /**
 143   
      * Check to see whether any DataType we hold references to is
 144   
      * included in the Stack (which holds all DataType instances that
 145   
      * directly or indirectly reference this instance, including this
 146   
      * instance itself).
 147   
      *
 148   
      * <p>If one is included, throw a BuildException created by {@link
 149   
      * #circularReference circularReference}.</p>
 150   
      *
 151   
      * <p>This implementation is appropriate only for a DataType that
 152   
      * cannot hold other DataTypes as children.</p> 
 153   
      *
 154   
      * <p>The general contract of this method is that it shouldn't do
 155   
      * anything if {@link #checked <code>checked</code>} is true and
 156   
      * set it to true on exit.</p> 
 157   
      */
 158  190
     protected void dieOnCircularReference( final Stack stack,
 159   
                                            final Project project )
 160   
         throws BuildException {
 161   
 
 162  190
         if (checked || !isReference()) {
 163  89
             return;
 164   
         }
 165  101
         Object o = ref.getReferencedObject(project);
 166   
         
 167  101
         if (o instanceof DataType) {
 168  101
             if (stack.contains(o)) {
 169  20
                 throw circularReference();
 170   
             } else {
 171  81
                 stack.push(o);
 172  81
                 ((DataType) o).dieOnCircularReference(stack, project);
 173  61
                 stack.pop();
 174   
             }
 175   
         }
 176  61
         checked = true;
 177   
     }
 178   
 
 179   
     /**
 180   
      * Performs the check for circular references and returns the
 181   
      * referenced object.  
 182   
      */
 183  344711
     protected Object getCheckedRef( final Class requiredClass,
 184   
                                     final String dataTypeName ) {
 185  344711
         if (!checked) {
 186  43
             Stack stk = new Stack();
 187  43
             stk.push(this);
 188  43
             dieOnCircularReference(stk, getProject());
 189   
         }
 190   
         
 191  344711
         Object o = ref.getReferencedObject(getProject());
 192  344711
         if (!(requiredClass.isAssignableFrom(o.getClass()))) {
 193  0
             String msg = ref.getRefId() + " doesn\'t denote a " + dataTypeName;
 194  0
             throw new BuildException(msg);
 195   
         } else {
 196  344711
             return o;
 197   
         }
 198   
     }
 199   
 
 200   
     /**
 201   
      * Creates an exception that indicates that refid has to be the
 202   
      * only attribute if it is set.  
 203   
      */
 204  32
     protected BuildException tooManyAttributes() {
 205  32
         return new BuildException("You must not specify more than one " 
 206   
             + "attribute when using refid");
 207   
     }
 208   
 
 209   
     /**
 210   
      * Creates an exception that indicates that this XML element must
 211   
      * not have child elements if the refid attribute is set.  
 212   
      */
 213  22
     protected BuildException noChildrenAllowed() {
 214  22
         return new BuildException("You must not specify nested elements " 
 215   
             + "when using refid");
 216   
     }
 217   
 
 218   
     /**
 219   
      * Creates an exception that indicates the user has generated a
 220   
      * loop of data types referencing each other.  
 221   
      */
 222  22
     protected BuildException circularReference() {
 223  22
         return new BuildException("This data type contains a circular " 
 224   
             + "reference.");
 225   
     }
 226   
 
 227  3879
     protected boolean isChecked() {
 228  3879
         return checked;
 229   
     }
 230   
 
 231  437
     protected void setChecked( final boolean checked ) {
 232  437
         this.checked = checked;
 233   
     }
 234   
 
 235  21
     protected Reference getRefid()
 236   
     {
 237  21
         return ref;
 238   
     }
 239   
 }
 240