use of groovy.lang.MetaMethod in project spock by spockframework.
the class AddSlotFactory method create.
public ISlot create(Object owner, Type ownerType, String name) {
String addMethodName = GroovyRuntimeUtil.propertyToMethodName("add", name);
MetaMethod addMethod = GroovyRuntimeUtil.getMetaClass(owner).pickMethod(addMethodName, new Class[] { Object.class });
return addMethod != null ? new SetterLikeSlot(owner, ownerType, addMethod) : null;
}
use of groovy.lang.MetaMethod in project grails-core by grails.
the class GrailsMetaClassUtils method invokeMethodIfExists.
/**
* Invokes a method if it exists otherwise returns null
*
* @param instance The instance
* @param methodName The method name
* @param args The arguments
*
* @return The result of the method call or null
*/
public static Object invokeMethodIfExists(Object instance, String methodName, Object[] args) {
MetaClass metaClass = getMetaClass(instance);
List<MetaMethod> methodList = metaClass.respondsTo(instance, methodName, args);
if (methodList != null && !methodList.isEmpty()) {
return metaClass.invokeMethod(instance, methodName, args);
}
return null;
}
use of groovy.lang.MetaMethod in project grails-core by grails.
the class DefaultGrailsApplication method initialiseGroovyExtensionModules.
protected static void initialiseGroovyExtensionModules() {
if (extensionMethodsInitialized)
return;
extensionMethodsInitialized = true;
Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Enumeration<URL> resources = classLoader.getResources(ExtensionModuleScanner.MODULE_META_INF_FILE);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url.getPath().contains("groovy-all")) {
// already registered
continue;
}
Properties properties = new Properties();
InputStream inStream = null;
try {
inStream = url.openStream();
properties.load(inStream);
((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).registerExtensionModuleFromProperties(properties, classLoader, map);
} catch (IOException e) {
throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e);
} finally {
if (inStream != null) {
inStream.close();
}
}
}
} catch (IOException ignored) {
}
for (Map.Entry<CachedClass, List<MetaMethod>> moduleMethods : map.entrySet()) {
CachedClass cls = moduleMethods.getKey();
cls.addNewMopMethods(moduleMethods.getValue());
}
}
use of groovy.lang.MetaMethod in project groovy-core by groovy.
the class ClosureMetaMethod method createMethodList.
public static List<MetaMethod> createMethodList(final String name, final Class declaringClass, final Closure closure) {
List<MetaMethod> res = new ArrayList<MetaMethod>();
if (closure instanceof MethodClosure) {
MethodClosure methodClosure = (MethodClosure) closure;
Object owner = closure.getOwner();
Class ownerClass = (Class) (owner instanceof Class ? owner : owner.getClass());
for (CachedMethod method : ReflectionCache.getCachedClass(ownerClass).getMethods()) {
if (method.getName().equals(methodClosure.getMethod())) {
MetaMethod metaMethod = new MethodClosureMetaMethod(name, declaringClass, closure, method);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
} else {
if (closure instanceof GeneratedClosure) {
for (CachedMethod method : ReflectionCache.getCachedClass(closure.getClass()).getMethods()) {
if (method.getName().equals("doCall")) {
MetaMethod metaMethod = new ClosureMetaMethod(name, declaringClass, closure, method);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
} else {
MetaMethod metaMethod = new AnonymousMetaMethod(closure, name, declaringClass);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
return res;
}
use of groovy.lang.MetaMethod in project groovy-core by groovy.
the class MetaMethodIndex method findMatchingMethod.
private int findMatchingMethod(FastArray list, MetaMethod method) {
int len = list.size();
Object[] data = list.getArray();
for (int j = 0; j != len; ++j) {
MetaMethod aMethod = (MetaMethod) data[j];
if (isMatchingMethod(aMethod, method))
return j;
}
return -1;
}
Aggregations