当我为Lint实现自定义Detector时如何调试java源代码?

时间:2016-01-14 08:52:30

标签: java android lint

我是Android开发人员。 我已经通过实现新的XXXDetector和XXXIssueRegistry设计了我自己的lint规则,这是我的源代码片段:

我的XXXIssueRegistry文件:

public class MyIssueRegistry extends IssueRegistry {
  @Override
  public List<Issue> getIssues() {

    System.out.println("!!!!!!!!!!!!! ljf MyIssueRegistry lint rules works");
    return Arrays.asList(AttrPrefixDetector.ISSUE,
            LoggerUsageDetector.ISSUE);
  }
}

我的XXXDetector文件:

public class LoggerUsageDetector extends Detector
    implements Detector.ClassScanner {
public static final Issue ISSUE = Issue.create("LogUtilsNotUsed",
        "You must use our `LogUtils`",
        "Logging should be avoided in production for security and performance reasons. Therefore, we created a LogUtils that wraps all our calls to Logger and disable them for release flavor.",
        Category.MESSAGES,
        9,
        Severity.ERROR,
        new Implementation(LoggerUsageDetector.class,
                Scope.CLASS_FILE_SCOPE));

@Override
public List<String> getApplicableCallNames() {
    return Arrays.asList("v", "d", "i", "w", "e", "wtf");
}

@Override
public List<String> getApplicableMethodNames() {
    return Arrays.asList("v", "d", "i", "w", "e", "wtf");
}

@Override
public void checkCall(@NonNull ClassContext context,
                      @NonNull ClassNode classNode,
                      @NonNull MethodNode method,
                      @NonNull MethodInsnNode call) {
    String owner = call.owner;
    if (owner.startsWith("android/util/Log")) {
        context.report(ISSUE,
                method,
                call,
                context.getLocation(call),
                "You must use our `LogUtils`");
    }
}
}

现在我可以通过runnig命令运行我的自定义lint规则:

$gradle lint

我会在控制台中获得输出消息。

但我想调试我的XXXDetector源文件。我怎样才能做到这一点? 如果我点击&#34; debug&#34;或&#34;运行&#34;或&#34;构建&#34; ,我的自定义lint规则不会运行!所以我必须在控制台中运行它,它不支持调试。 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

要调试自定义皮棉检查,您需要使用-Dorg.gradle.debug=true参数运行Gradle皮棉任务,例如:

./gradlew --no-daemon -Dorg.gradle.debug=true lintDebug

Gradle将停止执行,直到连接了调试器。

将调试器附加到本地进程:

Attach to local process

并选择相应的Java进程:

Select process

连接调试器后,Gradle将继续执行,您将能够调试自定义的lint规则:

Debugger window