use of groovy.lang.MetaClass in project spock by spockframework.
the class GroovyMockFactory method create.
public Object create(IMockConfiguration configuration, Specification specification) throws CannotCreateMockException {
final MetaClass oldMetaClass = GroovyRuntimeUtil.getMetaClass(configuration.getType());
GroovyMockMetaClass newMetaClass = new GroovyMockMetaClass(configuration, specification, oldMetaClass);
final Class<?> type = configuration.getType();
if (configuration.isGlobal()) {
if (type.isInterface()) {
throw new CannotCreateMockException(type, ". Global mocking is only possible for classes, but not for interfaces.");
}
GroovyRuntimeUtil.setMetaClass(type, newMetaClass);
specification.getSpecificationContext().getCurrentIteration().addCleanup(new Runnable() {
public void run() {
GroovyRuntimeUtil.setMetaClass(type, oldMetaClass);
}
});
return MockInstantiator.instantiate(type, type, configuration.getConstructorArgs(), configuration.isUseObjenesis());
}
if (isFinalClass(type)) {
final Object instance = MockInstantiator.instantiate(type, type, configuration.getConstructorArgs(), configuration.isUseObjenesis());
GroovyRuntimeUtil.setMetaClass(instance, newMetaClass);
return instance;
}
IProxyBasedMockInterceptor mockInterceptor = new GroovyMockInterceptor(configuration, specification, newMetaClass);
return ProxyBasedMockFactory.INSTANCE.create(type, Collections.<Class<?>>singletonList(GroovyObject.class), configuration.getConstructorArgs(), mockInterceptor, specification.getClass().getClassLoader(), configuration.isUseObjenesis());
}
use of groovy.lang.MetaClass in project groovy by apache.
the class BindPath method addListeners.
/**
* Add listeners to a specific object. Updates the bould flags and update set
*
* @param listener This listener to attach.
* @param newObject The object we should read our property off of.
* @param updateSet The list of objects we have added listeners to
*/
public void addListeners(PropertyChangeListener listener, Object newObject, Set updateSet) {
removeListeners();
if (newObject != null) {
// check for local synthetics
TriggerBinding syntheticTrigger = getSyntheticTriggerBinding(newObject);
MetaClass mc = InvokerHelper.getMetaClass(newObject);
if (syntheticTrigger != null) {
PropertyBinding psb = new PropertyBinding(newObject, propertyName);
PropertyChangeProxyTargetBinding proxytb = new PropertyChangeProxyTargetBinding(newObject, propertyName, listener);
syntheticFullBinding = syntheticTrigger.createBinding(psb, proxytb);
syntheticFullBinding.bind();
updateSet.add(newObject);
} else if (!mc.respondsTo(newObject, "addPropertyChangeListener", NAME_PARAMS).isEmpty()) {
InvokerHelper.invokeMethod(newObject, "addPropertyChangeListener", new Object[] { propertyName, listener });
localListener = listener;
updateSet.add(newObject);
} else if (!mc.respondsTo(newObject, "addPropertyChangeListener", GLOBAL_PARAMS).isEmpty()) {
InvokerHelper.invokeMethod(newObject, "addPropertyChangeListener", listener);
globalListener = listener;
updateSet.add(newObject);
}
}
currentObject = newObject;
}
use of groovy.lang.MetaClass in project groovy by apache.
the class GPathResult method setMetaClass.
/**
* Replaces the MetaClass of this GPathResult.
*
* @param metaClass the new MetaClass
*/
@Override
public void setMetaClass(final MetaClass metaClass) {
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
@Override
public Object getAttribute(final Object object, final String attribute) {
return GPathResult.this.getProperty("@" + attribute);
}
@Override
public void setAttribute(final Object object, final String attribute, final Object newValue) {
GPathResult.this.setProperty("@" + attribute, newValue);
}
};
super.setMetaClass(newMetaClass);
}
use of groovy.lang.MetaClass in project groovy by apache.
the class ResourceGroovyMethods method notFiltered.
private static boolean notFiltered(File file, Object filter, Object nameFilter, Object excludeFilter, Object excludeNameFilter) {
if (filter == null && nameFilter == null && excludeFilter == null && excludeNameFilter == null)
return true;
if (filter != null && nameFilter != null)
throw new IllegalArgumentException("Can't set both 'filter' and 'nameFilter'");
if (excludeFilter != null && excludeNameFilter != null)
throw new IllegalArgumentException("Can't set both 'excludeFilter' and 'excludeNameFilter'");
Object filterToUse = null;
Object filterParam = null;
if (filter != null) {
filterToUse = filter;
filterParam = file;
} else if (nameFilter != null) {
filterToUse = nameFilter;
filterParam = file.getName();
}
Object excludeFilterToUse = null;
Object excludeParam = null;
if (excludeFilter != null) {
excludeFilterToUse = excludeFilter;
excludeParam = file;
} else if (excludeNameFilter != null) {
excludeFilterToUse = excludeNameFilter;
excludeParam = file.getName();
}
final MetaClass filterMC = filterToUse == null ? null : InvokerHelper.getMetaClass(filterToUse);
final MetaClass excludeMC = excludeFilterToUse == null ? null : InvokerHelper.getMetaClass(excludeFilterToUse);
boolean included = filterToUse == null || DefaultTypeTransformation.castToBoolean(filterMC.invokeMethod(filterToUse, "isCase", filterParam));
boolean excluded = excludeFilterToUse != null && DefaultTypeTransformation.castToBoolean(excludeMC.invokeMethod(excludeFilterToUse, "isCase", excludeParam));
return included && !excluded;
}
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;
}
Aggregations