构造函数中注射次数的Lint规则

问题描述 投票:1回答:2

我正在尝试在我的Android代码中创建一个Lint规则来检查构造函数中的注入数量,所以如果我超过视图模型的某个数字,我会提出一个lint警告。

我知道我必须在我的Lint Detector中实现UastScanner,但我迷路了,因为我找不到好文档。还有其他人做过这样的事吗?或者我在哪里可以找到关于它的好消息?

谢谢!

android dependency-injection dagger-2 lint
2个回答
2
投票

*注意 - 阅读已编辑解决方案的完整答案。 *

我能够像这样编写UAST转换:

public class NumberOfDependenciesInjectedDetector extends Detector implements Detector.UastScanner {

    private static final int NUMBER_OF_INJECTIONS_ALLOWED = 5;
    private static final Class<? extends Detector> DETECTOR = NumberOfDependenciesInjectedDetector.class;
    private static final EnumSet<Scope> SCOPE = Scope.JAVA_FILE_SCOPE;
    private static final Implementation IMPLEMENTATION = new Implementation(DETECTOR, SCOPE);

    public static final Issue ISSUE = Issue.create(
            "NumberOfDependenciesInjected",
            "Limit number of injections in a class via constructor",
            "A longer description here",
            Category.CORRECTNESS,
            10,
            Severity.ERROR,
            IMPLEMENTATION
    );

    @Override
    public List<Class<? extends UElement>> getApplicableUastTypes() {
        return Collections.<Class<? extends UElement>>singletonList(UClass.class);
    }

    @Override
    public UElementHandler createUastHandler(JavaContext context) {
        return new ConstructorVisitor(context);
    }

    private static class ConstructorVisitor extends UElementHandler {

        private JavaContext javaContext;

        private ConstructorVisitor(JavaContext javaContext) {
            this.javaContext = javaContext;
        }

        @Override
        public void visitClass(UClass clazz){
            UMethod[] methods = clazz.getMethods();

            for(UMethod method : methods){
                if(!method.isConstructor()) continue;

                if (method.getParameterList().getParametersCount() > NUMBER_OF_INJECTIONS_ALLOWED) {
                    javaContext.report(ISSUE, method, javaContext.getLocation(method), "Injections exceed allowed value of " + NUMBER_OF_INJECTIONS_ALLOWED);
                }
            }
        }
    }
}

但是,似乎这仍然没有拿起Kotlin源文件......非常令人困惑。

编辑:12/19/17 - 固定

问题是双重的:

1)确实隐藏了一种阻止检查工作的Psi方法。 visitClass方法不应该使用getParameterList(),而是使用getUastParameters()。将上面的visitclass替换为:

@Override
public void visitClass(UClass clazz){
    UMethod[] methods = clazz.getMethods();

    for(UMethod method : methods){
        if(!method.isConstructor()) continue;

        if (method.getUastParameters().size() > NUMBER_OF_INJECTIONS_ALLOWED) {
            javaContext.report(ISSUE, clazz, javaContext.getLocation(method), "Injections exceed allowed value of " + NUMBER_OF_INJECTIONS_ALLOWED);
        }
    }
}

2)在与lint-dev小组直接与Tor Norbye交谈之后,他指出Android Studio 3.0实际上lint在外部不能与kotlin一起工作,因此预计不会像这里描述的那样工作。升级到Android Studio 3.1 Canary并运行linter产生了预期的报告。


0
投票

所以我能够使用JavaScanner找到解决方案,但我还没有找到任何使用UastScanner的东西(我想要使用它,因为我们也有Kotlin类):

public class NumberOfDependenciesInjectedDetector extends Detector implements Detector.JavaScanner {

    private static final int NUMBER_OF_INJECTIONS_ALLOWED = 5;
    private static final Class<? extends Detector> DETECTOR = NumberOfDependenciesInjectedDetector.class;
    private static final EnumSet<Scope> SCOPE = Scope.JAVA_FILE_SCOPE;
    private static final Implementation IMPLEMENTATION = new Implementation(DETECTOR, SCOPE);

    public static final Issue ISSUE = Issue.create(
            "NumberOfDependenciesInjected",
            "Limit number of injections in a class via constructor",
            "A longer description here",
            Category.CORRECTNESS,
            10,
            Severity.ERROR,
            IMPLEMENTATION
    );

    @Override
    public boolean appliesTo(Context context, File file) {
        return true;
    }

    @Override
    public Speed getSpeed(Issue issue) {
        return Speed.FAST;
    }

    @Override
    public List<Class<? extends Node>> getApplicableNodeTypes() {
        return Collections.<Class<? extends Node>>singletonList(ConstructorDeclaration.class);
    }

    @Override
    public AstVisitor createJavaVisitor(JavaContext context) {
        return new ConstructorVisitor(context);
    }

    private static class ConstructorVisitor extends ForwardingAstVisitor {

        private JavaContext javaContext;

        private ConstructorVisitor(JavaContext javaContext) {
            this.javaContext = javaContext;
        }

        @Override
        public boolean visitConstructorDeclaration(ConstructorDeclaration node) {
            if (node.astParameters().size() > NUMBER_OF_INJECTIONS_ALLOWED) {
                javaContext.report(ISSUE, node, javaContext.getLocation(node), "My message goes here");
                return true;
            }
            return false;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.