/projects/checkstyle-5.6/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheck.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 125 lines · 39 code · 7 blank · 79 comment · 4 complexity · c77ef273394df147ada7dea9a303f7d9 MD5 · raw file

  1. ////////////////////////////////////////////////////////////////////////////////
  2. // checkstyle: Checks Java source code for adherence to a set of rules.
  3. // Copyright (C) 2001-2012 Oliver Burn
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. ////////////////////////////////////////////////////////////////////////////////
  19. package com.puppycrawl.tools.checkstyle.checks.naming;
  20. import com.puppycrawl.tools.checkstyle.api.DetailAST;
  21. import com.puppycrawl.tools.checkstyle.api.TokenTypes;
  22. /**
  23. * <p>
  24. * Checks that method names conform to a format specified
  25. * by the format property. The format is a
  26. * {@link java.util.regex.Pattern regular expression}
  27. * and defaults to
  28. * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
  29. * </p>
  30. *
  31. * <p>
  32. * Also, checks if a method name has the same name as the residing class.
  33. * The default is false (it is not allowed). It is legal in Java to have
  34. * method with the same name as a class. As long as a return type is specified
  35. * it is a method and not a constructor which it could be easily confused as.
  36. * </p>
  37. *
  38. * <p>
  39. * An example of how to configure the check is:
  40. * </p>
  41. * <pre>
  42. * &lt;module name="MethodName"/&gt;
  43. * </pre>
  44. * <p>
  45. * An example of how to configure the check for names that begin with
  46. * a lower case letter, followed by letters, digits, and underscores is:
  47. * </p>
  48. * <pre>
  49. * &lt;module name="MethodName"&gt;
  50. * &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
  51. * &lt;/module&gt;
  52. * </pre>
  53. *
  54. * <p>
  55. * An example of how to configure the check to allow method names
  56. * to be equal to the residing class name is:
  57. * </p>
  58. * <pre>
  59. * &lt;module name="MethodName"&gt;
  60. * &lt;property name="allowClassName" value="true"/&gt;
  61. * &lt;/module&gt;
  62. * </pre>
  63. * @author Oliver Burn
  64. * @author Travis Schneeberger
  65. * @version 1.1
  66. */
  67. public class MethodNameCheck
  68. extends AbstractAccessControlNameCheck
  69. {
  70. /**
  71. * for allowing method name to be the same as the class name.
  72. */
  73. private boolean mAllowClassName;
  74. /** Creates a new <code>MethodNameCheck</code> instance. */
  75. public MethodNameCheck()
  76. {
  77. super("^[a-z][a-zA-Z0-9]*$");
  78. }
  79. @Override
  80. public int[] getDefaultTokens()
  81. {
  82. return new int[] {TokenTypes.METHOD_DEF, };
  83. }
  84. @Override
  85. public void visitToken(DetailAST aAst)
  86. {
  87. super.visitToken(aAst); // Will check the name against the format.
  88. if (!mAllowClassName) {
  89. final DetailAST method =
  90. aAst.findFirstToken(TokenTypes.IDENT);
  91. //in all cases this will be the classDef type except anon inner
  92. //with anon inner classes this will be the Literal_New keyword
  93. final DetailAST classDefOrNew = aAst.getParent().getParent();
  94. final DetailAST classIdent =
  95. classDefOrNew.findFirstToken(TokenTypes.IDENT);
  96. // Following logic is to handle when a classIdent can not be
  97. // found. This is when you have a Literal_New keyword followed
  98. // a DOT, which is when you have:
  99. // new Outclass.InnerInterface(x) { ... }
  100. // Such a rare case, will not have the logic to handle parsing
  101. // down the tree looking for the first ident.
  102. if ((null != classIdent)
  103. && method.getText().equals(classIdent.getText()))
  104. {
  105. log(method.getLineNo(), method.getColumnNo(),
  106. "method.name.equals.class.name", method.getText());
  107. }
  108. }
  109. }
  110. /**
  111. * Sets the property for allowing a method to be the same name as a class.
  112. * @param aAllowClassName true to allow false to disallow
  113. */
  114. public void setAllowClassName(boolean aAllowClassName)
  115. {
  116. mAllowClassName = aAllowClassName;
  117. }
  118. }