Search in sources :

Example 61 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class NodeList method setMetaClass.

protected static void setMetaClass(final Class nodelistClass, final MetaClass metaClass) {
    final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {

        @Override
        public Object getAttribute(final Object object, final String attribute) {
            NodeList nl = (NodeList) object;
            Iterator it = nl.iterator();
            List result = new ArrayList();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                result.add(node.attributes().get(attribute));
            }
            return result;
        }

        @Override
        public void setAttribute(final Object object, final String attribute, final Object newValue) {
            for (Object o : (NodeList) object) {
                Node node = (Node) o;
                node.attributes().put(attribute, newValue);
            }
        }

        @Override
        public Object getProperty(Object object, String property) {
            if (object instanceof NodeList) {
                NodeList nl = (NodeList) object;
                return nl.getAt(property);
            }
            return super.getProperty(object, property);
        }
    };
    GroovySystem.getMetaClassRegistry().setMetaClass(nodelistClass, newMetaClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Example 62 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class GroovyClassLoaderTest method testCompile.

public void testCompile() throws Exception {
    Class groovyClass = loader.parseClass(new File("src/test/org/codehaus/groovy/classgen/Main.groovy"));
    System.out.println("Invoking main...");
    GroovyObject object = (GroovyObject) groovyClass.newInstance();
    assertTrue(object != null);
    MetaClass metaClass = object.getMetaClass();
    System.out.println("Metaclass: " + metaClass);
    Class type = object.getClass();
    System.out.println("Type: " + type);
    // invoke via metaclass
    metaClass.invokeMethod(object, "main", null);
    // invoke directly
    object.invokeMethod("main", null);
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClass(groovy.lang.MetaClass) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 63 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 64 with MetaClass

use of groovy.lang.MetaClass in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngine method eval.

Object eval(final Class scriptClass, final ScriptContext context) throws ScriptException {
    this.checkClearCache();
    context.setAttribute("context", context, ScriptContext.ENGINE_SCOPE);
    java.io.Writer writer = context.getWriter();
    context.setAttribute("out", writer instanceof PrintWriter ? writer : new PrintWriter(writer), ScriptContext.ENGINE_SCOPE);
    Binding binding = new Binding() {

        public Object getVariable(String name) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope != -1) {
                    return context.getAttribute(name, scope);
                }
                throw new MissingPropertyException(name, getClass());
            }
        }

        public void setVariable(String name, Object value) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope == -1) {
                    scope = ScriptContext.ENGINE_SCOPE;
                }
                context.setAttribute(name, value, scope);
            }
        }
    };
    try {
        Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
        Method[] methods = scriptClass.getMethods();
        Map<String, MethodClosure> closures = new HashMap<String, MethodClosure>();
        for (Method m : methods) {
            String name = m.getName();
            closures.put(name, new MethodClosure(scriptObject, name));
        }
        globalClosures.putAll(closures);
        final MetaClass oldMetaClass = scriptObject.getMetaClass();
        scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {

            public Object invokeMethod(Object object, String name, Object args) {
                if (args == null) {
                    return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                } else if (args instanceof Tuple) {
                    return invokeMethod(object, name, ((Tuple) args).toArray());
                } else if (args instanceof Object[]) {
                    return invokeMethod(object, name, (Object[]) args);
                } else {
                    return invokeMethod(object, name, new Object[] { args });
                }
            }

            public Object invokeMethod(Object object, String name, Object[] args) {
                try {
                    return super.invokeMethod(object, name, args);
                } catch (MissingMethodException mme) {
                    return callGlobal(name, args, context);
                }
            }

            public Object invokeStaticMethod(Object object, String name, Object[] args) {
                try {
                    return super.invokeStaticMethod(object, name, args);
                } catch (MissingMethodException mme) {
                    return callGlobal(name, args, context);
                }
            }
        });
        return scriptObject.run();
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) CompiledScript(javax.script.CompiledScript) GroovyCompiledScript(org.codehaus.groovy.jsr223.GroovyCompiledScript) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MissingPropertyException(groovy.lang.MissingPropertyException) Method(java.lang.reflect.Method) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) Tuple(groovy.lang.Tuple) PrintWriter(java.io.PrintWriter)

Aggregations

MetaClass (groovy.lang.MetaClass)64 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