001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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 org.apache.commons.lang3.ArrayUtils;
027
028import com.puppycrawl.tools.checkstyle.api.Check;
029import com.puppycrawl.tools.checkstyle.api.DetailAST;
030import com.puppycrawl.tools.checkstyle.api.TokenTypes;
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="http://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 */
087public class OneTopLevelClassCheck extends Check {
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 getAcceptableTokens();
107    }
108
109    // ZERO tokens as Check do Traverse of Tree himself, he does not need to subscribed to Tokens
110    @Override
111    public int[] getAcceptableTokens() {
112        return ArrayUtils.EMPTY_INT_ARRAY;
113    }
114
115    @Override
116    public int[] getRequiredTokens() {
117        return getAcceptableTokens();
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}