根据工具PMD,以下是一个不好的做法:
String s = "" + 123; // bad
String t = Integer.toString(456); // ok
This is an inefficient way to convert any type to a `String`.
Run Code Online (Sandbox Code Playgroud)
为什么这样做不好?
代码审查工具抱怨saveSafeScan中可能的空指针取消引用safeScanWarnings(...)在if行(safeScanWarnings!= Null&safeScanWarnings.size()> 0)
我想知道这怎么可能?这是因为我们通过引用返回集合吗?
protected void saveSafeScan(final Response response, final Dtec dtec) throws dtecException
{
Collection<String> safeScanWarnings = dtec.getSafeScanWarnings();
if (safeScanWarnings!=null && safeScanWarnings.size()>0)
{
Iterator<String> iterator = safeScanWarnings.iterator();
int i = 0;
while (iterator.hasNext())
{
String safeScanCode = iterator.next();
if (i == 0)
{
response.setSafeScanCode(safeScanCode);
response.setSafeScanCodeText(getMessage(String.format("DTECRESPONSE_SAFESCANCODE_%s",
StringUtils.trimToEmpty(safeScanCode))));
}
SafeScanWarning safeScan = new SafeScanWarning();
safeScan.setCode(safeScanCode);
safeScan.setMessage(String.format("DTECRESPONSE_SAFESCANCODE_%s", StringUtils.trimToEmpty(safeScanCode)));
safeScan.setPriority(i);
response.getSafeScanWarnings().add(safeScan);
i++;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在使用PMD和Findbug作为我的应用程序,但fortify设法检测我的应用程序中的一些安全漏洞.我想知道是否还有其他开源软件可以完成与Fortify类似的工作?
尝试在Eclipse/Juno中为项目运行pmd时,我收到此错误.
An internal error occurred during: "ReviewCode".
Couldn't find the class Can't find resource rulesets/basic.xml. Make sure the resource is a valid file or URL or is on the CLASSPATH. Here's the current classpath: C:\eclipsejuno\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
好的,所以我理解为什么我们应该从这个问题宣布论证是最终的,但我不明白为什么我们不应该...
由于Java总是使用pass by value,这意味着我们不能通过给定的参数返回一个新值,我们只能覆盖它,并使参数无用,因为我们不使用传递的值...
Java中非最终方法参数的唯一好处是您不必创建参数类型的局部变量吗?
PS这个问题是由... PMD的规则引发的MethodArgumentCouldBeFinal
在Gradle的Java项目中,我们可以通过pmd插件使用PMD .配置我们想要使用的规则可以通过两种方式实现:
使用ruleSetFiles,您可以找到规则的名称并添加或排除规则,但在文档中没有关于ruleStes的信息吗?从哪里可以找到确切的名字?根据我从其他项目中发现的名称,这些名称与PMD文档中的名称类似,但是小写.例如:
Braces - > java-braces
Clone - > java-clone
Implementation - >java-implementation
Code Size - > java-codesize
Run Code Online (Sandbox Code Playgroud)
但是像安全代码指南这样的转换不会转换为- > java-securitycodeguidelines,而只是转换为java-sunsecure.我发现了与PMD 5.1.1一起使用的名称.是:
pmd {
ruleSets = [
'java-android',
'java-basic',
'java-braces',
'java-clone',
'java-codesize',
'java-comments',
'java-controversial',
'java-coupling',
'java-design',
'java-empty',
'java-finalizers',
'java-imports',
'java-j2ee',
'java-javabeans',
'java-junit',
'java-logging-jakarta-commons',
'java-logging-java',
'java-migrating',
'java-naming',
'java-optimizations',
'java-strictexception',
'java-strings',
'java-sunsecure',
'java-typeresolution',
'java-unnecessary',
'java-unusedcode'
]
toolVersion = '5.1.1'
ignoreFailures = true
}
Run Code Online (Sandbox Code Playgroud)
如何查找文档中显示的PMD名称与Gradle名称之间的映射?
我想在使用gradle构建的企业项目中使用gradle PMD插件.
我有一个pmd_rules.xml已经可以工作的文件,但我不能添加自己的java规则(我得到一个类未找到异常).我按照它的网站上的教程.
我在哪里必须制定自己的规则,以便他们得到gradle和PMD的认可?有人已经做过类似的事吗?
pmd.gradle:
apply from: rootProject.file("core/modules.gradle"), to : ext
if(project.name in (modules["modules"] +modules["modules"])){
apply plugin: 'pmd'
pmd {
ignoreFailures = true
ruleSetFiles = rootProject.files("../repo/pmd_rules.xml")
sourceSets = [sourceSets.main,sourceSets.test]
targetJdk = org.gradle.api.plugins.quality.TargetJdk.VERSION_1_7
ruleSets = []
toolVersion = "5.0.5"
}
}
Run Code Online (Sandbox Code Playgroud) 我在这里阅读一个有趣的教程:http://www.avajava.com/tutorials/lessons/how-do-i-generate-pmd-and-cpd-reports-for-a-site.html?page = 1
本教程介绍如何使用Maven运行开源静态分析工具PMD,以及在Maven创建的网站上查看生成的输出.Maven可以使用mvn site命令轻松创建网站,但本教程将介绍如何使用PMD获取有关源代码的更有用的指标.
我尽最大努力遵循指示.这是我pom.xml阅读教程的文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.name.bookstore</groupId>
<artifactId>bookstore</artifactId>
<packaging>jar</packaging>
<version>1</version>
<name>bookstore</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<!-- start for deploying using webdav -->
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-webdav</artifactId>
<version>1.0-beta-2</version>
</extension>
</extensions>
</build>
<distributionManagement>
<!-- start -location where site is deployed -->
<site>
<id>site.deployments</id>
<name>Site deployments</name>
<url>dav:localhost/${basedir}</url>
</site>
</distributionManagement>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> …Run Code Online (Sandbox Code Playgroud) 我正在研究的Java项目使用了代码分析工具的组合:PMD,Checkstyle和FindBugs.这些可以解决大量的错误,风格问题等,但是经常会漏网:
public class AbstractBadlyNamedClass { // Not abstract!
// ...
}
Run Code Online (Sandbox Code Playgroud)
注倒过来的检查,即public abstract BadlyNamedClass给出了PMD警告"抽象类应命名为AbstractXXX".
任何人都可以建议是否有办法检查这一点,或者使用上述工具之一(可能是某种自定义规则?)或其他自动化工具来完成这项工作?
我有以下Java方法:
private int calculate() {
return (bytes[0] & 0xff) + ((bytes[1] & 0xff) << 8);
}
Run Code Online (Sandbox Code Playgroud)
PMD对此代码抱怨"UselessParentheses"违规.
我已经审查了运算符优先级规则,但我仍然没有在该代码中看到冗余的括号.我错过了什么吗?
pmd ×10
java ×8
eclipse ×2
findbugs ×2
gradle ×2
maven ×2
checkstyle ×1
eclipse-juno ×1
fortify ×1
oop ×1
open-source ×1
security ×1
string ×1
tostring ×1