Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

lint

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?

like image 928
ljfxyj2008 Avatar asked Jan 14 '16 08:01

ljfxyj2008


People also ask

How do you check for lint errors?

Tip: You can manage the lint checking feature for your Java, Kotlin, or XML source files in the Default Preferences dialog. Select File > Other Settings > Default Settings, and then in the left pane of the Default Preferences dialog, select Editor > Inspections.

What is lint error android?

A lot of us probably do know what actually Android Lint is, even if we don't know the name. Because it is the thing that shows warning messages in the IDE. It may be warnings or errors that will actually fail the build. The following images shows an example of Lint suggestions.


1 Answers

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:

Attach to local process

And select a corresponding Java process:

Select process

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

Debugger window

like image 95
italankin Avatar answered Sep 28 '22 03:09

italankin