Search in sources :

Example 31 with MetaClass

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

the class DefaultGroovyMethods method dump.

/**
 * Generates a detailed dump string of an object showing its class,
 * hashCode and all accessible fields.
 *
 * @param self an object
 * @return the dump representation
 * @since 1.0
 */
public static String dump(Object self) {
    if (self == null) {
        return "null";
    }
    StringBuilder buffer = new StringBuilder("<");
    Class klass = self.getClass();
    buffer.append(klass.getName());
    buffer.append("@");
    buffer.append(Integer.toHexString(self.hashCode()));
    boolean groovyObject = self instanceof GroovyObject;
    while (klass != null) {
        for (final Field field : klass.getDeclaredFields()) {
            if ((field.getModifiers() & Modifier.STATIC) == 0) {
                if (groovyObject && field.getName().equals("metaClass")) {
                    continue;
                }
                VMPluginFactory.getPlugin().doPrivileged((PrivilegedAction<Object>) () -> {
                    ReflectionUtils.trySetAccessible(field);
                    return null;
                });
                buffer.append(" ");
                buffer.append(field.getName());
                buffer.append("=");
                try {
                    buffer.append(FormatHelper.toString(field.get(self)));
                } catch (Exception e) {
                    buffer.append(e);
                }
            }
        }
        klass = klass.getSuperclass();
    }
    buffer.append(">");
    return buffer.toString();
}
Also used : Field(java.lang.reflect.Field) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyCastException(org.codehaus.groovy.runtime.typehandling.GroovyCastException) NoSuchElementException(java.util.NoSuchElementException) GroovyObject(groovy.lang.GroovyObject)

Example 32 with MetaClass

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

the class DefaultGroovyMethods method metaClass.

/**
 * Sets/updates the metaclass for a given object to a closure.
 *
 * @param self the object whose metaclass we wish to update
 * @param closure the closure representing the new metaclass
 * @return the new metaclass value
 * @throws GroovyRuntimeException if the metaclass can't be set for this object
 * @since 1.6.0
 */
public static MetaClass metaClass(Object self, @ClosureParams(value = SimpleType.class, options = "java.lang.Object") @DelegatesTo(type = "groovy.lang.ExpandoMetaClass.DefiningClosure", strategy = Closure.DELEGATE_ONLY) Closure closure) {
    MetaClass emc = hasPerInstanceMetaClass(self);
    if (emc == null) {
        final ExpandoMetaClass metaClass = new ExpandoMetaClass(self.getClass(), false, true);
        metaClass.initialize();
        metaClass.define(closure);
        if (self instanceof GroovyObject) {
            setMetaClass((GroovyObject) self, metaClass);
        } else {
            setMetaClass(self, metaClass);
        }
        return metaClass;
    } else {
        if (emc instanceof ExpandoMetaClass) {
            ((ExpandoMetaClass) emc).define(closure);
            return emc;
        } else {
            if (emc instanceof DelegatingMetaClass && ((DelegatingMetaClass) emc).getAdaptee() instanceof ExpandoMetaClass) {
                ((ExpandoMetaClass) ((DelegatingMetaClass) emc).getAdaptee()).define(closure);
                return emc;
            } else {
                throw new RuntimeException("Can't add methods to non-ExpandoMetaClass " + emc);
            }
        }
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 33 with MetaClass

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

the class ManagedConcurrentValueMapStressTest method size.

private static int size(ManagedConcurrentValueMap<String, Object> map) {
    MetaClass metaClass = InvokerHelper.getMetaClass(map);
    ConcurrentHashMap<String, Object> internalMap = (ConcurrentHashMap<String, Object>) metaClass.getProperty(map, "internalMap");
    return internalMap.size();
}
Also used : MetaClass(groovy.lang.MetaClass) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 34 with MetaClass

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

the class OwnedMetaClass method getMetaMethod.

public MetaMethod getMetaMethod(String name, Class[] argTypes) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.getMetaMethod(name, argTypes);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 35 with MetaClass

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

the class OwnedMetaClass method getProperty.

@Override
public Object getProperty(Class sender, Object receiver, String messageName, boolean useSuper, boolean fromInsideClass) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.getProperty(sender, receiver, messageName, useSuper, fromInsideClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Aggregations

MetaClass (groovy.lang.MetaClass)141 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)62 GroovyObject (groovy.lang.GroovyObject)62 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)40 MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaProperty (groovy.lang.MetaProperty)12 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)11 MetaMethod (groovy.lang.MetaMethod)11 ArrayList (java.util.ArrayList)11 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)11 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 MetaClassRegistry (groovy.lang.MetaClassRegistry)9 Map (java.util.Map)9 GString (groovy.lang.GString)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)7 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)7 MissingMethodException (groovy.lang.MissingMethodException)6 MissingPropertyException (groovy.lang.MissingPropertyException)6 IOException (java.io.IOException)6 Method (java.lang.reflect.Method)6