ali-exception.xml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?xml version="1.0"?>
  2. <ruleset name="AlibabaJavaExceptions" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
  5. <rule name="MethodReturnWrapperTypeRule"
  6. language="java"
  7. since="1.6"
  8. message="java.exception.MethodReturnWrapperTypeRule.rule.msg"
  9. class="com.alibaba.p3c.pmd.lang.java.rule.exception.MethodReturnWrapperTypeRule">
  10. <priority>3</priority>
  11. <example>
  12. <![CDATA[
  13. public int method() {
  14. Integer a = null;
  15. return a;
  16. }
  17. ]]>
  18. </example>
  19. </rule>
  20. <rule name="AvoidReturnInFinallyRule"
  21. language="java"
  22. message="java.exception.AvoidReturnInFinallyRule.rule.msg"
  23. class="com.alibaba.p3c.pmd.lang.java.rule.exception.AvoidReturnInFinallyRule">
  24. <priority>2</priority>
  25. <example>
  26. <![CDATA[
  27. Negative example:
  28. public static Long readFileLength(String fileName) {
  29. try {
  30. File file = new File(fileName);
  31. RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  32. return randomAccessFile.length();
  33. } catch (Exception e) {
  34. logger.error(e.getMessage(), e);
  35. } finally {
  36. countDownLatch.countDown();
  37. return 0L;
  38. }
  39. }
  40. ]]>
  41. </example>
  42. </rule>
  43. <rule name="TransactionMustHaveRollbackRule"
  44. language="java"
  45. since="1.6"
  46. message="java.exception.TransactionMustHaveRollbackRule.rule.msg"
  47. dfa="true"
  48. class="com.alibaba.p3c.pmd.lang.java.rule.exception.TransactionMustHaveRollbackRule">
  49. <priority>3</priority>
  50. <example>
  51. <![CDATA[
  52. Positive example 1:
  53. /**
  54. * @author caikang
  55. * @date 2017/04/07
  56. */
  57. @Service
  58. @Transactional(rollbackFor = Exception.class)
  59. public class UserServiceImpl implements UserService {
  60. @Override
  61. public void save(User user) {
  62. //some code
  63. //db operation
  64. }
  65. }
  66. ]]>
  67. </example>
  68. <example>
  69. <![CDATA[
  70. Positive example 2:
  71. /**
  72. * @author caikang
  73. * @date 2017/04/07
  74. */
  75. @Service
  76. public class UserServiceImpl implements UserService {
  77. @Override
  78. @Transactional(rollbackFor = Exception.class)
  79. public void save(User user) {
  80. //some code
  81. //db operation
  82. }
  83. }
  84. ]]>
  85. </example>
  86. <example>
  87. <![CDATA[
  88. Positive example 3:
  89. /**
  90. * @author caikang
  91. * @date 2017/04/07
  92. */
  93. @Service
  94. public class UserServiceImpl implements UserService {
  95. @Autowired
  96. private DataSourceTransactionManager transactionManager;
  97. @Override
  98. @Transactional
  99. public void save(User user) {
  100. DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  101. // explicitly setting the transaction name is something that can only be done programmatically
  102. def.setName("SomeTxName");
  103. def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  104. TransactionStatus status = transactionManager.getTransaction(def);
  105. try {
  106. // execute your business logic here
  107. //db operation
  108. } catch (Exception ex) {
  109. transactionManager.rollback(status);
  110. throw ex;
  111. }
  112. }
  113. }
  114. ]]>
  115. </example>
  116. </rule>
  117. </ruleset>