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.