Search in sources :

Example 16 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class ResourceGroovyMethods method notFiltered.

private static boolean notFiltered(File file, Object filter, Object nameFilter, Object excludeFilter, Object excludeNameFilter) {
    if (filter == null && nameFilter == null && excludeFilter == null && excludeNameFilter == null)
        return true;
    if (filter != null && nameFilter != null)
        throw new IllegalArgumentException("Can't set both 'filter' and 'nameFilter'");
    if (excludeFilter != null && excludeNameFilter != null)
        throw new IllegalArgumentException("Can't set both 'excludeFilter' and 'excludeNameFilter'");
    Object filterToUse = null;
    Object filterParam = null;
    if (filter != null) {
        filterToUse = filter;
        filterParam = file;
    } else if (nameFilter != null) {
        filterToUse = nameFilter;
        filterParam = file.getName();
    }
    Object excludeFilterToUse = null;
    Object excludeParam = null;
    if (excludeFilter != null) {
        excludeFilterToUse = excludeFilter;
        excludeParam = file;
    } else if (excludeNameFilter != null) {
        excludeFilterToUse = excludeNameFilter;
        excludeParam = file.getName();
    }
    final MetaClass filterMC = filterToUse == null ? null : InvokerHelper.getMetaClass(filterToUse);
    final MetaClass excludeMC = excludeFilterToUse == null ? null : InvokerHelper.getMetaClass(excludeFilterToUse);
    boolean included = filterToUse == null || DefaultTypeTransformation.castToBoolean(filterMC.invokeMethod(filterToUse, "isCase", filterParam));
    boolean excluded = excludeFilterToUse != null && DefaultTypeTransformation.castToBoolean(excludeMC.invokeMethod(excludeFilterToUse, "isCase", excludeParam));
    return included && !excluded;
}
Also used : MetaClass(groovy.lang.MetaClass)

Example 17 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class CallSiteArray method createPojoSite.

// for MetaClassImpl we try to pick meta method,
// otherwise or if method doesn't exist we make call via POJO meta class
private static CallSite createPojoSite(CallSite callSite, Object receiver, Object[] args) {
    final Class klazz = receiver.getClass();
    MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
    if (!GroovyCategorySupport.hasCategoryInCurrentThread() && metaClass instanceof MetaClassImpl) {
        final MetaClassImpl mci = (MetaClassImpl) metaClass;
        final ClassInfo info = mci.getTheCachedClass().classInfo;
        if (info.hasPerInstanceMetaClasses()) {
            return new PerInstancePojoMetaClassSite(callSite, info);
        } else {
            return mci.createPojoCallSite(callSite, receiver, args);
        }
    }
    ClassInfo info = ClassInfo.getClassInfo(klazz);
    if (info.hasPerInstanceMetaClasses())
        return new PerInstancePojoMetaClassSite(callSite, info);
    else
        return new PojoMetaClassSite(callSite, metaClass);
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClass(groovy.lang.MetaClass) MetaClassImpl(groovy.lang.MetaClassImpl) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 18 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class CallSiteArray method createCallStaticSite.

private static CallSite createCallStaticSite(CallSite callSite, final Class receiver, Object[] args) {
    CallSite site;
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        public Void run() {
            try {
                Class.forName(receiver.getName(), true, receiver.getClassLoader());
            } catch (Exception e) {
            // force <clinit>
            }
            return null;
        }
    });
    MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
    if (metaClass instanceof MetaClassImpl) {
        site = ((MetaClassImpl) metaClass).createStaticSite(callSite, args);
    } else
        site = new StaticMetaClassSite(callSite, metaClass);
    replaceCallSite(callSite, site);
    return site;
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 19 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class CallSiteArray method createCallCurrentSite.

private static CallSite createCallCurrentSite(CallSite callSite, GroovyObject receiver, Object[] args, Class sender) {
    CallSite site;
    if (receiver instanceof GroovyInterceptable)
        site = new PogoInterceptableSite(callSite);
    else {
        MetaClass metaClass = receiver.getMetaClass();
        if (receiver.getClass() != metaClass.getTheClass() && !metaClass.getTheClass().isInterface()) {
            site = new PogoInterceptableSite(callSite);
        } else if (metaClass instanceof MetaClassImpl) {
            site = ((MetaClassImpl) metaClass).createPogoCallCurrentSite(callSite, sender, args);
        } else
            site = new PogoMetaClassSite(callSite, metaClass);
    }
    replaceCallSite(callSite, site);
    return site;
}
Also used : MetaClass(groovy.lang.MetaClass) GroovyInterceptable(groovy.lang.GroovyInterceptable) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 20 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

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)

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