1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2015 the original author or authors.
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.design;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26 * Make sure that utility classes (classes that contain only static methods)
27 * do not have a public constructor.
28 * <p>
29 * Rationale: Instantiating utility classes does not make sense.
30 * A common mistake is forgetting to hide the default constructor.
31 * </p>
32 *
33 * @author lkuehne
34 */
35 public class HideUtilityClassConstructorCheck extends Check
36 {
37
38 /**
39 * A key is pointing to the warning message text in "messages.properties"
40 * file.
41 */
42 public static final String MSG_KEY = "hide.utility.class";
43
44 @Override
45 public int[] getDefaultTokens()
46 {
47 return new int[] {TokenTypes.CLASS_DEF};
48 }
49
50 @Override
51 public int[] getAcceptableTokens()
52 {
53 return new int[] {TokenTypes.CLASS_DEF};
54 }
55
56 @Override
57 public void visitToken(DetailAST ast)
58 {
59 if (isAbstract(ast)) {
60 // abstract class could not have private constructor
61 return;
62 }
63
64 final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
65 DetailAST child = objBlock.getFirstChild();
66 final boolean hasStaticModifier = isStatic(ast);
67 boolean hasMethodOrField = false;
68 boolean hasNonStaticMethodOrField = false;
69 boolean hasNonPrivateStaticMethodOrField = false;
70 boolean hasDefaultCtor = true;
71 boolean hasPublicCtor = false;
72
73 while (child != null) {
74 final int type = child.getType();
75 if (type == TokenTypes.METHOD_DEF
76 || type == TokenTypes.VARIABLE_DEF)
77 {
78 hasMethodOrField = true;
79 final DetailAST modifiers =
80 child.findFirstToken(TokenTypes.MODIFIERS);
81 final boolean isStatic =
82 modifiers.branchContains(TokenTypes.LITERAL_STATIC);
83 final boolean isPrivate =
84 modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
85
86 if (!isStatic && !isPrivate) {
87 hasNonStaticMethodOrField = true;
88 }
89 if (isStatic && !isPrivate) {
90 hasNonPrivateStaticMethodOrField = true;
91 }
92 }
93 if (type == TokenTypes.CTOR_DEF) {
94 hasDefaultCtor = false;
95 final DetailAST modifiers =
96 child.findFirstToken(TokenTypes.MODIFIERS);
97 if (!modifiers.branchContains(TokenTypes.LITERAL_PRIVATE)
98 && !modifiers.branchContains(TokenTypes.LITERAL_PROTECTED))
99 {
100 // treat package visible as public
101 // for the purpose of this Check
102 hasPublicCtor = true;
103 }
104
105 }
106 child = child.getNextSibling();
107 }
108
109 final boolean hasAccessibleCtor = hasDefaultCtor || hasPublicCtor;
110
111 // figure out if class extends java.lang.object directly
112 // keep it simple for now and get a 99% solution
113 // TODO: check for "extends java.lang.Object" and "extends Object"
114 // consider "import org.omg.CORBA.*"
115 final boolean extendsJLO = // J.Lo even made it into in our sources :-)
116 ast.findFirstToken(TokenTypes.EXTENDS_CLAUSE) == null;
117
118 final boolean isUtilClass = extendsJLO && hasMethodOrField
119 && !hasNonStaticMethodOrField && hasNonPrivateStaticMethodOrField;
120
121 if (isUtilClass && hasAccessibleCtor && !hasStaticModifier) {
122 log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY);
123 }
124 }
125
126 /**
127 * @param ast class definition for check.
128 * @return true if a given class declared as abstract.
129 */
130 private boolean isAbstract(DetailAST ast)
131 {
132 return ast.findFirstToken(TokenTypes.MODIFIERS)
133 .branchContains(TokenTypes.ABSTRACT);
134 }
135
136 /**
137 * @param ast class definition for check.
138 * @return true if a given class declared as static.
139 */
140 private boolean isStatic(DetailAST ast)
141 {
142 return ast.findFirstToken(TokenTypes.MODIFIERS)
143 .branchContains(TokenTypes.LITERAL_STATIC);
144 }
145 }