Search in sources :

Example 1 with ReflectionMetaMethod

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();
    }
}
Also used : CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) ReflectionMetaMethod(org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) GString(groovy.lang.GString) GString(groovy.lang.GString) ReflectionMetaMethod(org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod)

Aggregations

GString (groovy.lang.GString)1 Method (java.lang.reflect.Method)1 CachedClass (org.codehaus.groovy.reflection.CachedClass)1 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)1 ReflectionMetaMethod (org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod)1