Android自定义Lint规则

时间:2016-01-04 03:31:06

标签: android lint

我正在尝试为我编写的库SDK编写自定义Lint规则。我在库中使用构建器模式,我想用Lint规则实现的是检查用户是否在调用build()之前调用了特定方法。

这是我迄今为止所做的事情 -

具有Builder模式的库的源/ java文件:

public class ShowCaseAPI {

private String regionCode = "";
private String subRegionCode = "";
private String latitude = "";
private String longitude = "";


private String REGION_CODE_KEY = "rc";
private String SUB_REGION_CODE_KEY = "sc";
private String CHANNEL_KEY = "ch";
private String LATITUDE_KEY = "lt";
private String LONGITUDE_KEY = "lg";
private String COMMAND_KEY = "cmd";

private String COMMAND_TYPE = "GETSHOWCASE";
private String CHANNEL_TYPE = "mobile";
private String showCaseUrl = Urls.GET_SHOWCASE_URL;


public ShowCaseAPI setRegionCode(String regionCode) {

    this.regionCode = regionCode;

    return this;
}

public ShowCaseAPI setSubRegionCode(String subRegionCode) {

    this.subRegionCode = subRegionCode;
    return  this;
}

public ShowCaseAPI setLatitude(String latitude) {
    this.latitude = latitude;
    return this;
}

public ShowCaseAPI setLongitude(String longitude) {
    this.longitude = longitude;
    return this;
}

public ShowCaseAPI overrideBaseURL(String newUrl) {

    this.showCaseUrl = newUrl;
    return this;

}

public String buildAPIParameters() {

    Uri builtUri = Uri.parse(showCaseUrl)
                        .buildUpon()
                        .appendQueryParameter(COMMAND_KEY, COMMAND_TYPE)
                        .appendQueryParameter(REGION_CODE_KEY, this.regionCode)
                        .appendQueryParameter(SUB_REGION_CODE_KEY, this.subRegionCode)
                        .appendQueryParameter(LATITUDE_KEY, this.latitude)
                        .appendQueryParameter(LONGITUDE_KEY, this.longitude)
                        .appendQueryParameter(CHANNEL_KEY, CHANNEL_TYPE)
                        .build();

    String showCaseUrl = builtUri.toString();

    return showCaseUrl;

}

我能够提出的Lint代码到现在为止:

@Override
public AstVisitor createJavaVisitor(@NonNull final JavaContext context) {
    return new ForwardingAstVisitor() {

        @Override
        public boolean visitMethodInvocation(MethodInvocation node) {

            String methodName = node.astName().astValue();

            if (methodName.equals("buildAPIParameters")){

                //Here is where I need to check if other methods have been invoked before calling this 

            }

            return super.visitMethodInvocation(node);
        }
    };
}

我试过看源代码中的lint_rules但是却找不到这样的东西。关于如何解决这个问题的任何想法或指示都会非常有用。

0 个答案:

没有答案