Thank you for this great tool, I've been using it for almost a year now :) recently, I've been facing this problem when writing a custom rule, could it be a bug or maybe we are not supposed to check use instance of when visiting a method? Taking the example method I change it to the following:
/**
* Sample detector showing how to analyze Kotlin/Java code.
* This example flags all string literals in the code that contain
* the word "lint".
*/
public class SampleCodeDetector extends Detector implements UastScanner {
/**
* Issue describing the problem and pointing to the detector implementation
*/
public static final Issue ISSUE = Issue.create(
// ID: used in @SuppressLint warnings etc
"ShortUniqueId",
// Title -- shown in the IDE's preference dialog, as category headers in the
// Analysis results window, etc
"Lint Mentions",
// Full explanation of the issue; you can use some markdown markup such as
// `monospace`, *italic*, and **bold**.
"This check highlights string literals in code which mentions " +
"the word `lint`. Blah blah blah.\n" +
"\n" +
"Another paragraph here.\n",
Category.CORRECTNESS,
6,
Severity.WARNING,
new Implementation(
SampleCodeDetector.class,
Scope.JAVA_FILE_SCOPE));
@Override
public List<Class<? extends UElement>> getApplicableUastTypes() {
return Collections.singletonList(UMethod.class);
}
@Override
public UElementHandler createUastHandler(JavaContext context) {
// Not: Visiting UAST nodes is a pretty general purpose mechanism;
// Lint has specialized support to do common things like "visit every class
// that extends a given super class or implements a given interface", and
// "visit every call site that calls a method by a given name" etc.
// Take a careful look at UastScanner and the various existing lint check
// implementations before doing things the "hard way".
// Also be aware of context.getJavaEvaluator() which provides a lot of
// utility functionality.
return new UElementHandler() {
@Override
public void visitMethod(UMethod node) {
// boolean isKotlinFunction = node instanceof KotlinUMethod;
context.report(ISSUE, node, context.getNameLocation(node),
"This is a method");
}
};
}
}
This rule is quite straight forward, it just highlights every method, and it works well with Android Studio. But as soon as I uncomment
\\boolean isKotlinFunction = node instanceof KtFunction; The report in Android Studio stops working. Seems like it breaks when I check KotlinUMethod but if I check JavaUMethod it doesn't break.
I'm using Android Studio 3.1.2(MacOS), Lint version: 26.1.2 and Gradle Plugin Version: 3.1.2.
Thank you!
Regards,
Daniel.