| Method from groovy.lang.MetaClassImpl Detail: |
public void addMetaBeanProperty(MetaBeanProperty mp) {
MetaProperty staticProperty = establishStaticMetaProperty(mp);
if (staticProperty != null) {
staticPropertyIndex.put(mp.getName(), mp);
} else {
SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
//keep field
CachedField field;
MetaProperty old = (MetaProperty) propertyMap.get(mp.getName());
if (old != null) {
if (old instanceof MetaBeanProperty) {
field = ((MetaBeanProperty) old).getField();
} else {
field = (CachedField) old;
}
mp.setField(field);
}
// put it in the list
// this will overwrite a possible field property
propertyMap.put(mp.getName(), mp);
}
}
Adds a new MetaBeanProperty to this MetaClass |
public void addMetaMethod(MetaMethod method) {
if (isInitialized()) {
throw new RuntimeException("Already initialized, cannot add new method: " + method);
}
final CachedClass declaringClass = method.getDeclaringClass();
addMetaMethodToIndex(method, metaMethodIndex.getHeader(declaringClass.getTheClass()));
}
adds a MetaMethod to this class. WARNING: this method will not
do the neccessary steps for multimethod logic and using this
method doesn't mean, that a method added here is replacing another
method from a parent class completely. These steps are usually done
by initalize, which means if you need these steps, you have to add
the method before running initialize the first time. |
protected void addMetaMethodToIndex(MetaMethod method,
Header header) {
checkIfStdMethod(method);
String name = method.getName();
MetaMethodIndex.Entry e = metaMethodIndex.getOrPutMethods(name, header);
if (method.isStatic()) {
e.staticMethods = metaMethodIndex.addMethodToList(e.staticMethods, method);
}
e.methods = metaMethodIndex.addMethodToList(e.methods, method);
}
|
public void addNewInstanceMethod(Method method) {
final CachedMethod cachedMethod = CachedMethod.find(method);
NewInstanceMetaMethod newMethod = new NewInstanceMetaMethod(cachedMethod);
final CachedClass declaringClass = newMethod.getDeclaringClass();
addNewInstanceMethodToIndex(newMethod, metaMethodIndex.getHeader(declaringClass.getTheClass()));
}
|
public void addNewStaticMethod(Method method) {
final CachedMethod cachedMethod = CachedMethod.find(method);
NewStaticMetaMethod newMethod = new NewStaticMetaMethod(cachedMethod);
final CachedClass declaringClass = newMethod.getDeclaringClass();
addNewStaticMethodToIndex(newMethod, metaMethodIndex.getHeader(declaringClass.getTheClass()));
}
|
protected final void checkIfGroovyObjectMethod(MetaMethod metaMethod) {
if (metaMethod instanceof ClosureMetaMethod || metaMethod instanceof MixinInstanceMetaMethod) {
if(isGetPropertyMethod(metaMethod)) {
getPropertyMethod = metaMethod;
}
else if(isInvokeMethod(metaMethod)) {
invokeMethodMethod = metaMethod;
}
else if(isSetPropertyMethod(metaMethod)) {
setPropertyMethod = metaMethod;
}
}
}
Checks if the metaMethod is a method from the GroovyObject interface such as setProperty, getProperty and invokeMethod |
protected void checkInitalised() {
if (!isInitialized())
throw new IllegalStateException(
"initialize must be called for meta " +
"class of " + theClass +
"(" + this.getClass() + ") " +
"to complete initialisation process " +
"before any invocation or field/property " +
"access can be done");
}
checks if the initialisation of the class id complete.
This method should be called as a form of assert, it is no
way to test if there is still initialisation work to be done.
Such logic must be implemented in a different way. |
protected Object chooseMethod(String methodName,
Object methodOrList,
Class[] arguments) {
if (methodOrList instanceof MetaMethod) {
if (((ParameterTypes) methodOrList).isValidMethod(arguments)) {
return methodOrList;
}
return null;
}
FastArray methods = (FastArray) methodOrList;
if (methods==null) return null;
int methodCount = methods.size();
if (methodCount < = 0) {
return null;
} else if (methodCount == 1) {
Object method = methods.get(0);
if (((ParameterTypes) method).isValidMethod(arguments)) {
return method;
}
return null;
}
Object answer;
if (arguments == null || arguments.length == 0) {
answer = MetaClassHelper.chooseEmptyMethodParams(methods);
} else if (arguments.length == 1 && arguments[0] == null) {
answer = MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods);
} else {
Object matchingMethods = null;
final int len = methods.size;
Object data[] = methods.getArray();
for (int i = 0; i != len; ++i) {
Object method = data[i];
// making this false helps find matches
if (((ParameterTypes) method).isValidMethod(arguments)) {
if (matchingMethods == null)
matchingMethods = method;
else
if (matchingMethods instanceof ArrayList)
((ArrayList)matchingMethods).add(method);
else {
ArrayList arr = new ArrayList(4);
arr.add(matchingMethods);
arr.add(method);
matchingMethods = arr;
}
}
}
if (matchingMethods == null) {
return null;
} else if (!(matchingMethods instanceof ArrayList)) {
return matchingMethods;
}
return chooseMostSpecificParams(methodName, (List) matchingMethods, arguments);
}
if (answer != null) {
return answer;
}
throw new MethodSelectionException(methodName, methods, arguments);
}
Chooses the correct method to use from a list of methods which match by
name. |
protected void clearInvocationCaches() {
metaMethodIndex.clearCaches ();
}
remove all method call cache entries. This should be done if a
method is added during runtime, but not by using a category. |
public CallSite createConstructorSite(CallSite site,
Object[] args) {
if (!(this instanceof AdaptingMetaClass)) {
Class[] params = MetaClassHelper.convertToTypeArray(args);
CachedConstructor constructor = (CachedConstructor) chooseMethod("< init >", constructors, params);
if (constructor != null) {
return ConstructorSite.createConstructorSite(site, this,constructor,params, args);
}
else {
if (args.length == 1 && args[0] instanceof Map) {
constructor = (CachedConstructor) chooseMethod("< init >", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY);
if (constructor != null) {
return new ConstructorSite.NoParamSite(site, this,constructor,params);
}
}
}
}
return new MetaClassConstructorSite(site, this);
}
|
public CallSite createPogoCallCurrentSite(CallSite site,
Class sender,
Object[] args) {
if (site.getUsage().get() == 0 && !(this instanceof AdaptingMetaClass)) {
Class [] params = MetaClassHelper.convertToTypeArray(args);
MetaMethod metaMethod = getMethodWithCachingInternal(sender, site, params);
if (metaMethod != null)
return PogoMetaMethodSite.createPogoMetaMethodSite(site, this, metaMethod, params, args);
}
return new PogoMetaClassSite(site, this);
}
|
public CallSite createPogoCallSite(CallSite site,
Object[] args) {
if (site.getUsage().get() == 0 && !(this instanceof AdaptingMetaClass)) {
Class [] params = MetaClassHelper.convertToTypeArray(args);
MetaMethod metaMethod = getMethodWithCachingInternal(theClass, site, params);
if (metaMethod != null)
return PogoMetaMethodSite.createPogoMetaMethodSite(site, this, metaMethod, params, args);
}
return new PogoMetaClassSite(site, this);
}
|
public CallSite createPojoCallSite(CallSite site,
Object receiver,
Object[] args) {
if (!(this instanceof AdaptingMetaClass)) {
Class [] params = MetaClassHelper.convertToTypeArray(args);
MetaMethod metaMethod = getMethodWithCachingInternal(getTheClass(), site, params);
if (metaMethod != null)
return PojoMetaMethodSite.createPojoMetaMethodSite(site, this, metaMethod, params, receiver, args);
}
return new PojoMetaClassSite(site, this);
}
|
public CallSite createStaticSite(CallSite site,
Object[] args) {
if (!(this instanceof AdaptingMetaClass)) {
Class [] params = MetaClassHelper.convertToTypeArray(args);
MetaMethod metaMethod = retrieveStaticMethod(site.getName(), args);
if (metaMethod != null)
return StaticMetaMethodSite.createStaticMetaMethodSite(site, this, metaMethod, params, args);
}
return new StaticMetaClassSite(site, this);
}
|
protected void dropMethodCache(String name) {
metaMethodIndex.clearCaches(name);
}
|
protected void dropStaticMethodCache(String name) {
metaMethodIndex.clearCaches(name);
}
|
protected static MetaMethod findMethodInClassHierarchy(Class instanceKlazz,
String methodName,
Class[] arguments,
MetaClass metaClass) {
if (metaClass instanceof MetaClassImpl) {
boolean check = false;
for (ClassInfo ci : ((MetaClassImpl)metaClass).theCachedClass.getHierarchy ()) {
final MetaClass aClass = ci.getStrongMetaClass();
if (aClass instanceof MutableMetaClass && ((MutableMetaClass)aClass).isModified()) {
check = true;
break;
}
}
if (!check)
return null;
}
MetaMethod method = null;
Class superClass;
if (metaClass.getTheClass().isArray() && !metaClass.getTheClass().getComponentType().isPrimitive() && metaClass.getTheClass().getComponentType() != Object.class) {
superClass = Object[].class;
}
else {
superClass = metaClass.getTheClass().getSuperclass();
}
if (superClass != null) {
MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(superClass);
method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
}
else {
if (metaClass.getTheClass().isInterface()) {
MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(Object.class);
method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
}
}
method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);
MetaMethod infMethod = searchInterfacesForMetaMethod(instanceKlazz, methodName, arguments, metaClass);
if (infMethod != null) {
if (method == null)
method = infMethod;
else
method = mostSpecific(method, infMethod, instanceKlazz);
}
method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
return method;
}
|
protected MetaMethod findMixinMethod(String methodName,
Class[] arguments) {
return null;
}
|
protected static MetaMethod findOwnMethod(Class instanceKlazz,
String methodName,
Class[] arguments,
MetaClass metaClass,
MetaMethod method) {
// we trick ourselves here
if (instanceKlazz == metaClass.getTheClass())
return method;
MetaMethod ownMethod = metaClass.pickMethod(methodName, arguments);
if (ownMethod != null) {
if (method == null)
method = ownMethod;
else
method = mostSpecific(method, ownMethod, instanceKlazz);
}
return method;
}
|
protected MetaBeanProperty findPropertyInClassHierarchy(String propertyName,
CachedClass theClass) {
MetaBeanProperty property= null;
if (theClass == null)
return null;
final CachedClass superClass = theClass.getCachedSuperClass();
if (superClass == null)
return null;
MetaClass metaClass = this.registry.getMetaClass(superClass.getTheClass());
if(metaClass instanceof MutableMetaClass) {
property = getMetaPropertyFromMutableMetaClass(propertyName,metaClass);
if(property == null) {
if(superClass != ReflectionCache.OBJECT_CLASS) {
property = findPropertyInClassHierarchy(propertyName, superClass);
}
if(property == null) {
final Class[] interfaces = theClass.getTheClass().getInterfaces();
property = searchInterfacesForMetaProperty(propertyName, interfaces);
}
}
}
return property;
}
|
public MetaMethod[] getAdditionalMetaMethods() {
return additionalMetaMethods;
}
|
public Object getAttribute(Object object,
String attribute) {
return getAttribute(theClass, object, attribute, false, false);
}
|
public Object getAttribute(Class sender,
Object receiver,
String messageName,
boolean useSuper) {
return getAttribute(receiver, messageName);
}
|
public Object getAttribute(Class sender,
Object object,
String attribute,
boolean useSuper,
boolean fromInsideClass) {
checkInitalised();
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
MetaClass mc = registry.getMetaClass((Class) object);
return mc.getAttribute(sender, object, attribute, useSuper);
}
MetaProperty mp = getMetaProperty(sender, attribute, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
mp = mbp.getField();
}
try {
// delegate the get operation to the metaproperty
if (mp != null) return mp.getProperty(object);
} catch (Exception e) {
throw new GroovyRuntimeException("Cannot read field: " + attribute, e);
}
}
throw new MissingFieldException(attribute, theClass);
}
Looks up the given attribute (field) on the given object |
public ClassInfo getClassInfo() {
return theCachedClass.classInfo;
}
|
public ClassNode getClassNode() {
if (classNode == null && GroovyObject.class.isAssignableFrom(theClass)) {
// lets try load it from the classpath
String groovyFile = theClass.getName();
int idx = groovyFile.indexOf('$");
if (idx > 0) {
groovyFile = groovyFile.substring(0, idx);
}
groovyFile = groovyFile.replace('.", '/") + ".groovy";
//System.out.println("Attempting to load: " + groovyFile);
URL url = theClass.getClassLoader().getResource(groovyFile);
if (url == null) {
url = Thread.currentThread().getContextClassLoader().getResource(groovyFile);
}
if (url != null) {
try {
/**
* todo there is no CompileUnit in scope so class name
* checking won't work but that mostly affects the bytecode
* generation rather than viewing the AST
*/
CompilationUnit.ClassgenCallback search = new CompilationUnit.ClassgenCallback() {
public void call(ClassVisitor writer, ClassNode node) {
if (node.getName().equals(theClass.getName())) {
MetaClassImpl.this.classNode = node;
}
}
};
final ClassLoader parent = theClass.getClassLoader();
GroovyClassLoader gcl = (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new GroovyClassLoader(parent);
}
});
CompilationUnit unit = new CompilationUnit();
unit.setClassgenCallback(search);
unit.addSource(url);
unit.compile(Phases.CLASS_GENERATION);
}
catch (Exception e) {
throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
}
}
}
return classNode;
}
|
public MetaProperty getEffectiveGetMetaProperty(Class sender,
Object object,
String name,
boolean useSuper) {
//----------------------------------------------------------------------
// handling of static
//----------------------------------------------------------------------
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
return new MetaProperty(name, Object.class) {
final MetaClass mc = registry.getMetaClass((Class) object);
public Object getProperty(Object object) {
return mc.getProperty(sender, object, name, useSuper,false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
checkInitalised();
//----------------------------------------------------------------------
// turn getProperty on a Map to get on the Map itself
//----------------------------------------------------------------------
if (!isStatic && this.isMap) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return ((Map) object).get(name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
MetaMethod method = null;
//----------------------------------------------------------------------
// getter
//----------------------------------------------------------------------
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
method = mbp.getGetter();
mp = mbp.getField();
}
}
// check for a category method named like a getter
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
String getterName = "get" + MetaClassHelper.capitalize(name);
MetaMethod categoryMethod = getCategoryMethodGetter(sender, getterName, false);
if (categoryMethod != null)
method = categoryMethod;
}
//----------------------------------------------------------------------
// field
//----------------------------------------------------------------------
if (method != null)
return new GetBeanMethodMetaProperty(name, method);
if (mp != null) {
return mp;
// try {
// return mp.getProperty(object);
// } catch (IllegalArgumentException e) {
// // can't access the field directly but there may be a getter
// mp = null;
// }
}
//----------------------------------------------------------------------
// generic get method
//----------------------------------------------------------------------
// check for a generic get method provided through a category
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
method = getCategoryMethodGetter(sender, "get", true);
if (method != null)
return new GetMethodMetaProperty(name, method);
}
// the generic method is valid, if available (!=null), if static or
// if it is not static and we do no static access
if (genericGetMethod != null && !(!genericGetMethod.isStatic() && isStatic)) {
method = genericGetMethod;
if (method != null)
return new GetMethodMetaProperty(name, method);
}
//----------------------------------------------------------------------
// special cases
//----------------------------------------------------------------------
/** todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
MetaClass mc = registry.getMetaClass(Class.class);
return mc.getProperty(Class.class, object, name, useSuper, false);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Collection) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt((Collection) object, name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else if (object instanceof Object[]) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
} else {
MetaMethod addListenerMethod = (MetaMethod) listeners.get(name);
if (addListenerMethod != null) {
//TODO: one day we could try return the previously registered Closure listener for easy removal
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return null;
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
}
//----------------------------------------------------------------------
// error due to missing method/field
//----------------------------------------------------------------------
if (isStatic || object instanceof Class)
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeStaticMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
else
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
return invokeMissingProperty(object, name, null, true);
}
public void setProperty(Object object, Object newValue) {
throw new UnsupportedOperationException();
}
};
}
|
public MetaMethod getMetaMethod(String name,
Object[] argTypes) {
Class[] classes = MetaClassHelper.castArgumentsToClassArray(argTypes);
return pickMethod(name, classes);
}
|
public List getMetaMethods() {
return new ArrayList(newGroovyMethodsSet);
}
|
public MetaProperty getMetaProperty(String name) {
SingleKeyHashMap propertyMap = classPropertyIndex.getNotNull(theCachedClass);
if (propertyMap.containsKey(name)) {
return (MetaProperty) propertyMap.get(name);
} else if (staticPropertyIndex.containsKey(name)) {
return (MetaProperty) staticPropertyIndex.get(name);
} else {
propertyMap = classPropertyIndexForSuper.getNotNull(theCachedClass);
if (propertyMap.containsKey(name))
return (MetaProperty) propertyMap.get(name);
else {
CachedClass superClass = theCachedClass;
while(superClass != null && superClass != ReflectionCache.OBJECT_CLASS) {
final MetaBeanProperty property = findPropertyInClassHierarchy(name, superClass);
if(property != null) {
onSuperPropertyFoundInHierarchy(property);
return property;
}
superClass = superClass.getCachedSuperClass();
}
return null;
}
}
}
|
public MetaMethod getMethodWithCaching(Class sender,
String methodName,
Object[] arguments,
boolean isCallToSuper) {
// lets try use the cache to find the method
if (!isCallToSuper && GroovyCategorySupport.hasCategoryInCurrentThread()) {
return getMethodWithoutCaching(sender, methodName, MetaClassHelper.convertToTypeArray(arguments), isCallToSuper);
} else {
final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(sender, methodName);
if (e == null)
return null;
return isCallToSuper ? getSuperMethodWithCaching(arguments, e) : getNormalMethodWithCaching(arguments, e);
}
}
|
public MetaMethod getMethodWithoutCaching(Class sender,
String methodName,
Class[] arguments,
boolean isCallToSuper) {
MetaMethod method = null;
Object methods = getMethods(sender, methodName, isCallToSuper);
if (methods != null) {
method = (MetaMethod) chooseMethod(methodName, methods, arguments);
}
return method;
}
|
public List getMethods() {
return allMethods;
}
|
public List getProperties() {
checkInitalised();
SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
// simply return the values of the metaproperty map as a List
List ret = new ArrayList(propertyMap.size());
for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext();) {
MetaProperty element = (MetaProperty) ((SingleKeyHashMap.Entry) iter.next()).value;
if (element instanceof CachedField) continue;
// filter out DGM beans
if (element instanceof MetaBeanProperty) {
MetaBeanProperty mp = (MetaBeanProperty) element;
boolean setter = true;
boolean getter = true;
if (mp.getGetter() == null || mp.getGetter() instanceof GeneratedMetaMethod || mp.getGetter() instanceof NewInstanceMetaMethod) {
getter = false;
}
if (mp.getSetter() == null || mp.getSetter() instanceof GeneratedMetaMethod || mp.getSetter() instanceof NewInstanceMetaMethod) {
setter = false;
}
if (!setter && !getter) continue;
// TODO: I (ait) don't know why these strange tricks needed and comment following as it effects some Grails tests
// if (!setter && mp.getSetter() != null) {
// element = new MetaBeanProperty(mp.getName(), mp.getType(), mp.getGetter(), null);
// }
// if (!getter && mp.getGetter() != null) {
// element = new MetaBeanProperty(mp.getName(), mp.getType(), null, mp.getSetter());
// }
}
ret.add(element);
}
return ret;
}
Get all the properties defined for this type |
public Object getProperty(Object object,
String property) {
return getProperty(theClass, object, property, false, false);
}
|
public Object getProperty(Class sender,
Object object,
String name,
boolean useSuper,
boolean fromInsideClass) {
//----------------------------------------------------------------------
// handling of static
//----------------------------------------------------------------------
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
MetaClass mc = registry.getMetaClass((Class) object);
return mc.getProperty(sender, object, name, useSuper, false);
}
checkInitalised();
//----------------------------------------------------------------------
// turn getProperty on a Map to get on the Map itself
//----------------------------------------------------------------------
if (!isStatic && this.isMap) {
return ((Map) object).get(name);
}
MetaMethod method = null;
Object[] arguments = EMPTY_ARGUMENTS;
//----------------------------------------------------------------------
// getter
//----------------------------------------------------------------------
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
method = mbp.getGetter();
mp = mbp.getField();
}
}
// check for a category method named like a getter
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
String getterName = "get" + MetaClassHelper.capitalize(name);
MetaMethod categoryMethod = getCategoryMethodGetter(sender, getterName, false);
if (categoryMethod != null) method = categoryMethod;
}
//----------------------------------------------------------------------
// field
//----------------------------------------------------------------------
if (method == null && mp != null) {
try {
return mp.getProperty(object);
} catch (IllegalArgumentException e) {
// can't access the field directly but there may be a getter
mp = null;
}
}
//----------------------------------------------------------------------
// generic get method
//----------------------------------------------------------------------
// check for a generic get method provided through a category
if (method == null && !useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
method = getCategoryMethodGetter(sender, "get", true);
if (method != null) arguments = new Object[]{name};
}
// the generic method is valid, if available (!=null), if static or
// if it is not static and we do no static access
if (method == null && genericGetMethod != null && !(!genericGetMethod.isStatic() && isStatic)) {
arguments = new Object[]{name};
method = genericGetMethod;
}
//----------------------------------------------------------------------
// special cases
//----------------------------------------------------------------------
if (method == null) {
/** todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
MetaClass mc = registry.getMetaClass(Class.class);
return mc.getProperty(Class.class, object, name, useSuper, false);
} else if (object instanceof Collection) {
return DefaultGroovyMethods.getAt((Collection) object, name);
} else if (object instanceof Object[]) {
return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
} else {
MetaMethod addListenerMethod = (MetaMethod) listeners.get(name);
if (addListenerMethod != null) {
//TODO: one day we could try return the previously registered Closure listener for easy removal
return null;
}
}
} else {
//----------------------------------------------------------------------
// executing the getter method
//----------------------------------------------------------------------
return method.doMethodInvoke(object, arguments);
}
//----------------------------------------------------------------------
// error due to missing method/field
//----------------------------------------------------------------------
if (isStatic || object instanceof Class)
return invokeStaticMissingProperty(object, name, null, true);
else
return invokeMissingProperty(object, name, null, true);
}
|
public MetaMethod getStaticMetaMethod(String name,
Object[] argTypes) {
Class[] classes = MetaClassHelper.castArgumentsToClassArray(argTypes);
return pickStaticMethod(name, classes);
}
|
protected Object getSubclassMetaMethods(String methodName) {
return null;
}
|
protected LinkedList getSuperClasses() {
LinkedList< CachedClass > superClasses = new LinkedList< CachedClass >();
if (theClass.isInterface()) {
superClasses.addFirst(ReflectionCache.OBJECT_CLASS);
} else {
for (CachedClass c = theCachedClass; c != null; c = c.getCachedSuperClass()) {
superClasses.addFirst(c);
}
if (theCachedClass.isArray && theClass != Object[].class && !theClass.getComponentType().isPrimitive()) {
superClasses.addFirst(ReflectionCache.OBJECT_ARRAY_CLASS);
}
}
return superClasses;
}
|
public final CachedClass getTheCachedClass() {
return theCachedClass;
}
|
public Class getTheClass() {
return this.theClass;
}
|
public int getVersion() {
return theCachedClass.classInfo.getVersion();
}
|
public MetaProperty hasProperty(Object obj,
String name) {
return getMetaProperty(name);
}
|
public void incVersion() {
theCachedClass.classInfo.incVersion();
}
|
public synchronized void initialize() {
if (!isInitialized()) {
fillMethodIndex();
addProperties();
initialized = true;
}
}
|
public Object invokeConstructor(Object[] arguments) {
return invokeConstructor(theClass, arguments);
}
|
public Object invokeConstructorAt(Class at,
Object[] arguments) {
return invokeConstructor(arguments);
} Deprecated! use - invokeConstructor instead
Warning, this method will be removed |
public Object invokeMethod(Object object,
String methodName,
Object arguments) {
if (arguments == null) {
return invokeMethod(object, methodName, MetaClassHelper.EMPTY_ARRAY);
}
if (arguments instanceof Tuple) {
Tuple tuple = (Tuple) arguments;
return invokeMethod(object, methodName, tuple.toArray());
}
if (arguments instanceof Object[]) {
return invokeMethod(object, methodName, (Object[]) arguments);
} else {
return invokeMethod(object, methodName, new Object[]{arguments});
}
}
|
public Object invokeMethod(Object object,
String methodName,
Object[] originalArguments) {
return invokeMethod(theClass, object, methodName, originalArguments, false, false);
}
Invokes the given method on the object.
TODO: should this be deprecated? If so, we have to propogate to many places. |
public Object invokeMethod(Class sender,
Object object,
String methodName,
Object[] originalArguments,
boolean isCallToSuper,
boolean fromInsideClass) {
checkInitalised();
if (object == null) {
throw new NullPointerException("Cannot invoke method: " + methodName + " on null object");
}
final Object[] arguments = originalArguments == null ? EMPTY_ARGUMENTS : originalArguments;
// final Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
//
// unwrap(arguments);
MetaMethod method = getMethodWithCaching(sender, methodName, arguments, isCallToSuper);
MetaClassHelper.unwrap(arguments);
if (method == null)
method = tryListParamMetaMethod(sender, methodName, isCallToSuper, arguments);
final boolean isClosure = object instanceof Closure;
if (isClosure) {
final Closure closure = (Closure) object;
final Object owner = closure.getOwner();
if (CLOSURE_CALL_METHOD.equals(methodName) || CLOSURE_DO_CALL_METHOD.equals(methodName)) {
final Class objectClass = object.getClass();
if (objectClass == MethodClosure.class) {
final MethodClosure mc = (MethodClosure) object;
methodName = mc.getMethod();
final Class ownerClass = owner instanceof Class ? (Class) owner : owner.getClass();
final MetaClass ownerMetaClass = registry.getMetaClass(ownerClass);
return ownerMetaClass.invokeMethod(ownerClass, owner, methodName, arguments, false, false);
} else if (objectClass == CurriedClosure.class) {
final CurriedClosure cc = (CurriedClosure) object;
// change the arguments for an uncurried call
final Object[] curriedArguments = cc.getUncurriedArguments(arguments);
final Class ownerClass = owner instanceof Class ? (Class) owner : owner.getClass();
final MetaClass ownerMetaClass = registry.getMetaClass(ownerClass);
return ownerMetaClass.invokeMethod(owner, methodName, curriedArguments);
}
if (method==null) invokeMissingMethod(object,methodName,arguments);
} else if (CLOSURE_CURRY_METHOD.equals(methodName)) {
return closure.curry(arguments);
}
final Object delegate = closure.getDelegate();
final boolean isClosureNotOwner = owner != closure;
final int resolveStrategy = closure.getResolveStrategy();
final Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
switch (resolveStrategy) {
case Closure.TO_SELF:
method = closure.getMetaClass().pickMethod(methodName, argClasses);
if (method != null) return method.invoke(closure, arguments);
break;
case Closure.DELEGATE_ONLY:
if (method == null && delegate != closure && delegate != null) {
MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
method = delegateMetaClass.pickMethod(methodName, argClasses);
if (method != null)
return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
else if (delegate != closure && (delegate instanceof GroovyObject)) {
return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
}
}
break;
case Closure.OWNER_ONLY:
if (method == null && owner != closure) {
MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
}
break;
case Closure.DELEGATE_FIRST:
if (method == null && delegate != closure && delegate != null) {
MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
method = delegateMetaClass.pickMethod(methodName, argClasses);
if (method != null)
return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
}
if (method == null && owner != closure) {
MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
method = ownerMetaClass.pickMethod(methodName, argClasses);
if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
}
if (method == null && resolveStrategy != Closure.TO_SELF) {
// still no methods found, test if delegate or owner are GroovyObjects
// and invoke the method on them if so.
MissingMethodException last = null;
if (delegate != closure && (delegate instanceof GroovyObject)) {
try {
return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
} catch (MissingMethodException mme) {
if (last == null) last = mme;
}
}
if (isClosureNotOwner && (owner instanceof GroovyObject)) {
try {
return invokeMethodOnGroovyObject(methodName, originalArguments, owner);
} catch (MissingMethodException mme) {
last = mme;
}
}
if (last != null) return invokeMissingMethod(object, methodName, originalArguments, last, isCallToSuper);
}
break;
default:
if (method == null && owner != closure) {
MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
method = ownerMetaClass.pickMethod(methodName, argClasses);
if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
}
if (method == null && delegate != closure && delegate != null) {
MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
method = delegateMetaClass.pickMethod(methodName, argClasses);
if (method != null)
return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
}
if (method == null && resolveStrategy != Closure.TO_SELF) {
// still no methods found, test if delegate or owner are GroovyObjects
// and invoke the method on them if so.
MissingMethodException last = null;
if (isClosureNotOwner && (owner instanceof GroovyObject)) {
try {
return invokeMethodOnGroovyObject(methodName, originalArguments, owner);
} catch (MissingMethodException mme) {
// proboboly needed here, but we need a test case to trip it first
if (last == null) last = mme;
}
catch (InvokerInvocationException iie) {
if (iie.getCause() instanceof MissingMethodException) {
MissingMethodException mme = (MissingMethodException) iie.getCause();
if (methodName.equals(mme.getMethod())) {
if (last == null) last = mme;
} else {
throw iie;
}
}
else
throw iie;
}
}
if (delegate != closure && (delegate instanceof GroovyObject)) {
try {
return invokeMethodOnGroovyObject(methodName, originalArguments, delegate);
} catch (MissingMethodException mme) {
last = mme;
}
catch (InvokerInvocationException iie) {
if (iie.getCause() instanceof MissingMethodException) {
last = (MissingMethodException) iie.getCause();
}
else
throw iie;
}
}
if (last != null) return invokeMissingMethod(object, methodName, originalArguments, last, isCallToSuper);
}
}
}
if (method != null) {
return method.doMethodInvoke(object, arguments);
} else {
return invokePropertyOrMissing(object, methodName, originalArguments, fromInsideClass, isCallToSuper);
}
}
Invokes the given method on the object. |
public Object invokeMissingMethod(Object instance,
String methodName,
Object[] arguments) {
return invokeMissingMethod(instance, methodName, arguments, null, false);
}
|
public Object invokeMissingProperty(Object instance,
String propertyName,
Object optionalValue,
boolean isGetter) {
Class theClass = instance instanceof Class ? (Class)instance : instance.getClass();
CachedClass superClass = theCachedClass;
while(superClass != null && superClass != ReflectionCache.OBJECT_CLASS) {
final MetaBeanProperty property = findPropertyInClassHierarchy(propertyName, superClass);
if(property != null) {
onSuperPropertyFoundInHierarchy(property);
if(!isGetter) {
property.setProperty(instance, optionalValue);
return null;
}
else {
return property.getProperty(instance);
}
}
superClass = superClass.getCachedSuperClass();
}
// got here to property not found, look for getProperty or setProperty overrides
if(isGetter) {
final Class[] getPropertyArgs = {String.class};
final MetaMethod method = findMethodInClassHierarchy(instance.getClass(), GET_PROPERTY_METHOD, getPropertyArgs, this);
if(method != null && method instanceof ClosureMetaMethod) {
onGetPropertyFoundInHierarchy(method);
return method.invoke(instance,new Object[]{propertyName});
}
}
else {
final Class[] setPropertyArgs = {String.class, Object.class};
final MetaMethod method = findMethodInClassHierarchy(instance.getClass(), SET_PROPERTY_METHOD, setPropertyArgs, this);
if(method != null && method instanceof ClosureMetaMethod) {
onSetPropertyFoundInHierarchy(method);
return method.invoke(instance, new Object[]{propertyName, optionalValue});
}
}
try {
if (!(instance instanceof Class)) {
if (isGetter && propertyMissingGet != null) {
return propertyMissingGet.invoke(instance, new Object[]{propertyName});
} else {
if (propertyMissingSet != null)
return propertyMissingSet.invoke(instance, new Object[]{propertyName, optionalValue});
}
}
} catch (InvokerInvocationException iie) {
boolean shouldHandle = isGetter && propertyMissingGet != null;
if (!shouldHandle) shouldHandle = !isGetter && propertyMissingSet != null;
if (shouldHandle && iie.getCause() instanceof MissingPropertyException) {
throw (MissingPropertyException) iie.getCause();
}
throw iie;
}
if (instance instanceof Class && theClass != Class.class) {
final MetaProperty metaProperty = InvokerHelper.getMetaClass(Class.class).hasProperty(instance, propertyName);
if (metaProperty != null)
if (isGetter)
return metaProperty.getProperty(instance);
else {
metaProperty.setProperty(instance, optionalValue);
return null;
}
}
throw new MissingPropertyExceptionNoStack(propertyName, theClass);
}
|
public Object invokeStaticMethod(Object object,
String methodName,
Object[] arguments) {
checkInitalised();
final Class sender = object instanceof Class ? (Class) object : object.getClass();
if (sender != theClass) {
MetaClass mc = registry.getMetaClass(sender);
return mc.invokeStaticMethod(sender, methodName, arguments);
}
if (sender == Class.class) {
return invokeMethod(object, methodName, arguments);
}
if (arguments == null) arguments = EMPTY_ARGUMENTS;
// Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
MetaMethod method = retrieveStaticMethod(methodName, arguments);
// lets try use the cache to find the method
if (method != null) {
MetaClassHelper.unwrap(arguments);
return method.doMethodInvoke(object, arguments);
}
Object prop = null;
try {
prop = getProperty(theClass, theClass, methodName, false, false);
} catch (MissingPropertyException mpe) {
// ignore
}
if (prop instanceof Closure) {
return invokeStaticClosureProperty(arguments, prop);
}
Object[] originalArguments = (Object[]) arguments.clone();
MetaClassHelper.unwrap(arguments);
Class superClass = sender.getSuperclass();
Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
while (superClass != Object.class && superClass != null) {
MetaClass mc = registry.getMetaClass(superClass);
method = mc.getStaticMetaMethod(methodName, argClasses);
if (method != null) return method.doMethodInvoke(object, arguments);
try {
prop = mc.getProperty(superClass, superClass, methodName, false, false);
} catch (MissingPropertyException mpe) {
// ignore
}
if (prop instanceof Closure) {
return invokeStaticClosureProperty(originalArguments, prop);
}
superClass = superClass.getSuperclass();
}
if(prop != null) {
MetaClass propMC = registry.getMetaClass(prop.getClass());
return propMC.invokeMethod(prop, CLOSURE_CALL_METHOD, arguments);
}
return invokeStaticMissingMethod(sender, methodName, arguments);
}
|
protected Object invokeStaticMissingProperty(Object instance,
String propertyName,
Object optionalValue,
boolean isGetter) {
MetaClass mc = instance instanceof Class ? registry.getMetaClass((Class) instance) : this;
if (isGetter) {
MetaMethod propertyMissing = mc.getMetaMethod(STATIC_PROPERTY_MISSING, GETTER_MISSING_ARGS);
if (propertyMissing != null) {
return propertyMissing.invoke(instance, new Object[]{propertyName});
}
} else {
MetaMethod propertyMissing = mc.getMetaMethod(STATIC_PROPERTY_MISSING, SETTER_MISSING_ARGS);
if (propertyMissing != null) {
return propertyMissing.invoke(instance, new Object[]{propertyName, optionalValue});
}
}
if (instance instanceof Class) {
throw new MissingPropertyException(propertyName, (Class) instance);
}
throw new MissingPropertyException(propertyName, theClass);
}
Hook to deal with the case of MissingProperty for static properties. The method will look attempt to look up
"propertyMissing" handlers and invoke them otherwise thrown a MissingPropertyException |
public boolean isGroovyObject() {
return isGroovyObject;
}
|
protected boolean isInitialized() {
return initialized;
}
|
public boolean isModified() {
return false; // MetaClassImpl not designed for modification, just return false
}
|
protected void onGetPropertyFoundInHierarchy(MetaMethod method) {
}
|
protected void onInvokeMethodFoundInHierarchy(MetaMethod method) {
}
|
protected void onMixinMethodFound(MetaMethod method) {
}
|
protected void onSetPropertyFoundInHierarchy(MetaMethod method) {
}
|
protected void onSuperMethodFoundInHierarchy(MetaMethod method) {
}
|
protected void onSuperPropertyFoundInHierarchy(MetaBeanProperty property) {
}
|
public MetaMethod pickMethod(String methodName,
Class[] arguments) {
return getMethodWithoutCaching(theClass, methodName, arguments, false);
}
|
public List respondsTo(Object obj,
String name) {
final Object o = getMethods(getTheClass(), name, false);
if (o instanceof FastArray)
return ((FastArray)o).toList();
else
return Collections.singletonList(o);
}
|
public List respondsTo(Object obj,
String name,
Object[] argTypes) {
Class[] classes = MetaClassHelper.castArgumentsToClassArray(argTypes);
MetaMethod m = getMetaMethod(name, classes);
List methods = new ArrayList();
if (m != null) {
methods.add(m);
}
return methods;
}
|
public Constructor retrieveConstructor(Class[] arguments) {
CachedConstructor constructor = (CachedConstructor) chooseMethod("< init >", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
constructor = (CachedConstructor) chooseMethod("< init >", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
return null;
}
|
protected MetaMethod retrieveMethod(String methodName,
Class[] arguments) {
return pickMethod(methodName, arguments);
} Deprecated! use - pickMethod instead
|
public MetaMethod retrieveStaticMethod(String methodName,
Object[] arguments) {
final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(theClass, methodName);
MetaMethodIndex.CacheEntry cacheEntry;
if (e != null) {
cacheEntry = e.cachedStaticMethod;
if (cacheEntry != null &&
MetaClassHelper.sameClasses(cacheEntry.params, arguments, e.staticMethods instanceof MetaMethod))
{
return cacheEntry.method;
}
cacheEntry = new MetaMethodIndex.CacheEntry ();
final Class[] classes = MetaClassHelper.convertToTypeArray(arguments);
cacheEntry.params = classes;
cacheEntry.method = pickStaticMethod(methodName, classes);
e.cachedStaticMethod = cacheEntry;
return cacheEntry.method;
}
else
return pickStaticMethod(methodName, MetaClassHelper.convertToTypeArray(arguments));
}
|
public int selectConstructorAndTransformArguments(int numberOfConstructors,
Object[] arguments) {
//TODO: that is just a quick prototype, not the real thing!
if (numberOfConstructors != constructors.size()) {
throw new IncompatibleClassChangeError("the number of constructors during runtime and compile time for " +
this.theClass.getName() + " do not match. Expected " + numberOfConstructors + " but got " + constructors.size());
}
if (arguments == null) arguments = EMPTY_ARGUMENTS;
Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
MetaClassHelper.unwrap(arguments);
CachedConstructor constructor = (CachedConstructor) chooseMethod("< init >", constructors, argClasses);
if (constructor == null) {
constructor = (CachedConstructor) chooseMethod("< init >", constructors, argClasses);
}
if (constructor == null) {
throw new GroovyRuntimeException(
"Could not find matching constructor for: "
+ theClass.getName()
+ "(" + InvokerHelper.toTypeString(arguments) + ")");
}
List l = new ArrayList(constructors.toList());
Comparator comp = new Comparator() {
public int compare(Object arg0, Object arg1) {
CachedConstructor c0 = (CachedConstructor) arg0;
CachedConstructor c1 = (CachedConstructor) arg1;
String descriptor0 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c0.getNativeParameterTypes());
String descriptor1 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c1.getNativeParameterTypes());
return descriptor0.compareTo(descriptor1);
}
};
Collections.sort(l, comp);
int found = -1;
for (int i = 0; i < l.size(); i++) {
if (l.get(i) != constructor) continue;
found = i;
break;
}
// NOTE: must be changed to "1 |" if constructor was vargs
return 0 | (found < < 8);
}
|
public void setAttribute(Object object,
String attribute,
Object newValue) {
setAttribute(theClass, object, attribute, newValue, false, false);
}
|
public void setAttribute(Class sender,
Object object,
String attribute,
Object newValue,
boolean useSuper,
boolean fromInsideClass) {
checkInitalised();
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
MetaClass mc = registry.getMetaClass((Class) object);
mc.setAttribute(sender, object, attribute, newValue, useSuper, fromInsideClass);
return;
}
MetaProperty mp = getMetaProperty(sender, attribute, useSuper, isStatic);
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
mp = mbp.getField();
}
if (mp != null) {
mp.setProperty(object, newValue);
return;
}
}
throw new MissingFieldException(attribute, theClass);
}
Sets the given attribute (field) on the given object |
public void setProperties(Object bean,
Map map) {
checkInitalised();
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String key = entry.getKey().toString();
Object value = entry.getValue();
setProperty(bean, key, value);
}
}
Sets a number of bean properties from the given Map where the keys are
the String names of properties and the values are the values of the
properties to set |
public void setProperty(Object object,
String property,
Object newValue) {
setProperty(theClass, object, property, newValue, false, false);
}
|
public void setProperty(Class sender,
Object object,
String name,
Object newValue,
boolean useSuper,
boolean fromInsideClass) {
checkInitalised();
//----------------------------------------------------------------------
// handling of static
//----------------------------------------------------------------------
boolean isStatic = theClass != Class.class && object instanceof Class;
if (isStatic && object != theClass) {
MetaClass mc = registry.getMetaClass((Class) object);
mc.getProperty(sender, object, name, useSuper, fromInsideClass);
return;
}
//----------------------------------------------------------------------
// Unwrap wrapped values fo now - the new MOP will handle them properly
//----------------------------------------------------------------------
if (newValue instanceof Wrapper) newValue = ((Wrapper) newValue).unwrap();
//----------------------------------------------------------------------
// turn setProperty on a Map to put on the Map itself
//----------------------------------------------------------------------
if (!isStatic && this.isMap) {
((Map) object).put(name, newValue);
return;
}
MetaMethod method = null;
Object[] arguments = null;
//----------------------------------------------------------------------
// setter
//----------------------------------------------------------------------
MetaProperty mp = getMetaProperty(sender, name, useSuper, isStatic);
MetaProperty field = null;
if (mp != null) {
if (mp instanceof MetaBeanProperty) {
MetaBeanProperty mbp = (MetaBeanProperty) mp;
method = mbp.getSetter();
MetaProperty f = mbp.getField();
if (method != null || (f != null && !Modifier.isFinal(f.getModifiers()))) {
arguments = new Object[]{newValue};
field = f;
}
} else {
field = mp;
}
}
// check for a category method named like a setter
if (!useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
String getterName = "set" + MetaClassHelper.capitalize(name);
MetaMethod categoryMethod = getCategoryMethodSetter(sender, getterName, false);
if (categoryMethod != null) {
method = categoryMethod;
arguments = new Object[]{newValue};
}
}
//----------------------------------------------------------------------
// listener method
//----------------------------------------------------------------------
boolean ambiguousListener = false;
if (method == null) {
method = (MetaMethod) listeners.get(name);
ambiguousListener = method == AMBIGUOUS_LISTENER_METHOD;
if (method != null &&
!ambiguousListener &&
newValue instanceof Closure) {
// lets create a dynamic proxy
Object proxy = Proxy.newProxyInstance(
theClass.getClassLoader(),
new Class[]{method.getParameterTypes()[0].getTheClass()},
new ConvertedClosure((Closure) newValue, name));
arguments = new Object[]{proxy};
newValue = proxy;
} else {
method = null;
}
}
//----------------------------------------------------------------------
// field
//----------------------------------------------------------------------
if (method == null && field != null) {
if (Modifier.isFinal(field.getModifiers())) {
throw new ReadOnlyPropertyException(name, theClass);
}
field.setProperty(object, newValue);
return;
}
//----------------------------------------------------------------------
// generic set method
//----------------------------------------------------------------------
// check for a generic get method provided through a category
if (method == null && !useSuper && !isStatic && GroovyCategorySupport.hasCategoryInCurrentThread()) {
method = getCategoryMethodSetter(sender, "set", true);
if (method != null) arguments = new Object[]{name, newValue};
}
// the generic method is valid, if available (!=null), if static or
// if it is not static and we do no static access
if (method == null && genericSetMethod != null && !(!genericSetMethod.isStatic() && isStatic)) {
arguments = new Object[]{name, newValue};
method = genericSetMethod;
}
//----------------------------------------------------------------------
// executing the getter method
//----------------------------------------------------------------------
if (method != null) {
if (arguments.length == 1) {
newValue = DefaultTypeTransformation.castToType(
newValue,
method.getParameterTypes()[0].getTheClass());
arguments[0] = newValue;
} else {
newValue = DefaultTypeTransformation.castToType(
newValue,
method.getParameterTypes()[1].getTheClass());
arguments[1] = newValue;
}
method.doMethodInvoke(object, arguments);
return;
}
//----------------------------------------------------------------------
// error due to missing method/field
//----------------------------------------------------------------------
if (ambiguousListener) {
throw new GroovyRuntimeException("There are multiple listeners for the property " + name + ". Please do not use the bean short form to access this listener.");
}
if (mp != null) {
throw new ReadOnlyPropertyException(name, theClass);
}
invokeMissingProperty(object, name, newValue, false);
}
Sets the property value on an object |
public String toString() {
return super.toString() + "[" + theClass + "]";
}
|