groovy.lang.ExpandoMetaClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovy-all-minimal Show documentation
Show all versions of groovy-all-minimal Show documentation
Groovy: A powerful, dynamic language for the JVM
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.lang;
import org.codehaus.groovy.reflection.CachedClass;
import org.codehaus.groovy.reflection.ReflectionCache;
import org.codehaus.groovy.runtime.*;
import org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod;
import org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod;
import org.codehaus.groovy.runtime.metaclass.ConcurrentReaderHashMap;
import org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
/**
* A MetaClass that implements GroovyObject and behaves like an Expando, allowing the addition of new methods on the fly
*
*
* // defines or replaces instance method:
* metaClass.myMethod = { args -> }
*
* // defines a new instance method
* metaClass.myMethod << { args -> }
*
* // creates multiple overloaded methods of the same name
* metaClass.myMethod << { String s -> } << { Integer i -> }
*
* // defines or replaces a static method with the 'static' qualifier
* metaClass.'static'.myMethod = { args -> }
*
* // defines a new static method with the 'static' qualifier
* metaClass.'static'.myMethod << { args -> }
*
* // defines a new constructor
* metaClass.constructor << { String arg -> }
*
* // defines or replaces a constructor
* metaClass.constructor = { String arg -> }
*
* // defines a new property with an initial value of "blah"
* metaClass.myProperty = "blah"
*
*
*
* By default methods are only allowed to be added before initialize() is called. In other words you create a new
* ExpandoMetaClass, add some methods and then call initialize(). If you attempt to add new methods after initialize()
* has been called an error will be thrown.
*
* This is to ensure that the MetaClass can operate appropriately in multi threaded environments as it forces you
* to do all method additions at the beginning, before using the MetaClass.
*
* If you need more fine grained control of how a method is matched you can use DynamicMethodsMetaClass
*
* WARNING: This MetaClass uses a thread-bound ThreadLocal instance to store and retrieve properties.
* In addition properties stored use soft references so they are both bound by the life of the Thread and by the soft
* references. The implication here is you should NEVER use dynamic properties if you want their values to stick around
* for long periods because as soon as the JVM is running low on memory or the thread dies they will be garbage collected.
*
* @author Graeme Rocher
* @since 1.1
*/
public class ExpandoMetaClass extends MetaClassImpl implements GroovyObject {
private static final String META_CLASS = "metaClass";
private static final String CLASS = "class";
private static final String META_METHODS = "metaMethods";
private static final String METHODS = "methods";
private static final String PROPERTIES = "properties";
public static final String STATIC_QUALIFIER = "static";
private static final Class[] ZERO_ARGUMENTS = new Class[0];
private static final String CONSTRUCTOR = "constructor";
private static final String GET_PROPERTY_METHOD = "getProperty";
private static final String SET_PROPERTY_METHOD = "setProperty";
private static final String INVOKE_METHOD_METHOD = "invokeMethod";
private static final String CLASS_PROPERTY = "class";
private static final String META_CLASS_PROPERTY = "metaClass";
private static final String GROOVY_CONSTRUCTOR = "