Search in sources :

Example 66 with MetaClass

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

the class GrailsFlashScope method reassociateObjectWithErrors.

private void reassociateObjectWithErrors(Map scope, Object value) {
    if (value instanceof Collection) {
        Collection values = (Collection) value;
        for (Object val : values) {
            reassociateObjectWithErrors(scope, val);
        }
    } else {
        String errorsKey = ERRORS_PREFIX + System.identityHashCode(value);
        Object errors = scope.get(errorsKey);
        if (value != null && errors != null) {
            MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(value.getClass());
            if (mc.hasProperty(value, ERRORS_PROPERTY) != null) {
                mc.setProperty(value, ERRORS_PROPERTY, errors);
            }
        }
    }
}
Also used : MetaClass(groovy.lang.MetaClass) Collection(java.util.Collection)

Example 67 with MetaClass

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

the class MetaClassChangeReporter method updateConstantMetaClass.

/**
 * Called when the a constant MetaClass is updated. If the new MetaClass is null, then the MetaClass
 * is removed. Be careful, while this method is executed other updates may happen. If you want this
 * method thread safe, you have to take care of that by yourself.
 *
 * @param cmcu - the change event
 */
public void updateConstantMetaClass(MetaClassRegistryChangeEvent cmcu) {
    Class<?> classToUpdate = cmcu.getClassToUpdate();
    MetaClass newMetaClass = cmcu.getNewMetaClass();
    System.out.println("Class [" + classToUpdate + "] updated MetaClass to [" + newMetaClass + "]");
    Thread.dumpStack();
}
Also used : MetaClass(groovy.lang.MetaClass)

Example 68 with MetaClass

use of groovy.lang.MetaClass in project hale by halestudio.

the class CustomMetaClassCreationHandle method createNormalMetaClass.

@Override
protected MetaClass createNormalMetaClass(@SuppressWarnings("rawtypes") Class theClass, MetaClassRegistry registry) {
    MetaClass metaClass = super.createNormalMetaClass(theClass, registry);
    for (MetaClassDescriptor descriptor : ext.getElements()) {
        if (descriptorApplies(descriptor, theClass)) {
            // create meta class
            Class<?> delegatingMetaClass = descriptor.getMetaClass();
            try {
                Constructor<?> constructor = delegatingMetaClass.getConstructor(MetaClass.class);
                metaClass = (MetaClass) constructor.newInstance(metaClass);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return metaClass;
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClassDescriptor(eu.esdihumboldt.util.groovy.meta.extension.MetaClassDescriptor)

Example 69 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)

Example 70 with MetaClass

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

the class DataBindingUtils method assignBidirectionalAssociations.

/**
     * Associations both sides of any bidirectional relationships found in the object and source map to bind
     *
     * @param object The object
     * @param source The source map
     * @param domainClass The DomainClass for the object
     */
public static void assignBidirectionalAssociations(Object object, Map source, GrailsDomainClass domainClass) {
    if (source == null) {
        return;
    }
    for (Object key : source.keySet()) {
        String propertyName = key.toString();
        if (propertyName.indexOf('.') > -1) {
            propertyName = propertyName.substring(0, propertyName.indexOf('.'));
        }
        if (domainClass.hasPersistentProperty(propertyName)) {
            GrailsDomainClassProperty prop = domainClass.getPropertyByName(propertyName);
            if (prop != null && prop.isOneToOne() && prop.isBidirectional()) {
                Object val = source.get(key);
                GrailsDomainClassProperty otherSide = prop.getOtherSide();
                if (val != null && otherSide != null) {
                    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(val.getClass());
                    try {
                        mc.setProperty(val, otherSide.getName(), object);
                    } catch (Exception e) {
                    // ignore
                    }
                }
            }
        }
    }
}
Also used : MetaClass(groovy.lang.MetaClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) InvalidRequestBodyException(org.grails.web.databinding.bindingsource.InvalidRequestBodyException)

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