dimanche 19 mai 2019

Custom Android Lint visiting method reference

I have working example of Detector which visits method calls by reference but it doesn't work for calls by method reference.

class RxOnErrorDetector : Detector(), Detector.UastScanner {
    override fun getApplicableMethodNames() = listOf("onError")

    override fun visitMethod(context: JavaContext, node: UCallExpression, method: PsiMethod) {
        if (context.evaluator.isMemberInClass(method, "io.reactivex.Emitter")) {
            context.report(ISSUE_ON_ERROR_CALL, node, context.getNameLocation(node), "onError method was called.")
        }
    }
}

Code example with method reference call.

public class ObservableMethodReferenceProducer {

    private Listener listener = new Listener();

    public Observable<Integer> produceObservable() {
        return Observable.create(e -> {
            listener.doOnError(e::onError);
        });
    }

    class Listener {
        void doOnError(ErrorListener errorListener) {

        }
    }

    interface ErrorListener {
        void onError(Exception e);
    }
}

What is the best way to detect method reference call of particular type in Android Lint?

Aucun commentaire:

Enregistrer un commentaire