0

I am trying to write a custom Lint Rule for a library SDK which I have written. I use the builder pattern in the library, what I want to achieve with the Lint Rule is to check if the user has invoked a particular method before calling build().

Here is what I have been able to do till now -

The source/java file for the library with the Builder pattern :

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;

}

The Lint code I have been able to come up with till now :

@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);
        }
    };
}

I have tried looking at the lint_rules in the source code but have not been able to find something like this. Any ideas or pointers as to how to go about this would be really helpful.

Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
  • 1
    I don't think it is the role of lint to make sure you call your methods in the right order. It is rather to the API to make sure it is not ambiguous, or that it accepts several configurations – njzk2 Jan 04 '16 at 03:41
  • Yes, I realize I could just write checks in the library code. I just was wondering if lint could help, looks like I am trying to use Lint the wrong way. – Adnan Mulla Jan 04 '16 at 03:44
  • See my question on this topic http://stackoverflow.com/questions/40133113/writing-custom-lint-warning-to-check-for-custom-annotation – Adam Oct 21 '16 at 08:11

0 Answers0