ApiHelper - GcsSloop/ViewSupport GitHub Wiki

版本检查工具,目前主要为 MitionEventHelper 提供支持。

拷贝自 GoogleSource - ApiHelper

在原类的基础上开放了几个方法,用于检查一个类是否拥有某个字段或者某个方法。

方法 摘要
hasField 判断某个类是否拥有某个字段
hasMethod 判断某个类是否拥有某个方法
public class TestApiHelperActivity extends BaseActivity{
    private static final String TAG = "TestApiHelper";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_see_logcat);

        Class testClass = com.gcssloop.test.viewsupport.MyClass.class;

        Log.i(TAG, "-----------------------");

        // 测试某个类是否包含该字段
        Log.i(TAG, "hasField - (T): "+ApiHelper.hasField(testClass, "mSloop"));
        Log.i(TAG, "hasField - (F): "+ApiHelper.hasField(testClass, "mField"));

        Log.i(TAG, "-----------------------");

        // 测试某个类是否包含该方法
        Log.i(TAG, "hasMethod - (T): "+ApiHelper.hasMethod("com.gcssloop.test.viewsupport.MyClass", "MyMethod"));
        Log.i(TAG, "hasMethod - (T): "+ApiHelper.hasMethod(testClass, "MyMethod"));
        Log.i(TAG, "hasMethod - (T): "+ApiHelper.hasMethod(testClass, "MyMethod", String.class));
        Log.i(TAG, "hasMethod - (F): "+ApiHelper.hasMethod(testClass, "MyMethod", int.class));

    }
}

/**
 * 一个被测试的类
 */
class MyClass {
    int mSloop = 8;


    int MyMethod(){
        return 0;
    }

    int MyMethod(String haha){
        return 0;
    }
}

结果:

TestApiHelper: -----------------------
TestApiHelper: hasField - (T): true
TestApiHelper: hasField - (F): false
TestApiHelper: -----------------------
TestApiHelper: hasMethod - (T): true
TestApiHelper: hasMethod - (T): true
TestApiHelper: hasMethod - (T): true
TestApiHelper: hasMethod - (F): false