001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.CommonUtils;
031
032/**
033 * Checks that each top-level class, interface
034 * or enum resides in a source file of its own.
035 * <p>
036 * Official description of a 'top-level' term:<a
037 * href="https://docs.oracle.com/javase/specs/jls/se8/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 code with violations:
043 * </p>
044 * <pre>{@code
045 * public class Foo{
046 *     //methods
047 * }
048 *
049 * class Foo2{
050 *     //methods
051 * }
052 * }</pre>
053 * <p>
054 * An example of code without top-level public type:
055 * </p>
056 * <pre>{@code
057 * class Foo{ //top-level class
058 *     //methods
059 * }
060 *
061 * class Foo2{
062 *     //methods
063 * }
064 * }</pre>
065 * <p>
066 * An example of check's configuration:
067 * </p>
068 * <pre>
069 * &lt;module name="OneTopLevelClass"/&gt;
070 * </pre>
071 *
072 * <p>
073 * An example of code without violations:
074 * </p>
075 * <pre>{@code
076 * public class Foo{
077 *     //methods
078 * }
079 * }</pre>
080 *
081 * <p> ATTENTION: This Check does not support customization of validated tokens,
082 *  so do not use the "tokens" property.
083 * </p>
084 *
085 * @author maxvetrenko
086 */
087@FileStatefulCheck
088public class OneTopLevelClassCheck extends AbstractCheck {
089
090    /**
091     * A key is pointing to the warning message text in "messages.properties"
092     * file.
093     */
094    public static final String MSG_KEY = "one.top.level.class";
095
096    /**
097     * True if a java source file contains a type
098     * with a public access level modifier.
099     */
100    private boolean publicTypeFound;
101
102    /** Mapping between type names and line numbers of the type declarations.*/
103    private final SortedMap<Integer, String> lineNumberTypeMap = new TreeMap<>();
104
105    @Override
106    public int[] getDefaultTokens() {
107        return getRequiredTokens();
108    }
109
110    @Override
111    public int[] getAcceptableTokens() {
112        return getRequiredTokens();
113    }
114
115    // ZERO tokens as Check do Traverse of Tree himself, he does not need to subscribed to Tokens
116    @Override
117    public int[] getRequiredTokens() {
118        return CommonUtils.EMPTY_INT_ARRAY;
119    }
120
121    @Override
122    public void beginTree(DetailAST rootAST) {
123        publicTypeFound = false;
124        lineNumberTypeMap.clear();
125
126        DetailAST currentNode = rootAST;
127        while (currentNode != null) {
128            if (currentNode.getType() == TokenTypes.CLASS_DEF
129                    || currentNode.getType() == TokenTypes.ENUM_DEF
130                    || currentNode.getType() == TokenTypes.INTERFACE_DEF) {
131                if (isPublic(currentNode)) {
132                    publicTypeFound = true;
133                }
134                else {
135                    final String typeName = currentNode
136                            .findFirstToken(TokenTypes.IDENT).getText();
137                    lineNumberTypeMap.put(currentNode.getLineNo(), typeName);
138                }
139            }
140            currentNode = currentNode.getNextSibling();
141        }
142    }
143
144    @Override
145    public void finishTree(DetailAST rootAST) {
146        if (!lineNumberTypeMap.isEmpty()) {
147            if (!publicTypeFound) {
148                // skip first top-level type.
149                lineNumberTypeMap.remove(lineNumberTypeMap.firstKey());
150            }
151
152            for (Map.Entry<Integer, String> entry
153                    : lineNumberTypeMap.entrySet()) {
154                log(entry.getKey(), MSG_KEY, entry.getValue());
155            }
156        }
157    }
158
159    /**
160     * Checks if a type is public.
161     * @param typeDef type definition node.
162     * @return true if a type has a public access level modifier.
163     */
164    private static boolean isPublic(DetailAST typeDef) {
165        final DetailAST modifiers =
166                typeDef.findFirstToken(TokenTypes.MODIFIERS);
167        return modifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null;
168    }
169
170}