|
|||||||||||||||||||
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% |
|
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 | 0 |
public Object run() {
|
71 | 0 |
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 | 0 |
public MetaClassRegistry() {
|
82 | 0 |
this(true); |
83 |
} |
|
84 |
|
|
85 | 0 |
public MetaClassRegistry(int loadDefault) { |
86 | 0 |
if (loadDefault == LOAD_DEFAULT) {
|
87 | 0 |
this.useAccessible = true; |
88 |
// lets register the default methods
|
|
89 | 0 |
lookup(DefaultGroovyMethods.class).registerInstanceMethods();
|
90 | 0 |
lookup(DefaultGroovyStaticMethods.class).registerStaticMethods();
|
91 | 0 |
checkInitialised(); |
92 |
} else {
|
|
93 | 0 |
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 | 0 |
public MetaClassRegistry(boolean useAccessible) { |
103 | 0 |
this.useAccessible = useAccessible;
|
104 |
|
|
105 |
// lets register the default methods
|
|
106 | 0 |
lookup(DefaultGroovyMethods.class).registerInstanceMethods();
|
107 | 0 |
lookup(DefaultGroovyStaticMethods.class).registerStaticMethods();
|
108 | 0 |
checkInitialised(); |
109 |
} |
|
110 |
|
|
111 | 0 |
public MetaClass getMetaClass(Class theClass) {
|
112 | 0 |
MetaClass answer = (MetaClass) metaClasses.get(theClass); |
113 | 0 |
if (answer == null) { |
114 | 0 |
try {
|
115 | 0 |
answer = new MetaClass(this, theClass); |
116 | 0 |
answer.checkInitialised(); |
117 |
} catch (IntrospectionException e) {
|
|
118 | 0 |
throw new GroovyRuntimeException("Could not introspect class: " + theClass.getName() + ". Reason: " + e, |
119 |
e); |
|
120 |
} |
|
121 | 0 |
metaClasses.put(theClass, answer); |
122 |
} |
|
123 | 0 |
return answer;
|
124 |
} |
|
125 |
|
|
126 | 0 |
public void removeMetaClass(Class theClass) { |
127 | 0 |
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 | 0 |
public void setMetaClass(Class theClass, MetaClass theMetaClass) { |
138 | 0 |
metaClasses.put(theClass, theMetaClass); |
139 |
} |
|
140 |
|
|
141 | 0 |
public boolean useAccessible() { |
142 | 0 |
return useAccessible;
|
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* A helper class to load meta class bytecode into the class loader
|
|
147 |
*/
|
|
148 | 0 |
public Class loadClass(final String name, final byte[] bytecode) throws ClassNotFoundException { |
149 | 0 |
return (Class) AccessController.doPrivileged(new PrivilegedAction() { |
150 | 0 |
public Object run() {
|
151 | 0 |
return loader.defineClass(name, bytecode, getClass().getProtectionDomain());
|
152 |
} |
|
153 |
}); |
|
154 |
} |
|
155 |
|
|
156 | 0 |
public Class loadClass(String name) throws ClassNotFoundException { |
157 | 0 |
return loader.loadClass(name);
|
158 |
} |
|
159 |
|
|
160 |
/**
|
|
161 |
* Ensures that all the registered MetaClass instances are initalized
|
|
162 |
*/
|
|
163 | 0 |
void checkInitialised() {
|
164 |
// lets copy all the classes in the repository right now
|
|
165 |
// to avoid concurrent modification exception
|
|
166 | 0 |
List list = new ArrayList(metaClasses.values());
|
167 | 0 |
for (Iterator iter = list.iterator(); iter.hasNext();) {
|
168 | 0 |
MetaClass metaClass = (MetaClass) iter.next(); |
169 | 0 |
metaClass.checkInitialised(); |
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
/**
|
|
174 |
* Used by MetaClass when registering new methods which avoids initializing the MetaClass instances on lookup
|
|
175 |
*/
|
|
176 | 0 |
MetaClass lookup(Class theClass) { |
177 | 0 |
MetaClass answer = (MetaClass) metaClasses.get(theClass); |
178 | 0 |
if (answer == null) { |
179 | 0 |
try {
|
180 | 0 |
answer = new MetaClass(this, theClass); |
181 |
} catch (IntrospectionException e) {
|
|
182 | 0 |
throw new GroovyRuntimeException("Could not introspect class: " + theClass.getName() + ". Reason: " + e, |
183 |
e); |
|
184 |
} |
|
185 | 0 |
metaClasses.put(theClass, answer); |
186 |
} |
|
187 | 0 |
return answer;
|
188 |
} |
|
189 |
|
|
190 |
|
|
191 | 0 |
public MetaMethod getDefinedMethod(Class theClass, String methodName, Class[] args, boolean isStatic) { |
192 | 0 |
MetaClass metaclass = this.getMetaClass(theClass);
|
193 | 0 |
if (metaclass == null) { |
194 | 0 |
return null; |
195 |
} else {
|
|
196 | 0 |
if (isStatic) {
|
197 | 0 |
return metaclass.retrieveStaticMethod(methodName, args);
|
198 |
} else {
|
|
199 | 0 |
return metaclass.retrieveMethod(methodName, args);
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 | 0 |
public Constructor getDefinedConstructor(Class theClass, Class[] args) {
|
205 | 0 |
MetaClass metaclass = this.getMetaClass(theClass);
|
206 | 0 |
if (metaclass == null) { |
207 | 0 |
return null; |
208 |
} else {
|
|
209 | 0 |
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 | 0 |
public static MetaClassRegistry getIntance(int includeExtension) { |
220 | 0 |
if (includeExtension != DONT_LOAD_DEFAULT) {
|
221 | 0 |
if (instanceInclude == null) { |
222 | 0 |
instanceInclude = new MetaClassRegistry();
|
223 |
} |
|
224 | 0 |
return instanceInclude;
|
225 |
} else {
|
|
226 | 0 |
if (instanceExclude == null) { |
227 | 0 |
instanceExclude = new MetaClassRegistry(DONT_LOAD_DEFAULT);
|
228 |
} |
|
229 | 0 |
return instanceExclude;
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
|
|