use of groovy.lang.GString in project spring-integration by spring-projects.
the class GroovyCommandMessageProcessor method executeScript.
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator();
if (this.customizer != null) {
customizerDecorator.setCustomizer(this.customizer);
}
if (!CollectionUtils.isEmpty(variables)) {
customizerDecorator.setVariables(variables);
}
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator);
if (this.beanClassLoader != null) {
factory.setBeanClassLoader(this.beanClassLoader);
}
if (this.beanFactory != null) {
factory.setBeanFactory(this.beanFactory);
}
Object result = factory.getScriptedObject(scriptSource);
return (result instanceof GString) ? result.toString() : result;
}
use of groovy.lang.GString in project spring-integration by spring-projects.
the class GroovyScriptExecutingMessageProcessor method executeScript.
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
Assert.notNull(scriptSource, "scriptSource must not be null");
this.parseScriptIfNecessary(scriptSource);
Object result = this.execute(variables);
return (result instanceof GString) ? result.toString() : result;
}
use of groovy.lang.GString in project rest-assured by rest-assured.
the class EncoderRegistry method encodeJSON.
/**
* <p>Accepts a Collection or a JavaBean object which is converted to JSON.
* A Map or Collection will be converted to a {@link JsonBuilder}.. A
* String or GString will be interpreted as valid JSON and passed directly
* as the request body (with charset conversion if necessary.)</p>
* <p/>
* <p>If a Closure is passed as the model, it will be executed as if it were
* a JSON object definition passed to a {@link JsonBuilder}. In order
* for the closure to be interpreted correctly, there must be a 'root'
* element immediately inside the closure. For example:</p>
* <p/>
* <pre>builder.post( JSON ) {
* body = {
* root {
* first {
* one = 1
* two = '2'
* }
* second = 'some string'
* }
* }
* }</pre>
* <p> will return the following JSON string:<pre>
* {"root":{"first":{"one":1,"two":"2"},"second":"some string"}}</pre></p>
*
* @param model data to be converted to JSON, as specified above.
* @return an {@link HttpEntity} encapsulating this request data
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unchecked")
public HttpEntity encodeJSON(Object contentType, Object model) throws UnsupportedEncodingException {
String contentTypeAsString = contentTypeToString(contentType);
Object json;
if (model instanceof Map || model instanceof Collection) {
json = new JsonBuilder(model);
} else if (model instanceof Closure) {
Closure closure = (Closure) model;
closure.setDelegate(new JsonBuilder());
json = closure.call();
} else if (model instanceof String || model instanceof GString || model instanceof byte[]) {
// assume valid JSON already.
json = model;
} else if (model instanceof File) {
json = toString((File) model, contentTypeAsString);
} else {
throw new UnsupportedOperationException("Internal error: Can't encode " + model + " to JSON.");
}
return createEntity(contentTypeAsString, json);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class ArrayCachedClass method coerceArgument.
public Object coerceArgument(Object argument) {
Class argumentClass = argument.getClass();
if (argumentClass.getName().charAt(0) != '[')
return argument;
Class argumentComponent = argumentClass.getComponentType();
Class paramComponent = getTheClass().getComponentType();
if (paramComponent.isPrimitive()) {
argument = DefaultTypeTransformation.convertToPrimitiveArray(argument, paramComponent);
} else if (paramComponent == String.class && argument instanceof GString[]) {
GString[] strings = (GString[]) argument;
String[] ret = new String[strings.length];
for (int i = 0; i < strings.length; i++) {
ret[i] = strings[i].toString();
}
argument = ret;
} else if (paramComponent == Object.class && argumentComponent.isPrimitive()) {
argument = DefaultTypeTransformation.primitiveArrayBox(argument);
}
return argument;
}
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;
}
Aggregations