findbugs

Reusing a PreparedStatement

两盒软妹~` 提交于 2019-12-01 05:29:02
问题 I ran findbugs on our code base and it pointed out there are two more Statements that still need to be closed. In this section of the code we run: preparedStatement = connection.prepareStatement(query); for 3 different queries, reusing preparedStatement. In the finally block we do close the resource: finally{ try{ if (resultSet != null) resultSet.close(); } catch (Exception e) { exceptionHandler.ignore(e); } try { if (preparedStatement != null) preparedStatement.close(); } catch(Exception e)

How to detect a new value was added to an enum and is not handled in a switch

房东的猫 提交于 2019-12-01 02:45:21
问题 From time to time I have to add a new value to a enum type in my project. public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, FILENOTFOUND //this one is new one } What I would like is to have a compile time error for every switch I have that is not treating the new value, like this one: switch (color) { case MONDAY: case TUESDAY: case WEDNESDAY: case THURSDAY: System.out.println("Mondays are bad."); break; case FRIDAY: System.out.println("Fridays are better.");

findbugs and database password security issue

被刻印的时光 ゝ 提交于 2019-12-01 00:32:14
问题 I'm using the following code to initialize database connection: public Connection getConnection() { try { if (null == connection) { String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver Class.forName(driverName); // Create a connection to the database String serverName = "localhost"; String database = "database"; String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url String username = "username"; String password = "password"; connection = DriverManager

Sonar 配置代码扫描规则 与 质量配置,设置各个语言默认的扫描规则

感情迁移 提交于 2019-12-01 00:21:04
配置代码扫描规则 质量配置,设置各个语言默认的扫描规则 代码规则设置 如果认为有些扫描规则太严格了,可以把它“挂起”,以后再构建的代码扫描项目时就不会再使用本规则,例如挂起JP中的一个BUG规则: 配置指标 编辑 质量规则 Sonar 中的findBug插件, 内置了 findBus secrity Jsp 规则,但是目前的网页代码使用的不是jsp方案。所以此时会关闭相关规则。 如下 后得到 上面的 FindBugs Jsp ,然后点击此项。进入下列页面,点击配置更多规则。 进入 选择 上方的批量修改,点击挂起 即可完成。 然后回到 质量配置界面 此时如果是非默认会出现默认按钮,点击选择默认即可。 后续的代码分析会选择此规则。 来源: oschina 链接: https://my.oschina.net/u/2918544/blog/1838292

FindBugs简介、安装、使用

夙愿已清 提交于 2019-12-01 00:20:52
一、FindBugs简介 FindBugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。不是通过分析类文件的形式或结构来确定程序的意图,而是通常使用 Visitor 模式来鉴别代码是否符合一些固定的规范。 官网地址: http://findbugs.sourceforge.net/ 二、FindBugs安装 FindBugs有界面版和eclipse插件版,下面一一介绍。 1、界面版 在官网下载findbugs-3.0.1.zip,然后解压。然后找到bin目录,下面有一个findbugs.bat。双击运行。 2、eclipse插件版 下载edu.umd.cs.findbugs.plugin.eclipse_3.0.1.20150306-5afe4d1.zip。然后解压。将解压后的文件夹copy到eclipse的plugins目录下。重启eclipse。安装完毕。 三、FindBugs使用 findbugs 简单易用,按照下图操作即可; 1, 在eclipse package Explorer 右键选择目标工程-> build project 2, 选择指定的包或者类进行findbug 此时findbugs会遍历指定的包或者类,进行分析,找出代码bug,然后集中显示在

Java resource management: understanding Findbugs results

荒凉一梦 提交于 2019-12-01 00:00:59
Findbugs bugs me about a method which opens two Closeable instances, but I can't understand why. Source public static void sourceXmlToBeautifiedXml(File input, File output) throws TransformerException, IOException, JAXBException { FileReader fileReader = new FileReader(input); FileWriter fileWriter = new FileWriter(output); try { // may throw something sourceXmlToBeautifiedXml(fileReader, fileWriter); } finally { try { fileReader.close(); } finally { fileWriter.close(); } } } Findbugs analysis Findbugs tells me Method [...] may fail to clean up java.io.Reader [...] and points to the line with

FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

好久不见. 提交于 2019-11-30 23:06:48
问题 I am clueless here... 1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> { 2: String tableName; 3: String fkFieldName; 4: 5: public int compareTo(ForeignKeyConstraint o) { 6: if (this.tableName.compareTo(o.tableName) == 0) { 7: return this.fkFieldName.compareTo(o.fkFieldName); 8: } 9: return this.tableName.compareTo(o.tableName); 10: } 11: } In line 6 I get from FindBugs: Bug: net.blabla.SqlFixer$ForeignKeyConstraint defines compareTo(SqlFixer

Writing a detector to search for uses of “System.out.println” using Findbugs

对着背影说爱祢 提交于 2019-11-30 21:18:38
I am trying to write a bug detector to find instances of the method call "System.out.println" using Findbugs. I understand that "System.out.println" in bytecode is compiled to a call to GETSTATIC, which pushes "System.out" onto the stack . A call to INVOKEVIRTUAL pops "System.out" off the stack and calls the method. I have prepared some code (found below) which finds the correct GETSTATIC and INVOKEVIRTUAL calls, but have been unable to link the two together. I suspect I may need to use OpcodeStack in some way, but am having trouble in understanding how I can use it. Any help would be

[sonarqube的使用] sonarqube安装

纵然是瞬间 提交于 2019-11-30 12:55:22
一 . SonarQube代码质量检查工具简介 Sonar (SonarQube)是一个开源平台,用于管理源代码的质量 Sonar 不只是一个质量数据报告工具,更是代码质量管理平台 支持Java, C#, C/C++, PL/SQL, Cobol, JavaScrip, Groovy 等等二十几种编程语言的代码质量管理与检测。 Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题。 不遵循代码标准 sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具规范代码编写。 潜在的缺陷 sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具检 测出潜在的缺陷。 糟糕的复杂度分布 文件、类、方法等,如果复杂度过高将难以改变,这会使得开发人员 难以理解它们, 且如果没有自动化的单元测试,对于程 序中的任何组件的改变都将可能导致需要全面的回归测试。 重复 显然程序中包含大量复制粘贴的代码是质量低下的,sonar可以展示 源码中重复严重的地方。 注释不足或者过多 没有注释将使代码可读性变差,特别是当不可避免地出现人员变动 时,程序的可读性将大幅下降 而过多的注释又会使得开发人员将精力过多地花费在阅读注释上,亦违背初衷。 缺乏单元测试 sonar可以很方便地统计并展示单元测试覆盖率。 糟糕的设计

Any easy way to generate a Findbug HTML report from Maven without site:site?

穿精又带淫゛_ 提交于 2019-11-30 12:17:37
I am trying to integrate FindBugs in a maven project. Does anyone have a sample pom.xml generating a simple findbug HTML report in target? Is it possible to generate this report without having to run site:site ? Check out Sonar . It's an open-source, stand-alone, web service that you "submit" your code to and it produces beautiful HTML reports on all kinds of code metrics. It also keeps a history of builds. And best of all, you don't have to modify your builds or poms! There is a maven goal for it too: sonar:sonar . Jenkins (previously Hudson) has a plugin for it, so it's totally painless if