View Javadoc

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