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();
}
}
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;
}
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);
}
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 });
}
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());
}
Aggregations