001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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.imports;
021
022import java.net.URI;
023import java.util.Set;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
029import com.puppycrawl.tools.checkstyle.api.DetailAST;
030import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
031import com.puppycrawl.tools.checkstyle.api.FullIdent;
032import com.puppycrawl.tools.checkstyle.api.TokenTypes;
033import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
034
035/**
036 * <div>
037 * Controls what can be imported in each package and file. Useful for ensuring
038 * that application layering rules are not violated, especially on large projects.
039 * </div>
040 *
041 * <p>
042 * You can control imports based on the package name or based on the file name.
043 * When controlling packages, all files and sub-packages in the declared package
044 * will be controlled by this check. To specify differences between a main package
045 * and a sub-package, you must define the sub-package inside the main package.
046 * When controlling file, only the file name is considered and only files processed by
047 * <a href="https://checkstyle.org/config.html#TreeWalker">TreeWalker</a>.
048 * The file's extension is ignored.
049 * </p>
050 *
051 * <p>
052 * Short description of the behaviour:
053 * </p>
054 * <ul>
055 * <li>
056 * Check starts checking from the longest matching subpackage (later 'current subpackage') or
057 * the first file name match described inside import control file to package defined in class file.
058 * <ul>
059 * <li>
060 * The longest matching subpackage is found by starting with the root package and
061 * examining if any of the sub-packages or file definitions match the current
062 * class' package or file name.
063 * </li>
064 * <li>
065 * If a file name is matched first, that is considered the longest match and becomes
066 * the current file/subpackage.
067 * </li>
068 * <li>
069 * If another subpackage is matched, then it's subpackages and file names are examined
070 * for the next longest match and the process repeats recursively.
071 * </li>
072 * <li>
073 * If no subpackages or file names are matched, the current subpackage is then used.
074 * </li>
075 * </ul>
076 * </li>
077 * <li>
078 * Order of rules in the same subpackage/root are defined by the order of declaration
079 * in the XML file, which is from top (first) to bottom (last).
080 * </li>
081 * <li>
082 * If there is matching allow/disallow rule inside the current file/subpackage
083 * then the Check returns the first "allowed" or "disallowed" message.
084 * </li>
085 * <li>
086 * If there is no matching allow/disallow rule inside the current file/subpackage
087 * then it continues checking in the parent subpackage.
088 * </li>
089 * <li>
090 * If there is no matching allow/disallow rule in any of the files/subpackages,
091 * including the root level (import-control), then the import is disallowed by default.
092 * </li>
093 * </ul>
094 *
095 * <p>
096 * The DTD for an import control XML document is at
097 * <a href="https://checkstyle.org/dtds/import_control_1_4.dtd">
098 * https://checkstyle.org/dtds/import_control_1_4.dtd</a>.
099 * It contains documentation on each of the elements and attributes.
100 * </p>
101 *
102 * <p>
103 * The check validates a XML document when it loads the document. To validate against
104 * the above DTD, include the following document type declaration in your XML document:
105 * </p>
106 * <div class="wrapper"><pre class="prettyprint"><code class="language-xml">
107 * &lt;!DOCTYPE import-control PUBLIC
108 *     "-//Checkstyle//DTD ImportControl Configuration 1.4//EN"
109 *     "https://checkstyle.org/dtds/import_control_1_4.dtd"&gt;
110 * </code></pre></div>
111 *
112 * <p>
113 * Notes:
114 * For a real-life import-control file example, look at the file called
115 * <a href="https://github.com/checkstyle/checkstyle/blob/master/config/import-control.xml">
116 * import-control.xml</a> which is part of the Checkstyle distribution.
117 * </p>
118 * <ul>
119 * <li>
120 * Property {@code file} - Specify the location of the file containing the
121 * import control configuration. It can be a regular file, URL or resource path.
122 * It will try loading the path as a URL first, then as a file, and finally as a resource.
123 * Type is {@code java.net.URI}.
124 * Default value is {@code null}.
125 * </li>
126 * <li>
127 * Property {@code path} - Specify the regular expression of file paths to which
128 * this check should apply. Files that don't match the pattern will not be checked.
129 * The pattern will be matched against the full absolute file path.
130 * Type is {@code java.util.regex.Pattern}.
131 * Default value is {@code ".*"}.
132 * </li>
133 * </ul>
134 *
135 * <p>
136 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
137 * </p>
138 *
139 * <p>
140 * Violation Message Keys:
141 * </p>
142 * <ul>
143 * <li>
144 * {@code import.control.disallowed}
145 * </li>
146 * <li>
147 * {@code import.control.missing.file}
148 * </li>
149 * <li>
150 * {@code import.control.unknown.pkg}
151 * </li>
152 * </ul>
153 *
154 * @since 4.0
155 */
156@FileStatefulCheck
157public class ImportControlCheck extends AbstractCheck implements ExternalResourceHolder {
158
159    /**
160     * A key is pointing to the warning message text in "messages.properties"
161     * file.
162     */
163    public static final String MSG_MISSING_FILE = "import.control.missing.file";
164
165    /**
166     * A key is pointing to the warning message text in "messages.properties"
167     * file.
168     */
169    public static final String MSG_UNKNOWN_PKG = "import.control.unknown.pkg";
170
171    /**
172     * A key is pointing to the warning message text in "messages.properties"
173     * file.
174     */
175    public static final String MSG_DISALLOWED = "import.control.disallowed";
176
177    /**
178     * A part of message for exception.
179     */
180    private static final String UNABLE_TO_LOAD = "Unable to load ";
181
182    /**
183     * Specify the location of the file containing the import control configuration.
184     * It can be a regular file, URL or resource path. It will try loading the path
185     * as a URL first, then as a file, and finally as a resource.
186     */
187    private URI file;
188
189    /**
190     * Specify the regular expression of file paths to which this check should apply.
191     * Files that don't match the pattern will not be checked. The pattern will
192     * be matched against the full absolute file path.
193     */
194    private Pattern path = Pattern.compile(".*");
195    /** Whether to process the current file. */
196    private boolean processCurrentFile;
197
198    /** The root package controller. */
199    private PkgImportControl root;
200    /** The package doing the import. */
201    private String packageName;
202    /** The file name doing the import. */
203    private String fileName;
204
205    /**
206     * The package controller for the current file. Used for performance
207     * optimisation.
208     */
209    private AbstractImportControl currentImportControl;
210
211    @Override
212    public int[] getDefaultTokens() {
213        return getRequiredTokens();
214    }
215
216    @Override
217    public int[] getAcceptableTokens() {
218        return getRequiredTokens();
219    }
220
221    @Override
222    public int[] getRequiredTokens() {
223        return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT, };
224    }
225
226    @Override
227    public void beginTree(DetailAST rootAST) {
228        currentImportControl = null;
229        final String fullFileName = getFilePath();
230        processCurrentFile = path.matcher(fullFileName).find();
231        fileName = CommonUtil.getFileNameWithoutExtension(fullFileName);
232    }
233
234    @Override
235    public void visitToken(DetailAST ast) {
236        if (processCurrentFile) {
237            if (ast.getType() == TokenTypes.PACKAGE_DEF) {
238                if (root == null) {
239                    log(ast, MSG_MISSING_FILE);
240                }
241                else {
242                    packageName = getPackageText(ast);
243                    currentImportControl = root.locateFinest(packageName, fileName);
244                    if (currentImportControl == null) {
245                        log(ast, MSG_UNKNOWN_PKG);
246                    }
247                }
248            }
249            else if (currentImportControl != null) {
250                final String importText = getImportText(ast);
251                final AccessResult access = currentImportControl.checkAccess(packageName, fileName,
252                        importText);
253                if (access != AccessResult.ALLOWED) {
254                    log(ast, MSG_DISALLOWED, importText);
255                }
256            }
257        }
258    }
259
260    @Override
261    public Set<String> getExternalResourceLocations() {
262        return Set.of(file.toASCIIString());
263    }
264
265    /**
266     * Returns package text.
267     *
268     * @param ast PACKAGE_DEF ast node
269     * @return String that represents full package name
270     */
271    private static String getPackageText(DetailAST ast) {
272        final DetailAST nameAST = ast.getLastChild().getPreviousSibling();
273        return FullIdent.createFullIdent(nameAST).getText();
274    }
275
276    /**
277     * Returns import text.
278     *
279     * @param ast ast node that represents import
280     * @return String that represents importing class
281     */
282    private static String getImportText(DetailAST ast) {
283        final FullIdent imp;
284        if (ast.getType() == TokenTypes.IMPORT) {
285            imp = FullIdent.createFullIdentBelow(ast);
286        }
287        else {
288            // know it is a static import
289            imp = FullIdent.createFullIdent(ast
290                    .getFirstChild().getNextSibling());
291        }
292        return imp.getText();
293    }
294
295    /**
296     * Setter to specify the location of the file containing the import control configuration.
297     * It can be a regular file, URL or resource path. It will try loading the path
298     * as a URL first, then as a file, and finally as a resource.
299     *
300     * @param uri the uri of the file to load.
301     * @throws IllegalArgumentException on error loading the file.
302     * @since 4.0
303     */
304    public void setFile(URI uri) {
305        // Handle empty param
306        if (uri != null) {
307            try {
308                root = ImportControlLoader.load(uri);
309                file = uri;
310            }
311            catch (CheckstyleException exc) {
312                throw new IllegalArgumentException(UNABLE_TO_LOAD + uri, exc);
313            }
314        }
315    }
316
317    /**
318     * Setter to specify the regular expression of file paths to which this check should apply.
319     * Files that don't match the pattern will not be checked. The pattern will be matched
320     * against the full absolute file path.
321     *
322     * @param pattern the file path regex this check should apply to.
323     * @since 7.5
324     */
325    public void setPath(Pattern pattern) {
326        path = pattern;
327    }
328}