use of org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod in project grails-core by grails.
the class BaseApiProvider method addApi.
@SuppressWarnings("unchecked")
public void addApi(final Object apiInstance) {
if (apiInstance == null) {
return;
}
Class<?> currentClass = apiInstance.getClass();
while (currentClass != Object.class) {
final Method[] declaredMethods = currentClass.getDeclaredMethods();
for (final Method javaMethod : declaredMethods) {
final int modifiers = javaMethod.getModifiers();
if (!isNotExcluded(javaMethod, modifiers)) {
continue;
}
if (Modifier.isStatic(modifiers)) {
if (isConstructorCallMethod(javaMethod)) {
constructors.add(javaMethod);
} else {
staticMethods.add(javaMethod);
}
} else {
instanceMethods.add(new ReflectionMetaMethod(new CachedMethod(javaMethod)) {
{
CachedClass[] paramTypes = super.getParameterTypes();
if (paramTypes.length > 0) {
setParametersTypes((CachedClass[]) GrailsArrayUtils.subarray(paramTypes, 1, paramTypes.length));
}
}
@Override
public String getName() {
String methodName = super.getName();
if (isConstructorCallMethod(javaMethod)) {
return CTOR_GROOVY_METHOD;
}
return methodName;
}
@Override
public Object invoke(Object object, Object[] arguments) {
if (arguments.length == 0) {
return super.invoke(apiInstance, new Object[] { object });
}
return super.invoke(apiInstance, (Object[]) GrailsArrayUtils.add(checkForGStrings(arguments), 0, object));
}
private Object[] checkForGStrings(Object[] arguments) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof GString) {
arguments[i] = arguments[i].toString();
}
}
return arguments;
}
@Override
public CachedClass[] getParameterTypes() {
final CachedClass[] paramTypes = method.getParameterTypes();
if (paramTypes.length > 0) {
return (CachedClass[]) GrailsArrayUtils.subarray(paramTypes, 1, paramTypes.length);
}
return paramTypes;
}
});
}
}
currentClass = currentClass.getSuperclass();
}
}
Aggregations