Search in sources :

Example 1 with MetaClass

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

the class NioGroovyMethods method notFiltered.

private static boolean notFiltered(Path path, 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 = path;
    } else if (nameFilter != null) {
        filterToUse = nameFilter;
        filterParam = path.getFileName().toString();
    }
    Object excludeFilterToUse = null;
    Object excludeParam = null;
    if (excludeFilter != null) {
        excludeFilterToUse = excludeFilter;
        excludeParam = path;
    } else if (excludeNameFilter != null) {
        excludeFilterToUse = excludeNameFilter;
        excludeParam = path.getFileName().toString();
    }
    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 2 with MetaClass

use of groovy.lang.MetaClass in project spock by spockframework.

the class ConfineMetaClassChangesInterceptor method intercept.

public void intercept(IMethodInvocation invocation) throws Throwable {
    MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    for (Class<?> clazz : classes) {
        originalMetaClasses.add(registry.getMetaClass(clazz));
        MetaClass temporaryMetaClass = new ExpandoMetaClass(clazz, true, true);
        temporaryMetaClass.initialize();
        registry.setMetaClass(clazz, temporaryMetaClass);
    }
    try {
        invocation.proceed();
    } finally {
        for (MetaClass original : originalMetaClasses) registry.setMetaClass(original.getTheClass(), original);
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 3 with MetaClass

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

the class ProxyUnwrappingMarshaller method unwrap.

private Object unwrap(Object o) {
    if (o == null)
        return o;
    final MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    MetaClass mc = registry.getMetaClass(o.getClass());
    final Object hibernateLazyInitializer = mc.getProperty(o, HIBERNATE_LAZY_INITIALIZER_PROP);
    return registry.getMetaClass(hibernateLazyInitializer.getClass()).getProperty(hibernateLazyInitializer, IMPLEMENTATION_PROP);
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass)

Example 4 with MetaClass

use of groovy.lang.MetaClass in project spring-framework by spring-projects.

the class GroovyBeanDefinitionReader method invokeMethod.

// INTERNAL HANDLING OF GROOVY CLOSURES AND PROPERTIES
/**
	 * This method overrides method invocation to create beans for each method name that
	 * takes a class argument.
	 */
public Object invokeMethod(String name, Object arg) {
    Object[] args = (Object[]) arg;
    if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
        return beans((Closure) args[0]);
    } else if ("ref".equals(name)) {
        String refName;
        if (args[0] == null)
            throw new IllegalArgumentException("Argument to ref() is not a valid bean or was not found");
        if (args[0] instanceof RuntimeBeanReference) {
            refName = ((RuntimeBeanReference) args[0]).getBeanName();
        } else {
            refName = args[0].toString();
        }
        boolean parentRef = false;
        if (args.length > 1) {
            if (args[1] instanceof Boolean) {
                parentRef = (Boolean) args[1];
            }
        }
        return new RuntimeBeanReference(refName, parentRef);
    } else if (this.namespaces.containsKey(name) && args.length > 0 && args[0] instanceof Closure) {
        GroovyDynamicElementReader reader = createDynamicElementReader(name);
        reader.invokeMethod("doCall", args);
    } else if (args.length > 0 && args[0] instanceof Closure) {
        // abstract bean definition
        return invokeBeanDefiningMethod(name, args);
    } else if (args.length > 0 && (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map)) {
        return invokeBeanDefiningMethod(name, args);
    } else if (args.length > 1 && args[args.length - 1] instanceof Closure) {
        return invokeBeanDefiningMethod(name, args);
    }
    MetaClass mc = DefaultGroovyMethods.getMetaClass(getRegistry());
    if (!mc.respondsTo(getRegistry(), name, args).isEmpty()) {
        return mc.invokeMethod(getRegistry(), name, args);
    }
    return this;
}
Also used : Closure(groovy.lang.Closure) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) GString(groovy.lang.GString) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 5 with MetaClass

use of groovy.lang.MetaClass in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecutionMemoryTest method loaderReleased.

@Test
public void loaderReleased() throws Exception {
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    r.jenkins.getWorkspaceFor(p).child("lib.groovy").write(CpsFlowExecutionMemoryTest.class.getName() + ".register(this)", null);
    p.setDefinition(new CpsFlowDefinition(CpsFlowExecutionMemoryTest.class.getName() + ".register(this); node {load 'lib.groovy'; evaluate(readFile('lib.groovy'))}", false));
    WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
    assertFalse(((CpsFlowExecution) b.getExecution()).getProgramDataFile().exists());
    assertFalse(LOADERS.isEmpty());
    {
        // TODO it seems that the call to CpsFlowExecutionMemoryTest.register(Object) on a Script1 parameter creates a MetaMethodIndex.Entry.cachedStaticMethod.
        // In other words any call to a foundational API might leak classes. Why does Groovy need to do this?
        // Unclear whether this is a problem in a realistic environment; for the moment, suppressing it so the test can run with no SoftReference.
        MetaClass metaClass = ClassInfo.getClassInfo(CpsFlowExecutionMemoryTest.class).getMetaClass();
        Method clearInvocationCaches = metaClass.getClass().getDeclaredMethod("clearInvocationCaches");
        clearInvocationCaches.setAccessible(true);
        clearInvocationCaches.invoke(metaClass);
    }
    for (WeakReference<ClassLoader> loaderRef : LOADERS) {
        MemoryAssert.assertGC(loaderRef, false);
    }
}
Also used : MetaClass(groovy.lang.MetaClass) Method(java.lang.reflect.Method) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

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