Clover coverage report - groovy - 1.0-beta-7
Coverage timestamp: Wed Sep 29 2004 16:55:52 BST
file stats: LOC: 237   Methods: 15
NCLOC: 134   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
MetaClassRegistry.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: MetaClassRegistry.java,v 1.15 2004/08/04 19:39:10 jez 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 groovy.lang;
 47   
 
 48   
 import java.beans.IntrospectionException;
 49   
 import java.lang.reflect.Constructor;
 50   
 import java.security.AccessController;
 51   
 import java.security.PrivilegedAction;
 52   
 import java.util.ArrayList;
 53   
 import java.util.Collections;
 54   
 import java.util.HashMap;
 55   
 import java.util.Iterator;
 56   
 import java.util.List;
 57   
 import java.util.Map;
 58   
 
 59   
 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
 60   
 import org.codehaus.groovy.runtime.DefaultGroovyStaticMethods;
 61   
 
 62   
 /**
 63   
  * A registery of MetaClass instances which caches introspection & 
 64   
  * reflection information and allows methods to be dynamically added to 
 65   
  * existing classes at runtime
 66   
  * 
 67   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 68   
  * @version $Revision: 1.15 $
 69   
  */
 70   
 public class MetaClassRegistry {
 71   
     private Map metaClasses = Collections.synchronizedMap(new HashMap());
 72   
     private boolean useAccessible;
 73   
     private GroovyClassLoader loader =  
 74   
         (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
 75  0
             public Object run() {
 76  0
                 return new GroovyClassLoader(getClass().getClassLoader()); 
 77   
             }
 78   
         });
 79   
 
 80   
     public static final int LOAD_DEFAULT = 0;
 81   
     public static final int DONT_LOAD_DEFAULT = 1;
 82   
     private static MetaClassRegistry instanceInclude;
 83   
     private static MetaClassRegistry instanceExclude;
 84   
 
 85   
 
 86  0
     public MetaClassRegistry() {
 87  0
         this(true);
 88   
     }
 89   
 
 90  0
     public MetaClassRegistry(int loadDefault) {
 91  0
         if (loadDefault == LOAD_DEFAULT) {
 92  0
             this.useAccessible = true;
 93   
             // lets register the default methods
 94  0
             lookup(DefaultGroovyMethods.class).registerInstanceMethods();
 95  0
             lookup(DefaultGroovyStaticMethods.class).registerStaticMethods();
 96  0
             checkInitialised();
 97   
         } else {
 98  0
             this.useAccessible = true;
 99   
             // do nothing to avoid loading DefaultGroovyMethod
 100   
         }
 101   
     }
 102   
 
 103   
     /**
 104   
      * @param useAccessible defines whether or not the {@link AccessibleObject.setAccessible()}
 105   
      * method will be called to enable access to all methods when using reflection
 106   
      */
 107  0
     public MetaClassRegistry(boolean useAccessible) {
 108  0
         this.useAccessible = useAccessible;
 109   
 
 110   
         // lets register the default methods
 111  0
         lookup(DefaultGroovyMethods.class).registerInstanceMethods();
 112  0
         lookup(DefaultGroovyStaticMethods.class).registerStaticMethods();
 113  0
         checkInitialised();
 114   
     }
 115   
 
 116  0
     public MetaClass getMetaClass(Class theClass) {
 117  0
         MetaClass answer = (MetaClass) metaClasses.get(theClass);
 118  0
         if (answer == null) {
 119  0
             try {
 120  0
                 answer = new MetaClass(this, theClass);
 121  0
                 answer.checkInitialised();
 122   
             }
 123   
             catch (IntrospectionException e) {
 124  0
                 throw new GroovyRuntimeException(
 125   
                     "Could not introspect class: " + theClass.getName() + ". Reason: " + e,
 126   
                     e);
 127   
             }
 128  0
             metaClasses.put(theClass, answer);
 129   
         }
 130  0
         return answer;
 131   
     }
 132   
 
 133   
     /**
 134   
      * Registers a new MetaClass in the registry to customize the type
 135   
      * 
 136   
      * @param theClass
 137   
      * @param theMetaClass
 138   
      */
 139  0
     public void setMetaClass(Class theClass, MetaClass theMetaClass) {
 140  0
         metaClasses.put(theClass, theMetaClass);
 141   
     }
 142   
 
 143  0
     public boolean useAccessible() {
 144  0
         return useAccessible;
 145   
     }
 146   
 
 147   
     /**
 148   
      * A helper class to load meta class bytecode into the class loader
 149   
      */
 150  0
     public Class loadClass(final String name, final byte[] bytecode) throws ClassNotFoundException {
 151  0
         return (Class) AccessController.doPrivileged(new PrivilegedAction() {
 152  0
             public Object run() {
 153  0
                 return loader.defineClass(name, bytecode, getClass().getProtectionDomain());
 154   
             }
 155   
         });
 156   
     }
 157   
 
 158  0
     public Class loadClass(String name) throws ClassNotFoundException {
 159  0
         return loader.loadClass(name);
 160   
     }
 161   
 
 162   
     /**
 163   
      * Ensures that all the registered MetaClass instances are initalized
 164   
      *
 165   
      */
 166  0
     void checkInitialised() {
 167   
         // lets copy all the classes in the repository right now 
 168   
         // to avoid concurrent modification exception
 169  0
         List list = new ArrayList(metaClasses.values());
 170  0
         for (Iterator iter = list.iterator(); iter.hasNext();) {
 171  0
             MetaClass metaClass = (MetaClass) iter.next();
 172  0
             metaClass.checkInitialised();
 173   
         }
 174   
     }
 175   
 
 176   
     /**
 177   
      * Used by MetaClass when registering new methods which avoids initializing the MetaClass instances on lookup
 178   
      */
 179  0
     MetaClass lookup(Class theClass) {
 180  0
         MetaClass answer = (MetaClass) metaClasses.get(theClass);
 181  0
         if (answer == null) {
 182  0
             try {
 183  0
                 answer = new MetaClass(this, theClass);
 184   
             }
 185   
             catch (IntrospectionException e) {
 186  0
                 throw new GroovyRuntimeException(
 187   
                     "Could not introspect class: " + theClass.getName() + ". Reason: " + e,
 188   
                     e);
 189   
             }
 190  0
             metaClasses.put(theClass, answer);
 191   
         }
 192  0
         return answer;
 193   
     }
 194   
 
 195   
 
 196  0
     public MetaMethod getDefinedMethod(Class theClass, String methodName, Class[] args, boolean isStatic) {
 197  0
         MetaClass metaclass = this.getMetaClass(theClass);
 198  0
         if (metaclass == null) {
 199  0
             return null;
 200   
         } else {
 201  0
             if (isStatic) {
 202  0
                 return metaclass.retrieveStaticMethod(methodName, args);
 203   
             } else {
 204  0
                 return metaclass.retrieveMethod(methodName, args);
 205   
             }
 206   
         }
 207   
     }
 208   
 
 209  0
     public Constructor getDefinedConstructor(Class theClass, Class[] args) {
 210  0
         MetaClass metaclass = this.getMetaClass(theClass);
 211  0
         if (metaclass == null) {
 212  0
             return null;
 213   
         } else {
 214  0
             return metaclass.retrieveConstructor(args);
 215   
         }
 216   
     }
 217   
             /**
 218   
      * Singleton of MetaClassRegistry. Shall we use threadlocal to store the instance?
 219   
      * @param includeExtension
 220   
      * @return
 221   
      */
 222  0
     public static MetaClassRegistry getIntance(int includeExtension) {
 223  0
         if (includeExtension != DONT_LOAD_DEFAULT) {
 224  0
             if (instanceInclude == null) {
 225  0
                 instanceInclude  = new MetaClassRegistry();
 226   
             }
 227  0
             return instanceInclude;
 228   
         }
 229   
         else {
 230  0
             if (instanceExclude == null) {
 231  0
                 instanceExclude  = new MetaClassRegistry(DONT_LOAD_DEFAULT);
 232   
             }
 233  0
             return instanceExclude;
 234   
         }
 235   
     }
 236   
 }
 237