001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.design;
021
022import java.util.Map;
023import java.util.SortedMap;
024import java.util.TreeMap;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
031
032/**
033 * <p>
034 * Checks that each top-level class, interface
035 * or enum resides in a source file of its own.
036 * Official description of a 'top-level' term:
037 * <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.6">
038 * 7.6. Top Level Type Declarations</a>. If file doesn't contains
039 * public class, enum or interface, top-level type is the first type in file.
040 * </p>
041 * <p>
042 * An example of check's configuration:
043 * </p>
044 * <pre>
045 * &lt;module name=&quot;OneTopLevelClass&quot;/&gt;
046 * </pre>
047 * <p>
048 * <b>ATTENTION:</b> This Check does not support customization of validated tokens,
049 * so do not use the "tokens" property.
050 * </p>
051 * <p>
052 * An example of code with violations:
053 * </p>
054 * <pre>
055 * public class Foo{
056 *   //methods
057 * }
058 *
059 * class Foo2{
060 *   //methods
061 * }
062 * </pre>
063 * <p>
064 * An example of code without public top-level type:
065 * </p>
066 * <pre>
067 * class Foo{ // top-level class
068 *   //methods
069 * }
070 *
071 * class Foo2{
072 *   //methods
073 * }
074 * </pre>
075 * <p>
076 * An example of code without violations:
077 * </p>
078 * <pre>
079 * public class Foo{
080 *   //methods
081 * }
082 * </pre>
083 *
084 * @since 5.8
085 */
086@FileStatefulCheck
087public class OneTopLevelClassCheck extends AbstractCheck {
088
089    /**
090     * A key is pointing to the warning message text in "messages.properties"
091     * file.
092     */
093    public static final String MSG_KEY = "one.top.level.class";
094
095    /**
096     * True if a java source file contains a type
097     * with a public access level modifier.
098     */
099    private boolean publicTypeFound;
100
101    /** Mapping between type names and line numbers of the type declarations.*/
102    private final SortedMap<Integer, String> lineNumberTypeMap = new TreeMap<>();
103
104    @Override
105    public int[] getDefaultTokens() {
106        return getRequiredTokens();
107    }
108
109    @Override
110    public int[] getAcceptableTokens() {
111        return getRequiredTokens();
112    }
113
114    // ZERO tokens as Check do Traverse of Tree himself, he does not need to subscribed to Tokens
115    @Override
116    public int[] getRequiredTokens() {
117        return CommonUtil.EMPTY_INT_ARRAY;
118    }
119
120    @Override
121    public void beginTree(DetailAST rootAST) {
122        publicTypeFound = false;
123        lineNumberTypeMap.clear();
124
125        DetailAST currentNode = rootAST;
126        while (currentNode != null) {
127            if (currentNode.getType() == TokenTypes.CLASS_DEF
128                    || currentNode.getType() == TokenTypes.ENUM_DEF
129                    || currentNode.getType() == TokenTypes.INTERFACE_DEF) {
130                if (isPublic(currentNode)) {
131                    publicTypeFound = true;
132                }
133                else {
134                    final String typeName = currentNode
135                            .findFirstToken(TokenTypes.IDENT).getText();
136                    lineNumberTypeMap.put(currentNode.getLineNo(), typeName);
137                }
138            }
139            currentNode = currentNode.getNextSibling();
140        }
141    }
142
143    @Override
144    public void finishTree(DetailAST rootAST) {
145        if (!lineNumberTypeMap.isEmpty()) {
146            if (!publicTypeFound) {
147                // skip first top-level type.
148                lineNumberTypeMap.remove(lineNumberTypeMap.firstKey());
149            }
150
151            for (Map.Entry<Integer, String> entry
152                    : lineNumberTypeMap.entrySet()) {
153                log(entry.getKey(), MSG_KEY, entry.getValue());
154            }
155        }
156    }
157
158    /**
159     * Checks if a type is public.
160     * @param typeDef type definition node.
161     * @return true if a type has a public access level modifier.
162     */
163    private static boolean isPublic(DetailAST typeDef) {
164        final DetailAST modifiers =
165                typeDef.findFirstToken(TokenTypes.MODIFIERS);
166        return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null;
167    }
168
169}