Groovy 反射字符串常量方法

Keywords: Groovy, Reflection, 反射

The Reflection of Groovy String constant style method.

Groovy支持以下的方法定义:

class A {
  def "I am a method"() {
  }
}

Groovy是继承Java的机制的,而Java显然是不支持这种函数定义命名的。然而实际上,你是用A.class.getMethods() 或 A.metaClass.getMethods() 都能获取到带空格的方法名,只是调用起来很麻烦,可能只能靠反射去invoke调用。所以一般这种写法主要用于TestSuite做单元测试或测试驱动开发。其实这个反射很简单,难的是下面的。

Spock是个很灵活的Java/Groovy的测试框架(Spock以后有空再介绍),基于JUnit。但是Spock中继承了Specfication的测试类的字符串常量方法,会在groovy compile的时候compile成一个名字类似$spock_feature_0_0 这样的名字,这时候想要找回原来的对应的常量就不是那么简单了。但既然JUnit能做到,我们就去看JUnit的run的源码,发现它会生成一个Sputnik的Runner,这是个Spock的类,其中它会有个private的SpecInfo的字段,里面包含了当前类的所有features。Spock把每个test method看作一个feature。

因此我只要new一个Sputnik,通过反射把SpecInfo中的信息拿出来,就能拿到按顺序所有排序的方法和对应编译后的方法名了了。

 public static List<String> getAllSpecFeatures(String className) {
        List<String> ret = new ArrayList<>();
        Class klass = Class.forName(className);
        Sputnik runner = new Sputnik(klass);
        Method m = runner.class.getDeclaredMethod("getSpec");
        m.setAccessible(true);
        SpecInfo specInfo = m.invoke(runner);
        List<FeatureInfo> featureInfos = specInfo.getAllFeatures();
        for (FeatureInfo featureInfo : featureInfos) {
            // original groovy method name
            ret.add(featureInfo.getName());
            
            // compiled method name, format like "$spock_feature_0_0"
            println(featureInfo.featureMethod.reflection.name)
        }
        return ret;
    }
原文地址:https://www.cnblogs.com/lhfcws/p/6726654.html