Search in sources :

Example 16 with GString

use of groovy.lang.GString 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)

Example 17 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class ObjectArrayPutAtMetaMethod method adjustNewValue.

private static Object adjustNewValue(Object[] objects, Object newValue) {
    Class arrayComponentClass = objects.getClass().getComponentType();
    Object adjustedNewVal = newValue;
    if (newValue instanceof Number) {
        if (!arrayComponentClass.equals(newValue.getClass())) {
            adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
        }
    } else if (Character.class.isAssignableFrom(arrayComponentClass)) {
        adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
    } else if (Number.class.isAssignableFrom(arrayComponentClass)) {
        if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) {
            Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
            adjustedNewVal = DefaultTypeTransformation.castToType(ch, arrayComponentClass);
        }
    } else if (arrayComponentClass.isArray()) {
        adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
    }
    return adjustedNewVal;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) GString(groovy.lang.GString) GString(groovy.lang.GString)

Example 18 with GString

use of groovy.lang.GString in project grails-core by grails.

the class BeanBuilder method setPropertyOnBeanConfig.

protected void setPropertyOnBeanConfig(String name, Object value) {
    if (value instanceof GString) {
        value = value.toString();
    }
    if (addToDeferred(currentBeanConfig, name, value)) {
        return;
    }
    if (value instanceof Closure) {
        BeanConfiguration current = currentBeanConfig;
        try {
            Closure<?> callable = (Closure<?>) value;
            Class<?> parameterType = callable.getParameterTypes()[0];
            if (parameterType.equals(Object.class)) {
                currentBeanConfig = springConfig.createSingletonBean("");
                callable.call(new Object[] { currentBeanConfig });
            } else {
                currentBeanConfig = springConfig.createSingletonBean(parameterType);
                callable.call();
            }
            value = currentBeanConfig.getBeanDefinition();
        } finally {
            currentBeanConfig = current;
        }
    }
    currentBeanConfig.addProperty(name, value);
}
Also used : Closure(groovy.lang.Closure) GString(groovy.lang.GString) DefaultBeanConfiguration(org.grails.spring.DefaultBeanConfiguration) BeanConfiguration(org.grails.spring.BeanConfiguration)

Example 19 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class InvokeConstructorTest method testConstructorWithGStringCoercion.

public void testConstructorWithGStringCoercion() throws Throwable {
    GString gstring = new GString(new Object[] { new Integer(123) }) {

        public String[] getStrings() {
            return new String[] { "" };
        }
    };
    Object expected = new Long(gstring.toString());
    assertConstructor(expected, new Object[] { gstring });
}
Also used : GString(groovy.lang.GString) GString(groovy.lang.GString)

Example 20 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class InvokeMethodTest method testCoerceGStringToStringOnGetBytes.

public void testCoerceGStringToStringOnGetBytes() throws Throwable {
    GString param = new GString(new Object[] { "US-ASCII" }) {

        public String[] getStrings() {
            return new String[] { "" };
        }
    };
    Object value = invoke("test", "getBytes", new Object[] { param });
    assertEquals("converted GString to string", "test".getBytes("US-ASCII").getClass(), value.getClass());
}
Also used : GString(groovy.lang.GString) GString(groovy.lang.GString)

Aggregations

GString (groovy.lang.GString)52 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 Closure (groovy.lang.Closure)3 IOException (java.io.IOException)3 Map (java.util.Map)3 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 NullObject (org.codehaus.groovy.runtime.NullObject)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)1 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)1