要导入什么来使用SuppressFBWarnings?我通过help/install新软件安装了findbugs插件当我输入import edu.时,我无法通过ctrl空间来获取选项.
例
try {
String t = null;
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value="NP_ALWAYS_NULL",
justification="I know what I'm doing")
int sl = t.length();
System.out.printf( "Length is %d", sl );
} catch (Throwable e) {
...
}
Run Code Online (Sandbox Code Playgroud)
有错误"edu无法解析为某种类型"
如何为Android设置SpotBugs?
我尝试遵循官方文档和gradle插件的文档,但Android的设置不完整且令人困惑,并且无法正常工作.
我尝试了以下设置.
build.gradle(项目):
buildscript {
repositories {
// ...
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
// ...
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.4"
}
}
Run Code Online (Sandbox Code Playgroud)
build.gradle(app):
//...
apply plugin: "com.github.spotbugs"
android {
// ...
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
}
// ...
spotbugs {
toolVersion = "3.1.3"
ignoreFailures = true
reportsDir = file("$project.buildDir/findbugsReports")
effort = "max"
reportLevel = "high"
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
// What do I need to do here?
}
Run Code Online (Sandbox Code Playgroud)
我试过运行它 …
在我的 Spring Boot 应用程序中,我使用com.github.spotbugs:spotbugs-maven-plugin
插件。Spotbug 检查报告以下类没有问题:
@Service
public class FooService {
@Autowired
CocoComponent cocoComponent;
@PostConstruct
public void init() {
System.out.println(cocoComponent.getGreeting() + " world!");
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用。然而,由于自动装配的字段并不意味着在注入后会发生变化,所以我更愿意将它们声明为最终的。像这样:
@Service
public class BarService {
final CocoComponent cocoComponent;
public BarService(CocoComponent cocoComponent) {
this.cocoComponent = cocoComponent;
}
@PostConstruct
public void init() {
System.out.println(cocoComponent.getGreeting() + " world!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,spotbugs 检查BarService
课堂上的报告问题:
[ERROR] Medium: new xxx.nnn.BarService(CocoComponent) may expose internal representation by storing an externally mutable object into BarService.cocoComponent [xxx.nnn.BarService] At BarService.java:[line 14] EI_EXPOSE_REP2
Run Code Online (Sandbox Code Playgroud)
当然,我可以: …
当前,我们正在使用Java Compiler 11,并将主要工件部署到Java11。在这里没有问题。
不幸的是,我们使用的服务仅支持Java 8,因此我们针对Java 8编译了其中的一些。
我们的问题是,开发人员可能会引用Java 8在运行时不可用的方法,例如List.of()
,Optional::stream
等。javac
版本11会将其编译为Java 8,但是在JVM版本8上执行时将引发异常。
后者很容易通过一个简单的grep语句来识别,但是后者则比较棘手,需要理解代码/ AST。
我没有检查Checkstyle,Spotbugs和PMD的文档。IntelliJ实际上非常擅长于此,但是它无法集成到我们的CI管道中。
我们得到了这个天真/简单的grep语句:
grep --recursive --extended-regexp '[ \(](List|Set|Map).of' 'our_project'
Run Code Online (Sandbox Code Playgroud)
我们希望有一种准确的方法来识别不兼容的API。
我正在使用 Gradle 5.2、spotbugs-gradle-plugin 版本 2.0.0 并尝试在我的项目上生成 SpotBugs 报告。我在我的项目中使用了以下配置,但它总是创建 XML 报告而不是 html 报告。
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:+'
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0"
}
}
allprojects {
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.github.spotbugs'
group = 'com.myproject'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile('com.google.cloud:google-cloud-storage:+') {
exclude group: "com.google.guava", module: "guava"
}
compile group: 'org.apache.commons', name: 'commons-collections4', version: '+'
compile group: 'com.google.guava', name: 'guava', version: '+'
testImplementation('org.junit.jupiter:junit-jupiter:+') …
Run Code Online (Sandbox Code Playgroud) 我正在使用 FileWrite 类写入文件。它工作正常。但 FindBugs 指出了我的代码片段中的一个小问题。
代码片段:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt";
FileWriter writer = null;
try {
File root = new File(Environment.getExternalStorageDirectory(), "Test");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, fileName);
writer = new FileWriter(gpxfile, true);
writer.append(text + "\n\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} …
Run Code Online (Sandbox Code Playgroud) 是否有一个 checkstyle 规则可以捕获这样的内容:
double result = someInt / someOtherInt;
Run Code Online (Sandbox Code Playgroud)
result
是 double (所以显然需要分数)但右侧会进行整数除法(向下取整)。
这样的东西存在吗?
我是 gradle 新手,正在尝试配置 Spotbugs。我已将该插件添加到 build.gradle 中,并且出现了 Spotbugs 问题。不过,我想排除 Findbugs EI_EXPOSE_REP 和 EI_EXPOSE_REP2 规则,因为它们会出现在我的所有 getter 和 setter 中。我在 build.gradle 中有以下代码片段:
apply plugin: 'java'
apply plugin: 'com.github.spotbugs'
apply plugin: 'findbugs'
spotbugs {
toolVersion = '5.0.0'
}
tasks.withType(SpotBugsTask) {
reports {
xml.enabled = false
html.enabled = true
}
}
findbugs {
excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
toolVersion = "3.0.1"
effort = "max"
}
Run Code Online (Sandbox Code Playgroud)
exceptFilter.xml 包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Bug pattern="EI_EXPOSE_REP"/>
</Match>
<Match>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>
</FindBugsFilter>
Run Code Online (Sandbox Code Playgroud)
我还尝试添加排除,如下所示:
tasks.withType(FindBugs) {
excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
} …
Run Code Online (Sandbox Code Playgroud) 我使用SpotBugs Maven插件进行静态分析,我想从检查中排除目录。查看spotbugs:check
目标文档,似乎无法通过这种方式配置插件。我还检查了SpotBugs 过滤器文件的文档。
在Apache Maven PMD插件中,这可以通过使用excludeRoots参数来完成:
<excludeRoots>
<excludeRoot>target</excludeRoot>
</excludeRoots>
Run Code Online (Sandbox Code Playgroud)
是否可以从SpotBugs检查中排除目录?
运行 Spot Bug 时出现错误“执行 com.github.spotbugs.snom.internal.SpotBugsRunnerForWorker$SpotBugsExecutor 时发生故障”
Gradle 版本:- 6.6.1 SpotBug 插件- 4.2
plugins {
id 'java'
id 'io.quarkus'
id "com.github.spotbugs" version "4.2.0"
}
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/main/resources'
}
}
}
dependencies {
compile "org.apache.commons:commons-lang3:3.11"
compile "org.jboss.logmanager:log4j2-jboss-logmanager"
compile 'org.jboss.slf4j:slf4j-jboss-logging'
compile 'org.jboss.logging:commons-logging-jboss-logging'
spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.10.1'
}
compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
spotbugs {
toolVersion = '4.2.0'
}
Run Code Online (Sandbox Code Playgroud) spotbugs ×10
java ×5
gradle ×4
findbugs ×3
android ×2
checkstyle ×2
autowired ×1
build.gradle ×1
include ×1
maven ×1
pmd ×1
spring-boot ×1