use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy by apache.
the class MixinInMetaClass method staticMethod.
private static void staticMethod(final MetaClass self, List<MetaMethod> arr, final CachedMethod method) {
CachedClass[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 0)
return;
NewInstanceMetaMethod metaMethod;
if (paramTypes[0].isAssignableFrom(self.getTheClass())) {
if (paramTypes[0].getTheClass() == self.getTheClass())
metaMethod = new NewInstanceMetaMethod(method);
else
metaMethod = new NewInstanceMetaMethod(method) {
public CachedClass getDeclaringClass() {
return ReflectionCache.getCachedClass(self.getTheClass());
}
};
arr.add(metaMethod);
} else {
if (self.getTheClass().isAssignableFrom(paramTypes[0].getTheClass())) {
metaMethod = new NewInstanceMetaMethod(method);
arr.add(metaMethod);
}
}
}
use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.
the class SimpleExtensionModule method createMetaMethods.
private void createMetaMethods(final Class extensionClass, final List<MetaMethod> metaMethods, final boolean isStatic) {
CachedClass cachedClass = ReflectionCache.getCachedClass(extensionClass);
CachedMethod[] methods = cachedClass.getMethods();
for (CachedMethod method : methods) {
if (method.isStatic() && method.isPublic() && method.getParamsCount() > 0) {
// an extension method is found
metaMethods.add(isStatic ? new NewStaticMetaMethod(method) : new NewInstanceMetaMethod(method));
}
}
}
use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.
the class NewStaticMetaMethodTest method testInvokeMetaMethod.
public void testInvokeMetaMethod() throws Exception {
Method method = getClass().getMethod("dummyMethod", new Class[] { String.class, String.class });
assertTrue("Should have found a method", method != null);
NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
Object answer = metaMethod.invoke("abc", new Object[] { "xyz" });
assertEquals("def", answer);
assertTrue("Should not appear as static method", !metaMethod.isStatic());
}
use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.
the class MetaClassImpl method addNewInstanceMethod.
/**
*Adds an instance method to this metaclass.
*
* @param method The method to be added
*/
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()));
}
use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.
the class MetaClassImpl method getProperties.
/**
* Get all the properties defined for this type
*
* @return a list of MetaProperty objects
*/
public List<MetaProperty> getProperties() {
checkInitalised();
SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
if (propertyMap == null) {
// GROOVY-6903: May happen in some special environment, like under Android, due
// to classloading issues
propertyMap = new SingleKeyHashMap();
}
// 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;
}
Aggregations