我正在编写一个定制的棉绒探测器,以获得乐趣.我正在关于创建一个
custom lint rule in Android的Big Nerd Ranches课程(对于Java应该是一样的)
我可以检测到我想要的发生.类构造函数被调用.但是由于我们正在通过一个Abstract Syntax Tree穿行
我没有检测到回调的实现.我不知道如何告诉Java来检查文件,只报告是否找不到出现.在这种情况下,接口实现.因为我一次只看到一片叶子.
如何查找两次事件,存储每次发生的位置,然后执行我的逻辑并相应地进行报告?
package com.bignerdranch.linette.detectors;
import com.android.annotations.NonNull;
import com.android.tools.lint.detector.api.Category;
import com.android.tools.lint.detector.api.Context;
import com.android.tools.lint.detector.api.Detector;
import com.android.tools.lint.detector.api.Implementation;
import com.android.tools.lint.detector.api.Issue;
import com.android.tools.lint.detector.api.JavaContext;
import com.android.tools.lint.detector.api.Location;
import com.android.tools.lint.detector.api.Scope;
import com.android.tools.lint.detector.api.Severity;
import com.android.tools.lint.detector.api.TextFormat;
import java.io.File;
import java.util.EnumSet;
import java.util.List;
import lombok.ast.AstVisitor;
import lombok.ast.Node;
/**
* Lint check for the usage of to-do statements
*/
public class CallBackDetector extends Detector implements Detector.JavaScanner {
private static final String FRAGMENT_MATCHER_STRING = "NoInternetDialogFragment()";
private static final String INTERFACE_MATCHER_STRING =
"NoInternetDialogFragment.NoInternetCallbackInterface";
private static final Class extends Detector> DETECTOR_CLASS = CallBackDetector.class;
private static final EnumSet DETECTOR_SCOPE = Scope.JAVA_FILE_SCOPE;
private static final Implementation IMPLEMENTATION = new Implementation(
DETECTOR_CLASS,
DETECTOR_SCOPE
);
private static final String ISSUE_ID = "NoInternetDialogFragment";
private static final String ISSUE_DESCRIPTION =
"NoInternetDialogFragment Callback not detected";
private static final String ISSUE_EXPLANATION =
"When using NoInternetDialogFragment you must implement its' callback -- "
+ "NoInternetCallbackInterface";
private static final Category ISSUE_CATEGORY = Category.CORRECTNESS;
private static final int ISSUE_PRIORITY = 10;
private static final Severity ISSUE_SEVERITY = Severity.ERROR;
public static final Issue ISSUE = Issue.create(
ISSUE_ID,
ISSUE_DESCRIPTION,
ISSUE_EXPLANATION,
ISSUE_CATEGORY,
ISSUE_PRIORITY,
ISSUE_SEVERITY,
IMPLEMENTATION
);
/**
* Constructs a new {@link CallBackDetector} check
*/
public CallBackDetector() {
}
@Override
public boolean appliesTo(@NonNull Context context, @NonNull File file) {
return true;
}
@Override
public List> getApplicableNodeTypes() {
return null;
}
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
String source = context.getContents();
// Check validity of source
if (source == null) {
return null;
}
if(source.indexOf(INTERFACE_MATCHER_STRING) >=0){
return null;
}
int index = source.indexOf(FRAGMENT_MATCHER_STRING);
for (int i = index; i >= 0; i = source.indexOf(FRAGMENT_MATCHER_STRING, i + 1)) {
Location location = Location.create(context.file, source, i,
i + FRAGMENT_MATCHER_STRING.length());
context.report(ISSUE, location, ISSUE.getBriefDescription(TextFormat.TEXT));
}
return null;
}
}