Package groovy.lang

Examples of groovy.lang.MetaClass


    private static final String HIBERNATE_LAZY_INITIALIZER_PROP = "hibernateLazyInitializer";
    private static final String IMPLEMENTATION_PROP = "implementation";

    public boolean supports(Object object) {
        if (object == null) return false;
        MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
        return mc.hasProperty(object, HIBERNATE_LAZY_INITIALIZER_PROP) != null;
    }
View Full Code Here


    private Object unwrap(Object o) {
        if (o == null) return o;

        final MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
        MetaClass mc = registry.getMetaClass(o.getClass());

        final Object hibernateLazyInitializer = mc.getProperty(o, HIBERNATE_LAZY_INITIALIZER_PROP);
        return registry.getMetaClass(hibernateLazyInitializer.getClass()).getProperty(
                hibernateLazyInitializer, IMPLEMENTATION_PROP);
    }
View Full Code Here

                new ConverterException("Error invoking toXML method of object with class " + object.getClass().getName(),e);
        }
    }

    protected MetaMethod getToXMLMethod(Object object) {
        MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
        if (mc != null) {
            return mc.getMetaMethod("toXML", new Object[] { XML.class });
        }
        return null;
    }
View Full Code Here

        //set up a map with ERRORS_PROPERTY
        Map map = new HashMap();
        StringWithError value = new StringWithError("flinstone");
        map.put("fred",value);
        map.put("barney","rabble");
        MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(value.getClass());
        mc.setProperty(value, ERRORS_PROPERTY, new Object());

        //put the map to scope
        FlashScope fs = new GrailsFlashScope();
        fs.put("test", "value");
        fs.put("flinstones", map);
View Full Code Here

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void copyExpandoMetaClass(Class<?> fromClass, Class<?> toClass, boolean removeSource) {

        MetaClassRegistry registry = getRegistry();

        MetaClass oldMetaClass = registry.getMetaClass(fromClass);

        AdaptingMetaClass adapter = null;
        ExpandoMetaClass emc;

        if (oldMetaClass instanceof AdaptingMetaClass) {
            adapter = ((AdaptingMetaClass)oldMetaClass);
            emc = (ExpandoMetaClass)adapter.getAdaptee();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Obtained adapted MetaClass ["+emc+"] from AdapterMetaClass instance ["+adapter+"]");
            }

            if (removeSource) {
                registry.removeMetaClass(fromClass);
            }
        }
        else {
            emc = (ExpandoMetaClass)oldMetaClass;
            if (LOG.isDebugEnabled()) {
                LOG.debug("No adapter MetaClass found, using original ["+emc+"]");
            }
        }

        ExpandoMetaClass replacement = new ExpandoMetaClass(toClass, true, true);

        for (Object obj : emc.getExpandoMethods()) {
            if (obj instanceof ClosureInvokingMethod) {
                ClosureInvokingMethod cim = (ClosureInvokingMethod) obj;
                Closure callable = cim.getClosure();
                if (!cim.isStatic()) {
                    replacement.setProperty(cim.getName(), callable);
                }
                else {
                    ((GroovyObject)replacement.getProperty(ExpandoMetaClass.STATIC_QUALIFIER)).setProperty(cim.getName(),callable);
                }
            }
        }

        for (Object o : emc.getExpandoProperties()) {
            if (o instanceof ThreadManagedMetaBeanProperty) {
                ThreadManagedMetaBeanProperty mbp = (ThreadManagedMetaBeanProperty)o;
                replacement.setProperty(mbp.getName(), mbp.getInitialValue());
            }
        }
        replacement.initialize();

        if (adapter == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding MetaClass for class ["+toClass+"] MetaClass ["+replacement+"]");
            }
            registry.setMetaClass(toClass, replacement);
        }
        else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding MetaClass for class [" + toClass + "] MetaClass [" + replacement +
                        "] with adapter [" + adapter + "]");
            }
            try {
                Constructor c = adapter.getClass().getConstructor(new Class[]{MetaClass.class});
                MetaClass newAdapter = (MetaClass) BeanUtils.instantiateClass(c,new Object[]{replacement});
                registry.setMetaClass(toClass,newAdapter);
            }
            catch (NoSuchMethodException e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Exception thrown constructing new MetaClass adapter when reloading: " + e.getMessage(),e);
View Full Code Here

    }

    public static ExpandoMetaClass getExpandoMetaClass(Class<?> aClass) {
        MetaClassRegistry registry = getRegistry();

        MetaClass mc = registry.getMetaClass(aClass);
        if (mc instanceof ExpandoMetaClass) {
            ExpandoMetaClass emc = (ExpandoMetaClass) mc;
            registry.setMetaClass(aClass, emc); // make permanent
            return emc;
        }
View Full Code Here

    }

    public static MetaClass getMetaClass(Object instance) {
        if (instance instanceof GroovyObject) {
            GroovyObject groovyObject = (GroovyObject) instance;
            MetaClass metaClass = groovyObject.getMetaClass();
           
            metaClass = unwrapDelegatingMetaClass(metaClass);
           
            if (!(metaClass instanceof ExpandoMetaClass)) {
                metaClass = getExpandoMetaClass(instance.getClass());
View Full Code Here

     * @param requiredType The required type of the property
     * @return The property value
     */
    @SuppressWarnings("unchecked")
    public static <T> T getPropertyIfExists(Object instance, String property, Class<T> requiredType) {
        MetaClass metaClass = getMetaClass(instance);

        MetaProperty metaProperty = metaClass.getMetaProperty(property);
        if (metaProperty != null) {
            Object value = metaProperty.getProperty(instance);
            if (value != null && requiredType.isInstance(value)) {
                return (T) value;
            }
View Full Code Here

     * @param args The arguments
     *
     * @return The result of the method call or null
     */
    public static Object invokeMethodIfExists(Object instance, String methodName, Object[] args) {
        MetaClass metaClass = getMetaClass(instance);
        List<MetaMethod> methodList = metaClass.respondsTo(instance, methodName, args);
        if (methodList != null && !methodList.isEmpty()) {
            return metaClass.invokeMethod(instance, methodName, args);
        }
        return null;
    }
View Full Code Here

        if (args.length > 1 && args[args.length - 1] instanceof Closure) {
            return invokeBeanDefiningMethod(name, args);
        }

        ApplicationContext ctx = springConfig.getUnrefreshedApplicationContext();
        MetaClass mc = DefaultGroovyMethods.getMetaClass(ctx);
        if (!mc.respondsTo(ctx, name, args).isEmpty()) {
            return mc.invokeMethod(ctx,name, args);
        }

        return this;
    }
View Full Code Here

TOP

Related Classes of groovy.lang.MetaClass

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.