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