gpt4 book ai didi

java - 每当有特定注释时如何使 Checkstyle 忽略丢失的 JavaDoc

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:14 26 4
gpt4 key购买 nike

我想自定义 Checkstyle JavadocVariable规则,这样它就不会提示具有 @FindBy 注释的字段。

class Demo{

@FindBy(id = "new-button")
public WebElement createButton; //<- no JavaDoc required because it is a field "injected" by selenium

public String otherField; //<- complain about missing Java doc
}

但我不知道如何在 checkstyle.xml 文件中指定它。有人有想法吗?

在此用例中不起作用的事情:

  • 修改checked类也是无解!
  • SuppressWithNearbyCommentFilter 不会起作用,因为它是注释而不是注释

最佳答案

我知道几种解决方案,但所有这些都需要额外的工作。

  1. 实现自己的 JavadocVariableCheck,在提供注释的情况下可以跳过检查。
  2. 实现 checkstyle 过滤器(如 SuppressWarningsHolder + SuppressWarningsFilter 但具有注释支持)
  3. 或者实现简单的过滤器,扫描 @FindBy 并忽略它后面的两行。

我的解决方案(最简单的一个):

package org.aap.checks;

import com.google.common.collect.Lists;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class SuppressWithAnnotationFilter
extends AutomaticBean
implements Filter {

public class Tag implements Comparable<Tag> {
private final int firstLine;
private final int lastLine;

public Tag(int firstLine, int lastLine) {
this.firstLine = firstLine;
this.lastLine = lastLine;
}

@Override
public int compareTo(Tag other) {
if (firstLine == other.firstLine) {
return lastLine - other.lastLine;
}
return (firstLine - other.firstLine);
}

public boolean isMatch(AuditEvent event) {
final int line = event.getLine();
return line >= firstLine && line <= lastLine;
}

@Override
public final String toString() {
return "Tag[lines=" + firstLine + " to " + lastLine + "]";
}
}

private final List<Tag> tags = Lists.newArrayList();
private WeakReference<FileContents> fileContentsReference =
new WeakReference<FileContents>(null);

public FileContents getFileContents() {
return fileContentsReference.get();
}

public void setFileContents(FileContents fileContents) {
fileContentsReference = new WeakReference<FileContents>(fileContents);
}

@Override
public boolean accept(AuditEvent event) {
if (event.getLocalizedMessage() == null) {
return true; // A special event.
}

final FileContents currentContents = FileContentsHolder.getContents();
if (currentContents == null) {
return true;
}
if (getFileContents() != currentContents) {
setFileContents(currentContents);
tagSuppressions();
}
for (final Iterator<Tag> iter = tags.iterator(); iter.hasNext(); ) {
final Tag tag = iter.next();
if (tag.isMatch(event)) {
return false;
}
}
return true;
}

private void tagSuppressions() {
tags.clear();
final FileContents contents = getFileContents();

String[] contentsLines = contents.getLines();
for (int i = 0; i < contentsLines.length; i++) {
if (contentsLines[i].contains("@FindBy")) {
tags.add(new Tag(i+1, i+2));
}
}

Collections.sort(tags);
}
}

将生成的类添加到 checkstyle 任务的类路径中,然后在 checkstyle.xml 中指定过滤器:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
....
<!-- your Check goes here -->
<module name="org.aap.checks.SuppressWithAnnotationFilter"/>
....
</module>
</module>

关于java - 每当有特定注释时如何使 Checkstyle 忽略丢失的 JavaDoc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28360943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com