Package groovy.lang

Examples of groovy.lang.MetaMethod


    public Object onCall(MuleEventContext eventContext) throws Exception
    {
        if (refreshableBean instanceof GroovyObject)
        {
            GroovyObject script = (GroovyObject)refreshableBean;
            MetaMethod onCall = script.getMetaClass().pickMethod("onCall", MULE_EVENT_CONTEXT);

            if (onCall != null)
            {
                return script.invokeMethod(ON_CALL, eventContext);
            }
View Full Code Here


    public boolean supports(Object object) {
        return getToJSONMethod(object) != null;
    }

    public void marshalObject(Object object, JSON converter) throws ConverterException {
        MetaMethod method = getToJSONMethod(object);
        try {
            Object result = method.invoke(object, new Object[]{ converter });
            if (result != null && !(result instanceof JSON) && !(result instanceof JSONWriter)) {
                converter.convertAnother(result);
            }
        }
        catch(Throwable e) {
View Full Code Here

    public boolean supports(Object object) {
        return getToXMLMethod(object) != null;
    }

    public void marshalObject(Object object, XML converter) throws ConverterException {
        MetaMethod method = getToXMLMethod(object);
        try {
            Object result = method.invoke(object, new Object[]{ converter });
            if (result != null && !(result instanceof XML)) {
                converter.convertAnother(result);
            }
        }
        catch(Throwable e) {
View Full Code Here

            }
        }

        MetaClass metaclass = InvokerHelper.getMetaClass(self);

        MetaMethod mm = metaclass.pickMethod(name, types);

        // try some simple transformations
        // transform a trailing closure to a function
        if (mm == null && objects.length > 0) {
            Object lastArg = objects[objects.length - 1];
            if (lastArg instanceof Closure) {
                Class<?>[] at2 = Arrays.copyOf(types, types.length);
                at2[objects.length - 1] = Function.class;
                mm = metaclass.pickMethod(name, at2);
                if (mm != null) {
                    objects[objects.length - 1] new ClosureFunction((Closure) lastArg);
                }
            }
        }


        // try instantiating a single class
        if (mm == null && objects.length == 1 && objects[0] instanceof Class) {
            final Class<?> cls = (Class) objects[0];
            Class[] at2 = {cls};
            final MetaMethod method = metaclass.pickMethod(name, at2);

            if (method != null) {
                try {
                    final Constructor ctor = cls.getConstructor();
                    return new Supplier<Object>() {
                        @Override
                        public Object get() {
                            Object[] objs;
                            try {
                                objs = new Object[]{ctor.newInstance()};
                            } catch (InstantiationException e) {
                                throw new RuntimeException("cannot instantiate " + cls, e);
                            } catch (IllegalAccessException e) {
                                throw new RuntimeException("cannot instantiate " + cls, e);
                            } catch (InvocationTargetException e) {
                                throw new RuntimeException("cannot instantiate " + cls, e);
                            }
                            return method.doMethodInvoke(self, objs);
                        }
                    };
                } catch (NoSuchMethodException e) {
                    /* no constructor avaialble, ignore */
                }
            }
        }

        if (mm == null) {
            return null;
        } else {
            final MetaMethod method = mm;
            final Object[] finalArgs = objects;
            return new Supplier<Object>() {
                @Override
                public Object get() {
                    return method.doMethodInvoke(self, finalArgs);
                }
            };
        }
    }
View Full Code Here

            throw e;
        }

        metaclass = InvokerHelper.getMetaClass(obj);

        MetaMethod mm = metaclass.getMetaMethod("setEvalConfig", new Class[]{EvalConfig.class});
        if (mm != null) {
            mm.invoke(obj, new Object[]{project.getConfig()});
        }

        mm = metaclass.getMetaMethod("setEvalProject", new Class[]{EvalProject.class});
        if (mm == null) {
            mm = metaclass.getMetaMethod("setProject", new Class[]{EvalProject.class});
        }
        if (mm != null) {
            mm.invoke(obj, new Object[]{project});
        }

        if (split.getRight() != null) {
            GroovyUtils.callWithDelegate(split.getRight(), makeConfigDelegate(obj));
        }
View Full Code Here

    private void appendMethods(StringBuilder buffer) {
        for (int i = 0; i < methods.size; i++) {
            buffer.append("\n  ");
            Object methodOrConstructor = methods.get(i);
            if (methodOrConstructor instanceof MetaMethod) {
                MetaMethod method = (MetaMethod) methodOrConstructor;
                buffer.append(Modifier.toString(method.getModifiers()));
                buffer.append(" ").append(method.getReturnType().getName());
                buffer.append(" ").append(method.getDeclaringClass().getName());
                buffer.append("#");
                buffer.append(method.getName());
                appendClassNames(buffer,method.getNativeParameterTypes());
            }
            else {
                CachedConstructor method = (CachedConstructor) methodOrConstructor;
                buffer.append(Modifier.toString(method.cachedConstructor.getModifiers()));
                buffer.append(" ").append(method.cachedConstructor.getDeclaringClass().getName());
                buffer.append("#<init>");
                appendClassNames(buffer,method.getNativeParameterTypes());
            }
        }
    }
View Full Code Here

        MetaClass metaClass = InvokerHelper.getMetaClass(objectUnderInspection);
        List metaMethods = metaClass.getMetaMethods();
        Object[] result = new Object[metaMethods.size()];
        int i = 0;
        for (Iterator iter = metaMethods.iterator(); iter.hasNext(); i++) {
            MetaMethod metaMethod = (MetaMethod) iter.next();
            result[i] = methodInfo(metaMethod);
        }
        return result;
    }
View Full Code Here

                try {
                    Class cls = Class.forName(className, false, DefaultGroovyMethods.class.getClassLoader());
                    Constructor[] declaredConstructors = cls.getDeclaredConstructors();
                    assertEquals(1, declaredConstructors.length);
                    Constructor constructor = declaredConstructors[0];
                    final MetaMethod metaMethod = (MetaMethod) constructor.newInstance(null,null, null, null);
                } catch (ClassNotFoundException e) {
                    fail("Failed to load " + className);
                } catch (IllegalAccessException e) {
                    fail("Failed to instantiate " + className);
                } catch (InstantiationException e) {
View Full Code Here

        this.setterName = MetaProperty.getSetterName(name);
    }

    @Override
    public Object getProperty(Object object) {
        MetaMethod getter = getGetter();
        if (getter == null) {
            if (field != null) return field.getProperty(object);
            throw new GroovyRuntimeException("Cannot read write-only property: " + name);
        }
        return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY);
    }
View Full Code Here

         * we will use that information to create the target MethodHandle.
         * If that is not the case we will produce a handle, which will use the
         * MetaMethod itself for invocation.
         */
        public void setHandleForMetaMethod() {
            MetaMethod metaMethod = method;
            isCategoryMethod = method instanceof CategoryMethod;

            if (
                    metaMethod instanceof NumberNumberMetaMethod ||
                    (method instanceof GeneratedMetaMethod && (name.equals("next") || name.equals("previous")))
View Full Code Here

TOP

Related Classes of groovy.lang.MetaMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.