use of org.codehaus.groovy.runtime.MethodKey in project groovy-core by groovy.
the class ExpandoMetaClass method registerStaticMethod.
/**
* Registers a new static method for the given method name and closure on this MetaClass
*
* @param name The method name
* @param callable The callable Closure
*/
protected void registerStaticMethod(final String name, final Closure callable, final Class[] paramTypes) {
performOperationOnMetaClass(new Callable() {
public void call() {
String methodName;
if (name.equals(METHOD_MISSING))
methodName = STATIC_METHOD_MISSING;
else if (name.equals(PROPERTY_MISSING))
methodName = STATIC_PROPERTY_MISSING;
else
methodName = name;
ClosureStaticMetaMethod metaMethod = null;
if (paramTypes != null) {
metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable, paramTypes);
} else {
metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable);
}
if (methodName.equals(INVOKE_METHOD_METHOD) && callable.getParameterTypes().length == 2) {
invokeStaticMethodMethod = metaMethod;
} else {
if (methodName.equals(METHOD_MISSING)) {
methodName = STATIC_METHOD_MISSING;
}
MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
addMetaMethod(metaMethod);
dropStaticMethodCache(methodName);
if (isGetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForGetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, true, true);
} else if (isSetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForSetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, false, true);
}
performRegistryCallbacks();
expandoMethods.put(key, metaMethod);
}
}
});
}
use of org.codehaus.groovy.runtime.MethodKey in project groovy-core by groovy.
the class ExpandoMetaClass method addSuperMethodIfNotOverridden.
private void addSuperMethodIfNotOverridden(final MetaMethod metaMethodFromSuper) {
performOperationOnMetaClass(new Callable() {
public void call() {
MetaMethod existing = null;
try {
existing = pickMethod(metaMethodFromSuper.getName(), metaMethodFromSuper.getNativeParameterTypes());
} catch (GroovyRuntimeException e) {
// ignore, this happens with overlapping method definitions
}
if (existing == null) {
addMethodWithKey(metaMethodFromSuper);
} else {
boolean isGroovyMethod = getMetaMethods().contains(existing);
if (isGroovyMethod) {
addMethodWithKey(metaMethodFromSuper);
} else if (inheritedMetaMethods.contains(existing)) {
inheritedMetaMethods.remove(existing);
addMethodWithKey(metaMethodFromSuper);
}
}
}
private void addMethodWithKey(final MetaMethod metaMethodFromSuper) {
inheritedMetaMethods.add(metaMethodFromSuper);
if (metaMethodFromSuper instanceof ClosureMetaMethod) {
ClosureMetaMethod closureMethod = (ClosureMetaMethod) metaMethodFromSuper;
String name = metaMethodFromSuper.getName();
final Class declaringClass = metaMethodFromSuper.getDeclaringClass().getTheClass();
ClosureMetaMethod localMethod = ClosureMetaMethod.copy(closureMethod);
addMetaMethod(localMethod);
MethodKey key = new DefaultCachedMethodKey(declaringClass, name, localMethod.getParameterTypes(), false);
checkIfGroovyObjectMethod(localMethod);
expandoMethods.put(key, localMethod);
}
}
});
}
use of org.codehaus.groovy.runtime.MethodKey in project groovy-core by groovy.
the class ExpandoMetaClass method registerInstanceMethod.
/**
* Registers a new instance method for the given method name and closure on this MetaClass
*
* @param metaMethod
*/
public void registerInstanceMethod(final MetaMethod metaMethod) {
final boolean inited = this.initCalled;
performOperationOnMetaClass(new Callable() {
public void call() {
String methodName = metaMethod.getName();
checkIfGroovyObjectMethod(metaMethod);
MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
if (isInitialized()) {
throw new RuntimeException("Already initialized, cannot add new method: " + metaMethod);
}
// we always adds meta methods to class itself
addMetaMethodToIndex(metaMethod, metaMethodIndex.getHeader(theClass));
dropMethodCache(methodName);
expandoMethods.put(key, metaMethod);
if (inited && isGetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForGetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, true, false);
} else if (inited && isSetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForSetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, false, false);
}
performRegistryCallbacks();
}
});
}
use of org.codehaus.groovy.runtime.MethodKey in project groovy by apache.
the class ExpandoMetaClass method addSuperMethodIfNotOverridden.
private void addSuperMethodIfNotOverridden(final MetaMethod metaMethodFromSuper) {
performOperationOnMetaClass(new Callable() {
public void call() {
MetaMethod existing = null;
try {
existing = pickMethod(metaMethodFromSuper.getName(), metaMethodFromSuper.getNativeParameterTypes());
} catch (GroovyRuntimeException e) {
// ignore, this happens with overlapping method definitions
}
if (existing == null) {
addMethodWithKey(metaMethodFromSuper);
} else {
boolean isGroovyMethod = getMetaMethods().contains(existing);
if (isGroovyMethod) {
addMethodWithKey(metaMethodFromSuper);
} else if (inheritedMetaMethods.contains(existing)) {
inheritedMetaMethods.remove(existing);
addMethodWithKey(metaMethodFromSuper);
}
}
}
private void addMethodWithKey(final MetaMethod metaMethodFromSuper) {
inheritedMetaMethods.add(metaMethodFromSuper);
if (metaMethodFromSuper instanceof ClosureMetaMethod) {
ClosureMetaMethod closureMethod = (ClosureMetaMethod) metaMethodFromSuper;
String name = metaMethodFromSuper.getName();
final Class declaringClass = metaMethodFromSuper.getDeclaringClass().getTheClass();
ClosureMetaMethod localMethod = ClosureMetaMethod.copy(closureMethod);
addMetaMethod(localMethod);
MethodKey key = new DefaultCachedMethodKey(declaringClass, name, localMethod.getParameterTypes(), false);
checkIfGroovyObjectMethod(localMethod);
expandoMethods.put(key, localMethod);
}
}
});
}
use of org.codehaus.groovy.runtime.MethodKey in project groovy by apache.
the class ExpandoMetaClass method registerStaticMethod.
/**
* Registers a new static method for the given method name and closure on this MetaClass
*
* @param name The method name
* @param callable The callable Closure
*/
protected void registerStaticMethod(final String name, final Closure callable, final Class[] paramTypes) {
performOperationOnMetaClass(new Callable() {
public void call() {
String methodName;
if (name.equals(METHOD_MISSING))
methodName = STATIC_METHOD_MISSING;
else if (name.equals(PROPERTY_MISSING))
methodName = STATIC_PROPERTY_MISSING;
else
methodName = name;
ClosureStaticMetaMethod metaMethod = null;
if (paramTypes != null) {
metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable, paramTypes);
} else {
metaMethod = new ClosureStaticMetaMethod(methodName, theClass, callable);
}
if (methodName.equals(INVOKE_METHOD_METHOD) && callable.getParameterTypes().length == 2) {
invokeStaticMethodMethod = metaMethod;
} else {
if (methodName.equals(METHOD_MISSING)) {
methodName = STATIC_METHOD_MISSING;
}
MethodKey key = new DefaultCachedMethodKey(theClass, methodName, metaMethod.getParameterTypes(), false);
addMetaMethod(metaMethod);
dropStaticMethodCache(methodName);
if (isGetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForGetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, true, true);
} else if (isSetter(methodName, metaMethod.getParameterTypes())) {
String propertyName = getPropertyForSetter(methodName);
registerBeanPropertyForMethod(metaMethod, propertyName, false, true);
}
performRegistryCallbacks();
expandoMethods.put(key, metaMethod);
}
}
});
}
Aggregations