use of groovy.lang.MetaMethod in project groovy by apache.
the class MetaMethodIndex method copyNonPrivateMethods.
private void copyNonPrivateMethods(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 (method.isPrivate())
continue;
if (e == null)
e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
} else {
MetaMethod method = (MetaMethod) oldListOrMethod;
if (!method.isPrivate()) {
Entry e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
}
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MetaMethodIndex method copyNonPrivateNonNewMetaMethods.
private void copyNonPrivateNonNewMetaMethods(Entry from, Header to) {
Object oldListOrMethod = from.methods;
if (oldListOrMethod == null)
return;
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 (method instanceof NewMetaMethod || method.isPrivate())
continue;
if (e == null)
e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
} else {
MetaMethod method = (MetaMethod) oldListOrMethod;
if (method instanceof NewMetaMethod || method.isPrivate())
return;
Entry e = getOrPutMethods(from.name, to);
e.methods = addMethodToList(e.methods, method);
}
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MethodRankHelper method getMethodSuggestionString.
/**
* Returns a string detailing possible solutions to a missing method
* if no good solutions can be found a empty string is returned.
*
* @param methodName the name of the method that doesn't exist
* @param type the class on which the method is invoked
* @param arguments the arguments passed to the method
* @return a string with probable solutions to the exception
*/
public static String getMethodSuggestionString(String methodName, Class type, Object[] arguments) {
ClassInfo ci = ClassInfo.getClassInfo(type);
List<MetaMethod> methods = new ArrayList<MetaMethod>(ci.getMetaClass().getMethods());
methods.addAll(ci.getMetaClass().getMetaMethods());
List<MetaMethod> sugg = rankMethods(methodName, arguments, methods);
StringBuilder sb = new StringBuilder();
if (!sugg.isEmpty()) {
sb.append("\nPossible solutions: ");
for (int i = 0; i < sugg.size(); i++) {
if (i != 0)
sb.append(", ");
sb.append(sugg.get(i).getName()).append("(");
sb.append(listParameterNames(sugg.get(i).getParameterTypes()));
sb.append(")");
}
}
Class[] argumentClasses = getArgumentClasses(arguments);
List<Pair<Class, Class>> conflictClasses = getConflictClasses(sugg, argumentClasses);
if (!conflictClasses.isEmpty()) {
sb.append("\nThe following classes appear as argument class and as parameter class, ");
sb.append("but are defined by different class loader:\n");
boolean first = true;
for (Pair<Class, Class> pair : conflictClasses) {
if (!first) {
sb.append(", ");
} else {
first = false;
}
sb.append(pair.u.getName()).append(" (defined by '");
sb.append(pair.u.getClassLoader());
sb.append("' and '");
sb.append(pair.v.getClassLoader());
sb.append("')");
}
sb.append("\nIf one of the method suggestions matches the method you wanted to call, ");
sb.append("\nthen check your class loader setup.");
}
return sb.toString();
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class MethodRankHelper method getConflictClasses.
private static List<Pair<Class, Class>> getConflictClasses(List<MetaMethod> sugg, Class[] argumentClasses) {
List<Pair<Class, Class>> ret = new LinkedList<Pair<Class, Class>>();
Set<Class> recordedClasses = new HashSet<Class>();
for (MetaMethod method : sugg) {
Class[] para = method.getNativeParameterTypes();
for (Class aPara : para) {
if (recordedClasses.contains(aPara))
continue;
for (Class argumentClass : argumentClasses) {
if (argumentClass == null)
continue;
if (argumentClass == aPara)
continue;
if (argumentClass.getName().equals(aPara.getName())) {
ret.add(new Pair<Class, Class>(argumentClass, aPara));
}
}
recordedClasses.add(aPara);
}
}
return ret;
}
use of groovy.lang.MetaMethod in project groovy by apache.
the class SimpleExtensionModule method getMetaMethods.
@Override
public List<MetaMethod> getMetaMethods() {
List<MetaMethod> metaMethods = new LinkedList<MetaMethod>();
List<Class> extensionClasses = getInstanceMethodsExtensionClasses();
for (Class extensionClass : extensionClasses) {
try {
createMetaMethods(extensionClass, metaMethods, false);
} catch (LinkageError e) {
LOG.warning("Module [" + getName() + "] - Unable to load extension class [" + extensionClass + "] due to [" + e.getMessage() + "]. Maybe this module is not supported by your JVM version.");
}
}
extensionClasses = getStaticMethodsExtensionClasses();
for (Class extensionClass : extensionClasses) {
try {
createMetaMethods(extensionClass, metaMethods, true);
} catch (LinkageError e) {
LOG.warning("Module [" + getName() + "] - Unable to load extension class [" + extensionClass + "] due to [" + e.getMessage() + "]. Maybe this module is not supported by your JVM version.");
}
}
return metaMethods;
}
Aggregations