Package groovy.lang

Examples of groovy.lang.MetaClass


  public void applyAttributes(GroovyBuilder builder, ObjectFactoryInterceptor ofi, String nodeName, Object instance, Map<Object, Object> attributes) {
    if (attributes == null) return;
    if (instance instanceof SelfAttributeApplicator) {
      ((SelfAttributeApplicator)instance).applyAttributes(attributes);
    } else {
      MetaClass mc = ofi.getMetaClassForNode(nodeName);
      if (mc.getTheClass() != instance.getClass()) mc = InvokerHelper.getMetaClass(instance.getClass());
      for (Object key : attributes.keySet()) {
        MetaProperty prop = mc.getMetaProperty(String.valueOf(key));
        if (prop == null) {
          try {
            Field field = mc.getTheClass().getField(String.valueOf(key));
            field.set(instance, attributes.get(key));
          }
          catch (SecurityException e) {}
          catch (NoSuchFieldException e) {}
          catch (IllegalAccessException e) {}
View Full Code Here


   *
   * @see ObjectFactoryInterceptor#isPassParentToConstructor()
   */
  public void associate(GroovyBuilder builder, ObjectFactoryInterceptor ofi, Object child, Object parent) {
    if (parent != null) {
      MetaClass pmc = InvokerHelper.getMetaClass(child.getClass());
      pmc.invokeMethod(parent, "addChild", new Object[] { parent });
    }
   
    MetaClass cmc = InvokerHelper.getMetaClass(child.getClass());
    cmc.invokeMethod(child, "setParent", new Object[] { parent });
  }
View Full Code Here

     /**
     * Get info about instance and class Methods that are dynamically added through Groovy.
     * @return  Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
     */
    public Object[] getMetaMethods(){
        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);
View Full Code Here

    public Object build(Script script) {
        // this used to be synchronized, but we also used to remove the
        // metaclass.  Since adding the metaclass is now a side effect, we
        // don't need to ensure the meta-class won't be observed and don't
        // need to hide the side effect.
        MetaClass scriptMetaClass = script.getMetaClass();
        script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this));
        script.setBinding(this);
        return script.run();
    }
View Full Code Here

    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);
            }
        }
View Full Code Here

        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);
    }
View Full Code Here

            this.parent().children().add(child);
        }
    }

    protected static void setMetaClass(final MetaClass metaClass, Class nodeClass) {
        final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
            @Override
            public Object getAttribute(final Object object, final String attribute) {
                Node n = (Node) object;
                return n.get("@" + attribute);
            }
View Full Code Here

        final String typeName = field.getClass().getName();
        throw new RuntimeException(createErrorMessage(className, fieldName, typeName, "constructing"));
    }

    public static void checkPropNames(Object instance, Map<String, Object> args) {
        final MetaClass metaClass = InvokerHelper.getMetaClass(instance);
        for (String k : args.keySet()) {
            if (metaClass.hasProperty(instance, k) == null)
                throw new MissingPropertyException(k, instance.getClass());
        }
    }
View Full Code Here

                }

                // save all current closures into global closures map
                globalClosures.putAll(closures);

                MetaClass oldMetaClass = scriptObject.getMetaClass();

                /*
                * We override the MetaClass of this script object so that we can
                * forward calls to global closures (of previous or future "eval" calls)
                * This gives the illusion of working on the same "global" scope.
View Full Code Here

        for (Class categoryClass : categoryClasses) {

            final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
            final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);

            final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
            final List<MetaProperty> propList = metaClass.getProperties();
            for (MetaProperty prop : propList)
                if (self.getMetaProperty(prop.getName()) == null) {
                    mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
                }

            for (MetaProperty prop : cachedCategoryClass.getFields())
                if (self.getMetaProperty(prop.getName()) == null) {
                    mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
                }

            for (MetaMethod method : metaClass.getMethods()) {
                final int mod = method.getModifiers();

                if (!Modifier.isPublic(mod))
                    continue;
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.