Hey
Firstly I am quite noob in writing custom lint rules. I have read tutorials, and also watched some youtube videos (including KotlinConf 2017) about creating custom rules.
I have been struggling with writing rule that:
a) Finds if class implements interface
b) Finds if this particular class have a field that is implementing other interface
c) if above conditions are true checks if there is onStop() method overrided and if it is,
there should be another method call on field object.
Problem looks like
class implements classInterface{ // this must implement classInterface
field // this must implement fieldInterface
onStop(){
field.fieldMethodCall(); // field must call method
}
}
So i wrote detector that implements UastScanner
I am able to find class that implements classInterface but I have trouble with determining whenever field implements given interface. I have tried to write visitor and check it on visitClass method but seems like thats not how this tree looks like (Question 2 in the end of this post*).
I think I could write last step logic myself -> onStop() + field that calls method inside
@Override
public List<Class<? extends UElement>> getApplicableUastTypes() {
return Collections.singletonList(UClass.class);
}
@Override
public UElementHandler createUastHandler(JavaContext Jcontext) {
return new UElementHandler() {
@Override
public void visitClass(UClass uClass) {
JavaEvaluator evaluator = Jcontext.getEvaluator();
//get classes that implements interfaces
if (evaluator.implementsInterface(uClass, classInterface, true)) {
//check all fields if they are instances of presenterinterface
UField[] fields = uClass.getFields();
if (fields.length > 0) {
for (UField field : fields) {
//this is where I am stucked
//check if field implements interface
}
}
}
}
}
}
And I have also few other questions;
1) Are you going to create documentation of Uast / Psi apis in future? If yes, when?
2) Is there a repository that cointais current android lint rules sources (migrated to new Uast api) available to developers?
or any other open source projects you know, that contain some rules so I could read it?
3) In one of slides your using method context.uastFile?.asRecursiveLogString() to see whole Log tree. I just can not find this method in java.
