I'm working on a groovy unit-testing class that contains a collection of rules for whether or not the contents of a file are correctly formatted. (Not using a proper rules engine, it just takes advantage of Groovy's assertion capabilities to do validation in a way that vaguely resembles what a rules engine would do.) I was thinking that I could create a method called FireAllRules that looks like this:
public static void FireAllRules(File file)
{
for(def method in Class.getMethods)
{
if(method.name.indexOf("rule" == 0) method.invoke(file);
}
}
ループで取得するメソッドはすべて静的であり、デバッグ プロセス中に Class.getMethods() 列挙にルール メソッドが含まれていないことに気付きました。理想的には、java.Object に付随する何十もの興味のないメソッドをソートするのではなく、私が個人的にクラスに書き込んだメソッドだけをループしたいと考えています。実行時にこれらの静的メソッドを反復するためにリフレクションを使用する方法はありますか?