assira.custom.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ruleset name="net.ranides.assira.custom"
  3. xmlns="http://pmd.sf.net/ruleset/1.0.0"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
  6. xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  7. <description>
  8. Assira Quality Policy - custom rules
  9. </description>
  10. <rule
  11. name="TooManyDifferentMethods"
  12. since="4.3"
  13. class="net.sourceforge.pmd.lang.rule.XPathRule"
  14. message="This class has too many different methods, consider refactoring it."
  15. externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#TooManyMethods"
  16. language="java"
  17. >
  18. <description><![CDATA[Counts unique methods (all overloaded versions are considered as the same). Ignores UtilityClassess.]]></description>
  19. <priority>3</priority>
  20. <properties>
  21. <property name="maxmethods" type="Integer" description="The method count reporting threshold " min="1" max="1000" value="15"/>
  22. <property name="xpath"><value>
  23. <![CDATA[
  24. //ClassOrInterfaceDeclaration[
  25. count(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[
  26. MethodDeclaration
  27. and not(Annotation/MarkerAnnotation/Name[@Image='Override'])
  28. and not(starts-with(MethodDeclaration/@MethodName,'get'))
  29. and not(starts-with(MethodDeclaration/@MethodName,'set'))
  30. and not(
  31. MethodDeclaration/@MethodName
  32. =
  33. preceding-sibling::ClassOrInterfaceBodyDeclaration/MethodDeclaration/@MethodName
  34. )
  35. ]) > $maxmethods
  36. and not (
  37. @Final='true'
  38. and
  39. ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private='true'][@ParameterCount='0']
  40. and
  41. count(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)=1
  42. )
  43. ]
  44. ]]>
  45. </value></property>
  46. </properties>
  47. </rule>
  48. <rule
  49. name="TooManyPublicMethods"
  50. since="4.3"
  51. class="net.sourceforge.pmd.lang.rule.XPathRule"
  52. message="This class has too many public methods, consider refactoring it."
  53. externalInfoUrl="http://pmd.sourceforge.net/rules/codesize.html#TooManyMethods"
  54. language="java"
  55. >
  56. <description><![CDATA[Counts public methods]]></description>
  57. <priority>3</priority>
  58. <properties>
  59. <property name="maxmethods" type="Integer" description="The method count reporting threshold " min="1" max="1000" value="40"/>
  60. <property name="xpath"><value>
  61. <![CDATA[
  62. //ClassOrInterfaceDeclaration[
  63. count(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[
  64. @Public='true'
  65. and not(starts-with(@MethodName,'get'))
  66. and not(starts-with(@MethodName,'set'))
  67. and not(../Annotation/MarkerAnnotation/Name[@Image='Override'])
  68. ]) > $maxmethods
  69. ]
  70. ]]>
  71. </value></property>
  72. </properties>
  73. </rule>
  74. <!--
  75. <rule
  76. name="ShortVar"
  77. since="2.0"
  78. class="net.sourceforge.pmd.lang.rule.XPathRule"
  79. message="Avoid variables with short names like {0}"
  80. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#ShortVariable"
  81. language="java"
  82. >
  83. <description>
  84. <![CDATA[Detects when a field, local, or parameter has a very short name.Ignores primitive types, local Iterators and for-variables.]]>
  85. </description>
  86. <priority>3</priority>
  87. <properties><property name="xpath" pluginname="true"><value>
  88. <![CDATA[
  89. //VariableDeclaratorId [
  90. string-length(@Image) < 3
  91. and not(@Image='_')
  92. and not(@Image='id')
  93. and not(ancestor::ForInit)
  94. and not(../Type/PrimitiveType)
  95. and not(../../Type/PrimitiveType)
  96. and not(
  97. (../../Type/ReferenceType/ClassOrInterfaceType[@Image='Iterator'])
  98. or
  99. (../../../FormalParameter)
  100. )
  101. and not(/TypeDeclaration/ClassOrInterfaceDeclaration[ends-with(@Image,'Test')])
  102. and not((ancestor::FormalParameter) and (ancestor::TryStatement))
  103. and not((@Image='em') and (../../Type/@TypeImage='EntityManager'))
  104. and not((@Image='em') and (../Type/@TypeImage='EntityManager'))
  105. ]
  106. ]]>
  107. </value></property></properties>
  108. </rule>
  109. -->
  110. <rule
  111. name="LongVar"
  112. since="0.3"
  113. message="Avoid excessively long variable names like {0}"
  114. class="net.sourceforge.pmd.lang.rule.XPathRule"
  115. externalInfoUrl="http://pmd.sourceforge.net/rules/naming.html#LongVariable"
  116. language="java"
  117. >
  118. <description>Detects when a field, formal or local variable is declared with a long name.</description>
  119. <priority>3</priority>
  120. <properties>
  121. <property name="minimum" type="Integer" description="The variable length reporting threshold" min="1" max="1000" value="16"/>
  122. <property name="xpath" pluginname="true"><value>
  123. <![CDATA[
  124. //VariableDeclaratorId[
  125. string-length(@Image) > $minimum
  126. and (@Static='false' or @Final='false')
  127. ]
  128. ]]>
  129. </value></property>
  130. </properties>
  131. </rule>
  132. <rule name="VarNameRules" since="2.0" class="net.sourceforge.pmd.lang.rule.XPathRule"
  133. message="Field name doesn't respect required pattern"
  134. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#VariableNamingRegexp"
  135. language="java"
  136. >
  137. <description>Checks all field names against regular expression</description>
  138. <priority>3</priority>
  139. <properties><property name="xpath" pluginname="true"><value>
  140. <![CDATA[
  141. //FieldDeclaration[
  142. not(matches(@VariableName,'^(m_|\$)?([a-z][a-zA-Z0-9]*)$'))
  143. and( (@Static='false') or (@Final='false') )
  144. ]
  145. ]]>
  146. </value></property></properties>
  147. </rule>
  148. <rule name="MethodNameRules" since="2.0" class="net.sourceforge.pmd.lang.rule.XPathRule"
  149. message="Method name doesn't respect required pattern"
  150. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#VariableNamingRegexp"
  151. language="java"
  152. >
  153. <description>Checks all methods names against regular expression</description>
  154. <priority>3</priority>
  155. <properties><property name="xpath" pluginname="true"><value>
  156. <![CDATA[
  157. //ClassOrInterfaceBodyDeclaration
  158. [not(Annotation/MarkerAnnotation/Name[@Image='Test'])]
  159. [not(Annotation/MarkerAnnotation/Name[@Image='InterfaceTest'])]
  160. /MethodDeclaration/MethodDeclarator
  161. [not(matches(@Image,'^(m_|\$)?([a-z][a-zA-Z0-9]*)$'))]
  162. ]]>
  163. </value></property></properties>
  164. </rule>
  165. <rule
  166. name="InvalidUtilityClass"
  167. since="3.0"
  168. message="Utility class does not provide any static methods, fields or interfaces"
  169. class="net.sourceforge.pmd.lang.rule.XPathRule"
  170. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#InvalidUtilityClass"
  171. language="java"
  172. >
  173. <description>A class that has private constructors and does not have any static methods, fields or type declarations cannot be used.</description>
  174. <priority>3</priority>
  175. <properties><property name="xpath"><value>
  176. <![CDATA[
  177. //ClassOrInterfaceDeclaration[@Nested='false'][
  178. (
  179. count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)>0
  180. and
  181. count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private='true'])
  182. )
  183. and count(.//MethodDeclaration[@Static='true'])=0
  184. and count(.//FieldDeclaration[@Private='false'][@Static='true'])=0
  185. and count(.//ClassOrInterfaceDeclaration[@Interface='true'])=0
  186. and count(.//AnnotationTypeDeclaration)=0
  187. and count(.//ClassOrInterfaceDeclaration[@Static='true' and @Private='false'])=0
  188. ]
  189. ]]>
  190. </value></property></properties>
  191. </rule>
  192. <rule
  193. name="TooManyParams"
  194. since="3.0"
  195. message="Avoid really long parameter lists."
  196. class="net.sourceforge.pmd.lang.rule.XPathRule"
  197. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#TooManyParams"
  198. language="java"
  199. >
  200. <description>Long parameter lists can indicate that a new object should be created to wrap the numerous parameters. Basically, try to group the parameters together.</description>
  201. <priority>3</priority>
  202. <properties>
  203. <property name="maximum" type="Integer" description="The parameter count reporting threshold" min="1" max="1000" value="6"/>
  204. <property name="xpath"><value>
  205. <![CDATA[
  206. //ClassOrInterfaceBodyDeclaration
  207. [ MethodDeclaration/MethodDeclarator/FormalParameters[@ParameterCount>$maximum] ]
  208. [ not(Annotation/MarkerAnnotation/Name[@Image='Override']) ]
  209. ]]>
  210. </value></property>
  211. </properties>
  212. </rule>
  213. <rule
  214. name="AvoidThreadGroup"
  215. since="3.6"
  216. message="Avoid using java.lang.ThreadGroup; it is not thread safe"
  217. class="net.sourceforge.pmd.lang.rule.XPathRule"
  218. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#AvoidThreadGroup"
  219. language="java"
  220. typeResolution="true"
  221. >
  222. <description>Avoid using java.lang.ThreadGroup; although it is intended to be used in a threaded environment it contains methods that are not thread safe.</description>
  223. <priority>3</priority>
  224. <properties>
  225. <property name="xpath"><value>
  226. <![CDATA[
  227. //AllocationExpression/ClassOrInterfaceType
  228. [ typeof(@Image, 'java.lang.ThreadGroup') and contains(@Image, 'ThreadGroup') ]
  229. |
  230. //PrimarySuffix[contains(@Image, 'getThreadGroup')]
  231. ]]>
  232. </value></property>
  233. </properties>
  234. </rule>
  235. <rule
  236. name="UseSingleton"
  237. since="3.0"
  238. message="All methods are static. Consider using Singleton instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning."
  239. class="net.sourceforge.pmd.lang.rule.XPathRule"
  240. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#UseSingleton"
  241. language="java"
  242. >
  243. <description><![CDATA[If you have a class that has nothing but static methods, consider making it a Singleton. If you want this class to be a Singleton, remember to add a private constructor to prevent instantiation.]]></description>
  244. <priority>3</priority>
  245. <properties>
  246. <property name="xpath"><value>
  247. <![CDATA[
  248. //ClassOrInterfaceDeclaration[
  249. @Interface='false' and @Abstract='false'
  250. and not(ExtendsList)
  251. and not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[
  252. @Private!='true' and @Static='false'
  253. ])
  254. and not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration[
  255. @Private!='true' and @Static='false'
  256. ])
  257. and not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration[
  258. @MethodName='main' and
  259. @Static='true' and
  260. @Public='true' and
  261. @Void='true' and
  262. MethodDeclarator/FormalParameters[@ParameterCount=1] and
  263. MethodDeclarator/FormalParameters/FormalParameter/Type[@Array='true' and @TypeImage='String']
  264. ])
  265. and (
  266. @Final='false' or
  267. count(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)!=1
  268. or not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[
  269. @Private='true'
  270. and FormalParameters[@ParameterCount=0]
  271. ])
  272. )
  273. ]
  274. ]]>
  275. </value></property>
  276. </properties>
  277. </rule>
  278. <rule
  279. name="TooManyFields"
  280. since="4.3"
  281. message="Too many fields"
  282. class="net.sourceforge.pmd.lang.rule.XPathRule"
  283. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#TooManyFields"
  284. typeResolution="true"
  285. language="java"
  286. >
  287. <description><![CDATA[Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.]]></description>
  288. <priority>3</priority>
  289. <properties>
  290. <property name="maxfields" type="Integer" description="The field count reporting threshold " min="1" max="1000" value="15"/>
  291. <property name="xpath"><value>
  292. <![CDATA[
  293. //ClassOrInterfaceDeclaration[
  294. count(
  295. ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
  296. [@Final='false']
  297. [@Static='false']
  298. ) > $maxfields
  299. ]
  300. ]]>
  301. </value></property>
  302. </properties>
  303. </rule>
  304. <!-- Ignores parameters prefixed with $ :) -->
  305. <rule name="UnusedParameter"
  306. language="java"
  307. since="5.3"
  308. message="Avoid unused {0} parameters such as ''{1}''."
  309. class="net.ranides.assira.rules.UnusedParameterRule"
  310. externalInfoUrl="${pmd.website.baseurl}/rules/java/unusedcode.html#UnusedFormalParameter">
  311. <description>
  312. Avoid passing parameters to methods or constructors without actually referencing them in the method body.
  313. </description>
  314. <priority>3</priority>
  315. <example>
  316. <![CDATA[
  317. public class Foo {
  318. private void bar(String howdy) {
  319. // howdy is not used
  320. }
  321. }
  322. ]]>
  323. </example>
  324. </rule>
  325. <!--
  326. <rule
  327. name="LexicableConstructor"
  328. since="4.3"
  329. message="Lexicable class must be constructable from LexicalContext"
  330. class="net.sourceforge.pmd.lang.rule.XPathRule"
  331. externalInfoUrl="http://java.ranides.net/docs/pmd.htm#TooManyFields"
  332. typeResolution="true"
  333. >
  334. <description><![CDATA[Classes that implement Lexicable interface must declare apriotiate ocnstructor with signature: Construct(LexicalContext context). It can be public but this is not obligatory.]]></description>
  335. <priority>3</priority>
  336. <properties>
  337. <property name="xpath"><value>
  338. <![CDATA[
  339. //ClassOrInterfaceDeclaration[
  340. ImplementsList/ClassOrInterfaceType[
  341. @Image='Lexicable' or @Image='net.ranides.assira.text.Lexicable'
  342. ]
  343. and
  344. not(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[
  345. FormalParameters[
  346. @ParameterCount='1'
  347. and
  348. FormalParameter/Type[@Array='false'][@TypeImage='LexicalContext' or @TypeImage='net.ranides.assira.text.LexicalContext']
  349. ]
  350. ])
  351. ]
  352. ]]>
  353. </value></property>
  354. </properties>
  355. </rule>
  356. -->
  357. </ruleset>