use of groovy.lang.MetaClass in project groovy by apache.
the class InvokerHelper method createScript.
public static Script createScript(Class scriptClass, Binding context) {
Script script;
if (scriptClass == null) {
script = new NullScript(context);
} else {
try {
if (Script.class.isAssignableFrom(scriptClass)) {
try {
Constructor constructor = scriptClass.getConstructor(Binding.class);
script = (Script) constructor.newInstance(context);
} catch (NoSuchMethodException e) {
// Fallback for non-standard "Script" classes.
script = (Script) scriptClass.newInstance();
script.setBinding(context);
}
} else {
final GroovyObject object = (GroovyObject) scriptClass.newInstance();
// it could just be a class, so let's wrap it in a Script
// wrapper; though the bindings will be ignored
script = new Script(context) {
public Object run() {
Object argsToPass = EMPTY_MAIN_ARGS;
try {
Object args = getProperty("args");
if (args instanceof String[]) {
argsToPass = args;
}
} catch (MissingPropertyException e) {
// They'll get empty args since none exist in the context.
}
object.invokeMethod("main", argsToPass);
return null;
}
};
Map variables = context.getVariables();
MetaClass mc = getMetaClass(object);
for (Object o : variables.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String key = entry.getKey().toString();
// assume underscore variables are for the wrapper script
setPropertySafe(key.startsWith("_") ? script : object, mc, key, entry.getValue());
}
}
} catch (Exception e) {
throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e, e);
}
}
return script;
}
use of groovy.lang.MetaClass in project groovy by apache.
the class InvokerHelper method setProperties.
/**
* Sets the properties on the given object
*/
public static void setProperties(Object object, Map map) {
MetaClass mc = getMetaClass(object);
for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String key = entry.getKey().toString();
Object value = entry.getValue();
setPropertySafe(object, mc, key, value);
}
}
use of groovy.lang.MetaClass in project groovy by apache.
the class InvokerHelper method invokeMethod.
/**
* Invokes the given method on the object.
*/
public static Object invokeMethod(Object object, String methodName, Object arguments) {
if (object == null) {
object = NullObject.getNullObject();
//throw new NullPointerException("Cannot invoke method " + methodName + "() on null object");
}
// if the object is a Class, call a static method from that class
if (object instanceof Class) {
Class theClass = (Class) object;
MetaClass metaClass = metaRegistry.getMetaClass(theClass);
return metaClass.invokeStaticMethod(object, methodName, asArray(arguments));
}
// it's an instance; check if it's a Java one
if (!(object instanceof GroovyObject)) {
return invokePojoMethod(object, methodName, arguments);
}
// a groovy instance (including builder, closure, ...)
return invokePogoMethod(object, methodName, arguments);
}
use of groovy.lang.MetaClass in project groovy by apache.
the class InvokerHelper method invokeSuperMethod.
public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {
if (object == null) {
throw new NullPointerException("Cannot invoke method " + methodName + "() on null object");
}
Class theClass = object.getClass();
MetaClass metaClass = metaRegistry.getMetaClass(theClass.getSuperclass());
return metaClass.invokeMethod(object, methodName, asArray(arguments));
}
use of groovy.lang.MetaClass in project groovy by apache.
the class Node method setMetaClass.
/**
* Extension point for subclasses to override the metaclass. The default
* one supports the property and @ attribute notations.
*
* @param metaClass the original metaclass
* @param nodeClass the class whose metaclass we wish to override (this class or a subclass)
*/
protected static void setMetaClass(final MetaClass metaClass, Class nodeClass) {
// TODO Is protected static a bit of a smell?
// TODO perhaps set nodeClass to be Class<? extends Node>
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
@Override
public Object getAttribute(final Object object, final String attribute) {
Node n = (Node) object;
return n.get("@" + attribute);
}
@Override
public void setAttribute(final Object object, final String attribute, final Object newValue) {
Node n = (Node) object;
n.attributes().put(attribute, newValue);
}
@Override
public Object getProperty(Object object, String property) {
if (object instanceof Node) {
Node n = (Node) object;
return n.get(property);
}
return super.getProperty(object, property);
}
@Override
public void setProperty(Object object, String property, Object newValue) {
if (property.startsWith("@")) {
setAttribute(object, property.substring(1), newValue);
return;
}
delegate.setProperty(object, property, newValue);
}
};
GroovySystem.getMetaClassRegistry().setMetaClass(nodeClass, newMetaClass);
}
Aggregations