How to debug java source code when I implement a custom Detector for Lint?

核能气质少年 提交于 2020-12-27 17:13:55

问题


I am a Android developer. I have already design my own lint rules by implementing new XXXDetector and XXXIssueRegistry, here is my source code snip:

My XXXIssueRegistry file:

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);
  }
}

My XXXDetector file:

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`");
    }
}
}

Now I can run my custom lint rules by runnig command:

$gradle lint

And I will get output message like I expected in console.

But I want to debug my XXXDetector source file. How can I do that? If I click "debug" or "run" or "build" , my custom lint rules will NOT run! So I have to run it in console, which don't support debug. How can I solve this?


回答1:


To debug a custom lint check you need to run a Gradle lint task with -Dorg.gradle.debug=true parameter, for example:

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

The Gradle will stop execution until a debugger is attached.

Attach a debugger to a local process:

And select a corresponding Java process:

After debugger has been attached, the Gradle will continue execution and you will able to debug your custom lint rule:



来源:https://stackoverflow.com/questions/34785161/how-to-debug-java-source-code-when-i-implement-a-custom-detector-for-lint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!