use of groovy.lang.MetaMethod in project groovy by apache.
the class ClosureMetaMethod method createMethodList.
public static List<MetaMethod> createMethodList(final String name, final Class declaringClass, final Closure closure) {
List<MetaMethod> res = new ArrayList<MetaMethod>();
if (closure instanceof MethodClosure) {
MethodClosure methodClosure = (MethodClosure) closure;
Object owner = closure.getOwner();
Class ownerClass = (Class) (owner instanceof Class ? owner : owner.getClass());
for (CachedMethod method : ReflectionCache.getCachedClass(ownerClass).getMethods()) {
if (method.getName().equals(methodClosure.getMethod())) {
MetaMethod metaMethod = new MethodClosureMetaMethod(name, declaringClass, closure, method);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
} else {
if (closure instanceof GeneratedClosure) {
for (CachedMethod method : ReflectionCache.getCachedClass(closure.getClass()).getMethods()) {
if (method.getName().equals("doCall")) {
MetaMethod metaMethod = new ClosureMetaMethod(name, declaringClass, closure, method);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
} else {
MetaMethod metaMethod = new AnonymousMetaMethod(closure, name, declaringClass);
res.add(adjustParamTypesForStdMethods(metaMethod, name));
}
}
return res;
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MetaMethodIndex method copyAllMethodsToSuper.
private void copyAllMethodsToSuper(Entry from, Header to) {
Object oldListOrMethod = from.methods;
if (oldListOrMethod instanceof FastArray) {
FastArray oldList = (FastArray) oldListOrMethod;
Entry e = null;
int len1 = oldList.size();
Object[] list = oldList.getArray();
for (int j = 0; j != len1; ++j) {
MetaMethod method = (MetaMethod) list[j];
if (e == null)
e = getOrPutMethods(from.name, to);
e.methodsForSuper = addMethodToList(e.methodsForSuper, method);
}
} else {
MetaMethod method = (MetaMethod) oldListOrMethod;
Entry e = getOrPutMethods(from.name, to);
e.methodsForSuper = addMethodToList(e.methodsForSuper, method);
}
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MetaMethodIndex method addMethodToList.
public Object addMethodToList(Object o, MetaMethod method) {
if (o == null) {
return method;
}
if (o instanceof MetaMethod) {
MetaMethod match = (MetaMethod) o;
if (!isMatchingMethod(match, method)) {
FastArray list = new FastArray(2);
list.add(match);
list.add(method);
return list;
} else {
if (match.isPrivate() || (!isNonRealMethod(match) && match.getDeclaringClass().isInterface() && !method.getDeclaringClass().isInterface())) {
// do not overwrite interface methods with instance methods
// do not overwrite private methods
// Note: private methods from parent classes are not shown here,
// but when doing the multimethod connection step, we overwrite
// methods of the parent class with methods of a subclass and
// in that case we want to keep the private methods
} else {
CachedClass methodC = method.getDeclaringClass();
CachedClass matchC = match.getDeclaringClass();
if (methodC == matchC) {
if (isNonRealMethod(method)) {
return method;
}
} else if (!methodC.isAssignableFrom(matchC.getTheClass())) {
return method;
}
}
}
return o;
}
if (o instanceof FastArray) {
FastArray list = (FastArray) o;
int found = findMatchingMethod(list, method);
if (found == -1) {
list.add(method);
} else {
MetaMethod match = (MetaMethod) list.get(found);
if (match == method)
return o;
if (match.isPrivate() || (!isNonRealMethod(match) && match.getDeclaringClass().isInterface() && !method.getDeclaringClass().isInterface())) {
// do not overwrite interface methods with instance methods
// do not overwrite private methods
// Note: private methods from parent classes are not shown here,
// but when doing the multimethod connection step, we overwrite
// methods of the parent class with methods of a subclass and
// in that case we want to keep the private methods
} else {
CachedClass methodC = method.getDeclaringClass();
CachedClass matchC = match.getDeclaringClass();
if (methodC == matchC) {
if (isNonRealMethod(method)) {
list.set(found, method);
}
} else if (!methodC.isAssignableFrom(matchC.getTheClass())) {
list.set(found, method);
}
}
}
}
return o;
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MethodSelectionException method appendMethods.
private void appendMethods(StringBuilder buffer) {
for (int i = 0; i < methods.size; i++) {
buffer.append("\n ");
Object methodOrConstructor = methods.get(i);
if (methodOrConstructor instanceof MetaMethod) {
MetaMethod method = (MetaMethod) methodOrConstructor;
buffer.append(Modifier.toString(method.getModifiers()));
buffer.append(" ").append(method.getReturnType().getName());
buffer.append(" ").append(method.getDeclaringClass().getName());
buffer.append("#");
buffer.append(method.getName());
appendClassNames(buffer, method.getNativeParameterTypes());
} else {
CachedConstructor method = (CachedConstructor) methodOrConstructor;
buffer.append(Modifier.toString(method.cachedConstructor.getModifiers()));
buffer.append(" ").append(method.cachedConstructor.getDeclaringClass().getName());
buffer.append("#<init>");
appendClassNames(buffer, method.getNativeParameterTypes());
}
}
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MethodRankHelper method rankMethods.
/**
* Returns a sorted(ranked) list of a selection of the methods among candidates which
* most closely resembles original.
*
* @param name
* @param original
* @param methods
* @return a sorted lists of Methods
*/
private static List<MetaMethod> rankMethods(String name, Object[] original, List<MetaMethod> methods) {
List<RankableMethod> rm = new ArrayList<RankableMethod>(methods.size());
if (original == null)
original = EMPTY_OBJECT_ARRAY;
Class[] ta = new Class[original.length];
Class nullC = NullObject.class;
for (int i = 0; i < original.length; i++) {
//All nulls have to be wrapped so that they can be compared
ta[i] = original[i] == null ? nullC : original[i].getClass();
}
for (MetaMethod m : methods) {
rm.add(new RankableMethod(name, ta, m));
}
Collections.sort(rm);
List<MetaMethod> l = new ArrayList<MetaMethod>(rm.size());
for (RankableMethod m : rm) {
if (l.size() > MAX_RECOMENDATIONS)
break;
if (m.score > MAX_METHOD_SCORE)
break;
l.add(m.m);
}
return l;
}
Aggregations