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();
}
}
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;
}
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;
}
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);
}
}
}
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;
}
Aggregations