Clover coverage report - groovy - 1.0-beta-7
Coverage timestamp: Wed Sep 29 2004 16:55:52 BST
file stats: LOC: 208   Methods: 13
NCLOC: 83   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
VariableScope.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: VariableScope.java,v 1.3 2004/07/10 03:31:38 bran Exp $
 3   
 
 4   
  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
 5   
 
 6   
  Redistribution and use of this software and associated documentation
 7   
  ("Software"), with or without modification, are permitted provided
 8   
  that the following conditions are met:
 9   
 
 10   
  1. Redistributions of source code must retain copyright
 11   
     statements and notices.  Redistributions must also contain a
 12   
     copy of this document.
 13   
 
 14   
  2. Redistributions in binary form must reproduce the
 15   
     above copyright notice, this list of conditions and the
 16   
     following disclaimer in the documentation and/or other
 17   
     materials provided with the distribution.
 18   
 
 19   
  3. The name "groovy" must not be used to endorse or promote
 20   
     products derived from this Software without prior written
 21   
     permission of The Codehaus.  For written permission,
 22   
     please contact info@codehaus.org.
 23   
 
 24   
  4. Products derived from this Software may not be called "groovy"
 25   
     nor may "groovy" appear in their names without prior written
 26   
     permission of The Codehaus. "groovy" is a registered
 27   
     trademark of The Codehaus.
 28   
 
 29   
  5. Due credit should be given to The Codehaus -
 30   
     http://groovy.codehaus.org/
 31   
 
 32   
  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
 33   
  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 34   
  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 35   
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 36   
  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 37   
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 38   
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 39   
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 40   
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 41   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 42   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 43   
  OF THE POSSIBILITY OF SUCH DAMAGE.
 44   
 
 45   
  */
 46   
 package org.codehaus.groovy.ast;
 47   
 
 48   
 import java.util.ArrayList;
 49   
 import java.util.HashSet;
 50   
 import java.util.Iterator;
 51   
 import java.util.List;
 52   
 import java.util.Set;
 53   
 
 54   
 /**
 55   
  * Represents a variable scope. This is primarily used to determine variable sharing
 56   
  * across method and closure boundaries.
 57   
  *
 58   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 59   
  * @version $Revision: 1.3 $
 60   
  */
 61   
 public class VariableScope  {
 62   
     static int i = 0;
 63   
     private Set declaredVariables = new HashSet(); // contain var names
 64   
     private Set referencedVariables = new HashSet(); // contain var names
 65   
 
 66   
     /**
 67   
      *  br contain vars really declared and defined in the current scope.
 68   
      * to be filler later by comparing with parent scope all the ancestors.
 69   
      */
 70   
     private Set varsDeclaredHere = null;
 71   
     // each var should really have a scope decarator
 72   
     // for now it's a naked name, which has the problem of name clashing with parameters
 73   
     // and ancesters' vars with the same name. Effectively this means we cannot have a
 74   
     // local var defined with the same name with any parameters or vars in any of the
 75   
     // ancestors.
 76   
     // we need to reengineer the scope and vars framework.
 77   
 
 78   
     private VariableScope parent;
 79   
     private List children = new ArrayList();
 80   
     String name = null;
 81   
 
 82  0
     public VariableScope() {
 83  0
         name = String.valueOf(i++);  // should give it a more meaningful name, w/ class, methed, blockstatement etc.
 84   
     }
 85   
 
 86  0
     public VariableScope(VariableScope parent) {
 87  0
         this.parent = parent;
 88  0
         parent.children.add(this);
 89  0
         name = String.valueOf(i);
 90   
     }
 91   
 
 92  0
     public Set getDeclaredVariables() {
 93  0
         return declaredVariables;
 94   
     }
 95   
 
 96  0
     public Set getReferencedVariables() {
 97  0
         return referencedVariables;
 98   
     }
 99   
 
 100   
     /**
 101   
      * @return all the child scopes
 102   
      */
 103  0
     public List getChildren() {
 104  0
         return children;
 105   
     }
 106   
 
 107   
     /**
 108   
      * Creates a composite variable scope combining all the variable references
 109   
      * and declarations from all the child scopes not including this scope
 110   
      *
 111   
      * @return
 112   
      */
 113  0
     public VariableScope createCompositeChildScope() {
 114  0
         VariableScope answer = new VariableScope();
 115  0
         for (Iterator iter = children.iterator(); iter.hasNext(); ) {
 116  0
             answer.appendRecursive((VariableScope) iter.next());
 117   
         }
 118  0
         answer.parent = this;  // shall we cache this for better performance?
 119  0
         return answer;
 120   
     }
 121   
 
 122   
     /**
 123   
      * Creates a scope including this scope and all nested scopes combined together
 124   
      *
 125   
      * @return
 126   
      */
 127  0
     public VariableScope createRecursiveChildScope() {
 128  0
         VariableScope answer = createCompositeChildScope();
 129  0
         answer.referencedVariables.addAll(referencedVariables);
 130  0
         answer.declaredVariables.addAll(declaredVariables);
 131  0
         return answer;
 132   
     }
 133   
 
 134   
     /**
 135   
      * Creates a scope including this scope and all parent scopes combined together
 136   
      *
 137   
      * @return
 138   
      */
 139  0
     public VariableScope createRecursiveParentScope() {
 140  0
         VariableScope answer = new VariableScope();
 141  0
         VariableScope node = this;
 142  0
         do {
 143  0
             answer.append(node);
 144  0
             node = node.parent;
 145   
         }
 146  0
         while (node != null);
 147  0
         return answer;
 148   
     }
 149   
 
 150   
     /**
 151   
      * Appends all of the references and declarations from the given scope
 152   
      * to this one
 153   
      *
 154   
      * @param scope
 155   
      */
 156  0
     protected void append(VariableScope scope) {
 157  0
         referencedVariables.addAll(scope.referencedVariables);
 158  0
         declaredVariables.addAll(scope.declaredVariables);
 159   
     }
 160   
 
 161   
     /**
 162   
      * Appends all of the references and declarations from the given scope
 163   
      * and all its children to this one
 164   
      *
 165   
      * @param scope
 166   
      */
 167  0
     protected void appendRecursive(VariableScope scope) {
 168  0
         append(scope);
 169   
 
 170   
         // now lets traverse the children
 171  0
         for (Iterator iter = scope.children.iterator(); iter.hasNext(); ) {
 172  0
             appendRecursive((VariableScope) iter.next());
 173   
         }
 174   
      }
 175   
 
 176   
     /*
 177   
      * compute the real declares in the current scope
 178   
      * @author bran
 179   
      *
 180   
      * To change the template for this generated type comment go to
 181   
      * Window - Preferences - Java - Code Generation - Code and Comments
 182   
      */
 183  0
     public void computeRealDeclares() {
 184  0
         if (this.varsDeclaredHere == null) this.varsDeclaredHere = new HashSet();
 185  0
         Set decls = this.declaredVariables;
 186  0
         for (Iterator iter = decls.iterator(); iter.hasNext();) {
 187  0
             String var = (String) iter.next();
 188   
             // simplistic way of determing declares by simple name matching
 189  0
             if (!createRecursiveParentScope().getDeclaredVariables().contains(var)) {
 190  0
                 this.varsDeclaredHere.add(var);
 191   
             }
 192   
         }
 193   
     }
 194   
     /**
 195   
      * @return Returns the varsDeclaredHere.
 196   
      */
 197  0
     public Set getVarsDeclaredHere() {
 198  0
         if (this.varsDeclaredHere == null) computeRealDeclares();
 199  0
         return varsDeclaredHere;
 200   
     }
 201   
     /**
 202   
      * @param varsDeclaredHere The varsDeclaredHere to set.
 203   
      */
 204  0
     public void setVarsDeclaredHere(Set varsDeclaredHere) {
 205  0
         this.varsDeclaredHere = varsDeclaredHere;
 206   
     }
 207   
 }
 208