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