Search in sources :

Example 51 with MetaClass

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());
}
Also used : MetaClass(groovy.lang.MetaClass) CannotCreateMockException(org.spockframework.mock.CannotCreateMockException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 52 with MetaClass

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;
}
Also used : MetaClass(groovy.lang.MetaClass)

Example 53 with MetaClass

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);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Example 54 with MetaClass

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;
}
Also used : MetaClass(groovy.lang.MetaClass)

Example 55 with MetaClass

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;
}
Also used : Script(groovy.lang.Script) Constructor(java.lang.reflect.Constructor) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingPropertyException(groovy.lang.MissingPropertyException) GString(groovy.lang.GString) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingMethodException(groovy.lang.MissingMethodException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SpreadMapEvaluatingException(groovy.lang.SpreadMapEvaluatingException) IOException(java.io.IOException) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Aggregations

MetaClass (groovy.lang.MetaClass)65 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)18 GroovyObject (groovy.lang.GroovyObject)17 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)14 MetaClassImpl (groovy.lang.MetaClassImpl)10 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 GString (groovy.lang.GString)6 Map (java.util.Map)6 MetaClassRegistry (groovy.lang.MetaClassRegistry)5 MetaMethod (groovy.lang.MetaMethod)5 MissingMethodException (groovy.lang.MissingMethodException)5 HashMap (java.util.HashMap)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 MetaProperty (groovy.lang.MetaProperty)4 MissingPropertyException (groovy.lang.MissingPropertyException)4 Script (groovy.lang.Script)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)4 Binding (groovy.lang.Binding)3