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();
}
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);
}
}
}
}
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();
}
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);
}
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);
}
Aggregations