
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-jdk14 Show documentation
Show all versions of groovy-jdk14 Show documentation
Groovy: A powerful, dynamic language for the JVM
The newest version!
/*
* Copyright 2003-2009 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.MixinInMetaClass;
import org.codehaus.groovy.runtime.DefaultCachedMethodKey;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.runtime.MethodKey;
import org.codehaus.groovy.runtime.callsite.CallSite;
import org.codehaus.groovy.runtime.callsite.ConstructorMetaMethodSite;
import org.codehaus.groovy.runtime.callsite.PogoMetaClassSite;
import org.codehaus.groovy.runtime.callsite.PojoMetaClassSite;
import org.codehaus.groovy.runtime.callsite.StaticMetaClassSite;
import org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod;
import org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod;
import org.codehaus.groovy.runtime.metaclass.MetaMethodIndex;
import org.codehaus.groovy.runtime.metaclass.MixedInMetaClass;
import org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod;
import org.codehaus.groovy.runtime.metaclass.OwnedMetaClass;
import org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty;
import org.codehaus.groovy.util.FastArray;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* A MetaClass that implements GroovyObject and behaves like an Expando, allowing the addition of new methods on the fly.
*
* Some examples of usage:
*
* // 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.5
*/
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";
public static final String CONSTRUCTOR = "constructor";
private static final String CLASS_PROPERTY = "class";
private static final String META_CLASS_PROPERTY = "metaClass";
private static final String GROOVY_CONSTRUCTOR = "© 2015 - 2025 Weber Informatics LLC | Privacy Policy