Search in sources :

Example 26 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class ClassInfo method getMetaClass.

/**
 * Returns the {@code MetaClass} for the {@code Class} associated with this {@code ClassInfo}.
 * If no {@code MetaClass} exists one will be created.
 * <p>
 * It is not safe to call this method without a {@code Class} associated with this {@code ClassInfo}.
 * It is advisable to aways retrieve a ClassInfo instance from the cache by using the static
 * factory method {@link ClassInfo#getClassInfo(Class)} to ensure the referenced Class is
 * strongly reachable.
 *
 * @return a {@code MetaClass} instance
 */
public final MetaClass getMetaClass() {
    MetaClass answer = getMetaClassForClass();
    if (answer != null)
        return answer;
    lock();
    try {
        return getMetaClassUnderLock();
    } finally {
        unlock();
    }
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass)

Example 27 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class ClassInfo method getMetaClassForClass.

public MetaClass getMetaClassForClass() {
    // safe value here to avoid multiple reads with possibly
    // differing values due to concurrency
    MetaClass strongMc = strongMetaClass;
    if (strongMc != null)
        return strongMc;
    MetaClass weakMc = getWeakMetaClass();
    if (isValidWeakMetaClass(weakMc)) {
        return weakMc;
    }
    return null;
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass)

Example 28 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class Inspector method getMetaMethods.

/**
 * Get info about instance and class Methods that are dynamically added through Groovy.
 *
 * @return Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
 */
public Object[] getMetaMethods() {
    MetaClass metaClass = InvokerHelper.getMetaClass(objectUnderInspection);
    List metaMethods = metaClass.getMetaMethods();
    Object[] result = new Object[metaMethods.size()];
    int i = 0;
    for (Iterator iter = metaMethods.iterator(); iter.hasNext(); i++) {
        MetaMethod metaMethod = (MetaMethod) iter.next();
        result[i] = methodInfo(metaMethod);
    }
    return result;
}
Also used : MetaMethod(groovy.lang.MetaMethod) MetaClass(groovy.lang.MetaClass) Iterator(java.util.Iterator) List(java.util.List) GroovyObject(groovy.lang.GroovyObject)

Example 29 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class FactoryInterceptorMetaClass method build.

public Object build(Script script) {
    // this used to be synchronized, but we also used to remove the
    // metaclass.  Since adding the metaclass is now a side effect, we
    // don't need to ensure the meta-class won't be observed and don't
    // need to hide the side effect.
    MetaClass scriptMetaClass = script.getMetaClass();
    script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this));
    script.setBinding(this);
    Object oldScriptName = getProxyBuilder().getVariables().get(SCRIPT_CLASS_NAME);
    try {
        getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, script.getClass().getName());
        return script.run();
    } finally {
        if (oldScriptName != null) {
            getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, oldScriptName);
        } else {
            getProxyBuilder().getVariables().remove(SCRIPT_CLASS_NAME);
        }
    }
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Example 30 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class DefaultGroovyMethods method average.

/**
 * Averages the items in an array. This is equivalent to invoking the
 * "plus" method on all items in the array and then dividing by the
 * total count using the "div" method for the resulting sum.
 * <pre class="groovyTestCase">
 * assert 3 == ([1, 2, 6] as Integer[]).average()
 * </pre>
 *
 * @param self The array of values to average
 * @return The average of all of the items
 * @see #sum(java.lang.Object[])
 * @since 3.0.0
 */
public static Object average(Object[] self) {
    Object result = sum(self);
    MetaClass metaClass = InvokerHelper.getMetaClass(result);
    result = metaClass.invokeMethod(result, "div", self.length);
    return result;
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Aggregations

MetaClass (groovy.lang.MetaClass)141 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)62 GroovyObject (groovy.lang.GroovyObject)62 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)40 MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaProperty (groovy.lang.MetaProperty)12 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)11 MetaMethod (groovy.lang.MetaMethod)11 ArrayList (java.util.ArrayList)11 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)11 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 MetaClassRegistry (groovy.lang.MetaClassRegistry)9 Map (java.util.Map)9 GString (groovy.lang.GString)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)7 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)7 MissingMethodException (groovy.lang.MissingMethodException)6 MissingPropertyException (groovy.lang.MissingPropertyException)6 IOException (java.io.IOException)6 Method (java.lang.reflect.Method)6