View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.checkstyle;
32  
33  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
34  import com.puppycrawl.tools.checkstyle.api.DetailAST;
35  import com.puppycrawl.tools.checkstyle.api.FileContents;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
38  import java.util.regex.Pattern;
39  
40  /**
41   * Checks that non static method must contain at least one reference to
42   * {@code this}.
43   *
44   * <p>If your method doesn't need {@code this} than why it is not
45   * {@code static}?
46   *
47   * The exception here is when method has {@code @Override} annotation. There's
48   * no concept of inheritance and polymorphism for static methods even if they
49   * don't need {@code this} to perform the actual work.
50   *
51   * Another exception is when method is {@code abstract} or {@code native}.
52   * Such methods don't have body so detection based on {@code this} doesn't
53   * make sense for them.
54   *
55   * @since 0.3
56   */
57  public final class NonStaticMethodCheck extends AbstractCheck {
58  
59      /**
60       * Files to exclude from this check.
61       * This is mostly to exclude JUnit tests.
62       */
63      private Pattern exclude = Pattern.compile("^$");
64  
65      /**
66       * Exclude files matching given pattern.
67       * @param excl Regexp of classes to exclude.
68       */
69      public void setExcludeFileNamePattern(final String excl) {
70          this.exclude = Pattern.compile(excl);
71      }
72  
73      @Override
74      public int[] getDefaultTokens() {
75          return new int[] {
76              TokenTypes.METHOD_DEF,
77          };
78      }
79  
80      @Override
81      public int[] getAcceptableTokens() {
82          return this.getDefaultTokens();
83      }
84  
85      @Override
86      public int[] getRequiredTokens() {
87          return this.getDefaultTokens();
88      }
89  
90      @Override
91      @SuppressWarnings("deprecation")
92      public void visitToken(final DetailAST ast) {
93          if (this.exclude.matcher(this.getFileContents().getFileName())
94              .find()) {
95              return;
96          }
97          if (TokenTypes.CLASS_DEF == ast.getParent().getParent().getType()) {
98              this.checkClassMethod(ast);
99          }
100     }
101 
102     /**
103      * Check that non static class method refer {@code this}. Methods that
104      * are {@code native}, {@code abstract} or annotated with {@code @Override}
105      * are excluded.  Additionally, if the method only throws an exception, it
106      * too is excluded.
107      * @param method DetailAST of method
108      */
109     private void checkClassMethod(final DetailAST method) {
110         final DetailAST modifiers = method
111             .findFirstToken(TokenTypes.MODIFIERS);
112         if (modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) != null) {
113             return;
114         }
115         final BranchContains checker = new BranchContains(method);
116         final boolean onlythrow =
117             checker.check(TokenTypes.LITERAL_THROW)
118                 && !checker.check(TokenTypes.LCURLY)
119                 && this.countSemiColons(method) == 1;
120         if (!AnnotationUtil.containsAnnotation(method, "Override")
121             && !isInAbstractOrNativeMethod(method)
122             && !checker.check(TokenTypes.LITERAL_THIS)
123             && !onlythrow) {
124             final int line = method.getLineNo();
125             this.log(
126                 line,
127                 "This method must be static, because it does not refer to \"this\""
128             );
129         }
130     }
131 
132     /**
133      * Determines whether a method is {@code abstract} or {@code native}.
134      * @param method Method to check.
135      * @return True if method is abstract or native.
136      */
137     private static boolean isInAbstractOrNativeMethod(final DetailAST method) {
138         final DetailAST modifiers = method.findFirstToken(TokenTypes.MODIFIERS);
139         final BranchContains checker = new BranchContains(modifiers);
140         return checker.check(TokenTypes.ABSTRACT)
141             || checker.check(TokenTypes.LITERAL_NATIVE);
142     }
143 
144     /**
145      * Determines the number semicolons in a method excluding those in
146      * comments.
147      * @param method Method to count
148      * @return The number of semicolons in the method as an int
149      */
150     @SuppressWarnings("deprecation")
151     private int countSemiColons(final DetailAST method) {
152         final DetailAST openingbrace = method.findFirstToken(TokenTypes.SLIST);
153         int count = 0;
154         if (openingbrace != null) {
155             final DetailAST closingbrace =
156                 openingbrace.findFirstToken(TokenTypes.RCURLY);
157             final int lastline = closingbrace.getLineNo();
158             final int firstline = openingbrace.getLineNo();
159             final FileContents contents = this.getFileContents();
160             for (int line = firstline - 1; line < lastline; line += 1) {
161                 if (!contents.lineIsBlank(line)
162                     && !contents.lineIsComment(line)
163                     && contents.getLine(line).contains(";")) {
164                     count += 1;
165                 }
166             }
167         }
168         return count;
169     }
170 }