Code with Finding: |
class CallGraph {
/**
* Finds a function with the given name. Throws an exception if
* there are no functions or multiple functions with the name. This is
* for testing purposes only.
*/
@VisibleForTesting
public Function getUniqueFunctionWithName(final String desiredName) {
Collection<Function> functions =
Collections2.<Function>filter(getAllFunctions(),
new Predicate<Function>() {
public boolean apply(Function function) {
String functionName = function.getName();
// Anonymous functions will have null names,
// so it is important to handle that correctly here
if (functionName != null && desiredName != null) {
return desiredName.equals(functionName);
} else {
return desiredName == functionName;
}
}
});
if (functions.size() == 1) {
return functions.iterator().next();
} else {
throw new IllegalStateException("Found " + functions.size()
+ " functions with name " + desiredName);
}
}
}
|