How to debug Java source code when I implement a custom detector for Lint?

I am an Android developer. I already developed my own lint rules by running the new XXXDetector and XXXIssueRegistry, here is my snip source code:

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 with the runnig command:

 $gradle lint 

And I will get the output message as I expected in the console.

But I want to debug the source XXXDetector file. How can i do this? If I press "debug" or "run" or "build", my custom lint rules will not run! Therefore, I have to run it in a console that does not support debug. How can i solve this?

+6
source share
1 answer

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

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

Gradle will stop execution until a debugger is connected.

Attach the debugger to the local process:

Attach to local process

And select the appropriate Java process:

Select process

Once the debugger is connected, Gradle will continue to execute, and you can debug your own lint rule:

Debugger window

+1
source

Source: https://habr.com/ru/post/1240595/


All Articles