Pmd-ida 插件安装使用
Pmd-ida代码工程: https://github.com/ybroeker/pmd-idea
Pmd-idea主页:https://plugins.jetbrains.com/plugin/15412-pmd-idea
路径:file->setting->plugins->Marketplace,国内在线安装下载吧不到安装包,到https://github.com/ybroeker/pmd-idea 在release界面下载和idea版本适配的zip包安装
安装完成后,设置pmd检查规则文件,设置路径:file->setting->Tools->PMD-IDEA
选择规则文件
提供官网一个规则参考内容
<?xml version="1.0"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default Maven PMD Plugin Ruleset" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"> <description> GeoTools ruleset. See https://pmd.github.io/latest/pmd_userdocs_understanding_rulesets.html </description> <rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP"/> <rule ref="category/java/bestpractices.xml/CheckResultSet"/> <!--rule ref="category/java/bestpractices.xml/UnusedFormalParameter" /--> <rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/> <rule ref="category/java/bestpractices.xml/UnusedPrivateField"/> <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/> <rule ref="category/java/bestpractices.xml/SystemPrintln"/> <rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach" /> <rule ref="category/java/bestpractices.xml/UseTryWithResources" > <properties> // The rule reports false positives for closeable arguments that are used // closed within the method body. This suppression tries to avoid false positives for // this case, until we can switch to Java 9 and just use try(variable) {...} without // the need to instatiate the variable in the try <property name="violationSuppressXPath" value="./FinallyStatement//Name[substring-after(@Image, '.') = 'close' or substring-after(@Image, '.') = 'closeQuietly'][pmd-java:typeIs('java.lang.AutoCloseable') and fn:substring-before(@Image, '.') = ancestor::MethodDeclaration/MethodDeclarator//VariableDeclaratorId/@Name]"/> </properties> </rule> <rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap" /> <rule ref="category/java/bestpractices.xml/ReplaceVectorWithList" /> <rule ref="category/java/bestpractices.xml/AvoidPrintStackTrace" /> <rule ref="category/java/bestpractices.xml/MissingOverride" /> <rule ref="category/java/bestpractices.xml/UseStandardCharsets" /> <rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation" /> <rule ref="category/java/bestpractices.xml/UseCollectionIsEmpty" /> <rule ref="category/java/codestyle.xml/EmptyControlStatement"/> <rule ref="category/java/codestyle.xml/ExtendsObject"/> <rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop"/> <rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName"/> <rule ref="category/java/codestyle.xml/UseDiamondOperator" /> <rule ref="category/java/codestyle.xml/UnnecessaryReturn"/> <rule ref="category/java/codestyle.xml/UselessParentheses"/> <rule ref="category/java/codestyle.xml/UselessQualifiedThis"/> <rule ref="category/java/codestyle.xml/UnnecessaryCast" /> <rule ref="category/java/codestyle.xml/IdenticalCatchBranches" /> <rule ref="category/java/codestyle.xml/UseShortArrayInitializer" /> <rule ref="category/java/codestyle.xml/UnnecessaryImport" /> <rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/> <rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators"/> <rule ref="category/java/errorprone.xml/AvoidUsingOctalValues"/> <rule ref="category/java/errorprone.xml/BrokenNullCheck"/> <rule ref="category/java/errorprone.xml/CheckSkipResult"/> <rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray"/> <rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices"/> <rule ref="category/java/errorprone.xml/JumbledIncrementer"/> <rule ref="category/java/errorprone.xml/MisplacedNullCheck"/> <rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode"/> <rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock"/> <rule ref="category/java/errorprone.xml/UnconditionalIfStatement"/> <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary"/> <rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals"/> <rule ref="category/java/errorprone.xml/UselessOperationOnImmutable"/> <rule ref="category/java/errorprone.xml/CloseResource"> <properties> <!-- When calling the store to close, PMD wants the full prefix before the call to the method to match, so let's try to use common var names for store ... --> <property name="closeTargets" value="releaseConnection,store.releaseConnection,closeQuietly,closeConnection,closeSafe,store.closeSafe,dataStore.closeSafe,getDataStore().closeSafe,close,closeResultSet,closeStmt"/> <property name="allowedResourceTypes" value="java.io.ByteArrayOutputStream|java.io.ByteArrayInputStream|java.io.StringWriter|java.io.CharArrayWriter|java.util.stream.Stream|java.util.stream.IntStream|java.util.stream.LongStream|java.util.stream.DoubleStream|java.io.StringReader" /> </properties> </rule> <rule ref="category/java/multithreading.xml/AvoidThreadGroup"/> <rule ref="category/java/multithreading.xml/DontCallThreadRun"/> <rule ref="category/java/multithreading.xml/DoubleCheckedLocking"/> <rule ref="category/java/performance.xml/UseArrayListInsteadOfVector" /> <rule ref="category/java/performance.xml/BigIntegerInstantiation"/> <rule ref="category/java/performance.xml/StringInstantiation"/> <rule name="wildcards" language="java" message="No Wildcard Imports" class="net.sourceforge.pmd.lang.rule.XPathRule"> <description> Don't use wildcard imports </description> <priority>3</priority> <properties> <property name="version" value="2.0"/> <property name="xpath"> <value><![CDATA[ //ImportDeclaration[@ImportedName=@PackageName] ]]></value> </property> </properties> </rule> <rule ref="category/java/design.xml/CognitiveComplexity"> <properties> <property name="reportLevel" value="140"/> </properties> </rule> </ruleset>
执行检查扫描