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.util.HashSet; 023import java.util.Set; 024 025import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.FullIdent; 029import com.puppycrawl.tools.checkstyle.api.TokenTypes; 030 031/** 032 * <div> 033 * Checks for redundant import statements. An import statement is 034 * considered redundant if: 035 * </div> 036 * <ul> 037 * <li>It is a duplicate of another import. This is, when a class or a module 038 * is imported more than once.</li> 039 * <li>The class non-statically imported is from the {@code java.lang} 040 * package, e.g. importing {@code java.lang.String}.</li> 041 * <li>The class non-statically imported is from the same package as the 042 * current package.</li> 043 * </ul> 044 * 045 * @since 3.0 046 */ 047@FileStatefulCheck 048public class RedundantImportCheck 049 extends AbstractCheck { 050 051 /** 052 * A key is pointing to the warning message text in "messages.properties" 053 * file. 054 */ 055 public static final String MSG_LANG = "import.lang"; 056 057 /** 058 * A key is pointing to the warning message text in "messages.properties" 059 * file. 060 */ 061 public static final String MSG_SAME = "import.same"; 062 063 /** 064 * A key is pointing to the warning message text in "messages.properties" 065 * file. 066 */ 067 public static final String MSG_DUPLICATE = "import.duplicate"; 068 069 /** Set of the imports. */ 070 private final Set<FullIdent> imports = new HashSet<>(); 071 /** Set of static and module imports. */ 072 private final Set<FullIdent> staticAndModuleImports = new HashSet<>(); 073 074 /** Name of package in file. */ 075 private String pkgName; 076 077 @Override 078 public void beginTree(DetailAST aRootAST) { 079 pkgName = null; 080 imports.clear(); 081 staticAndModuleImports.clear(); 082 } 083 084 @Override 085 public int[] getDefaultTokens() { 086 return getRequiredTokens(); 087 } 088 089 @Override 090 public int[] getAcceptableTokens() { 091 return getRequiredTokens(); 092 } 093 094 @Override 095 public int[] getRequiredTokens() { 096 return new int[] { 097 TokenTypes.IMPORT, 098 TokenTypes.STATIC_IMPORT, 099 TokenTypes.PACKAGE_DEF, 100 TokenTypes.MODULE_IMPORT, 101 }; 102 } 103 104 @Override 105 public void visitToken(DetailAST ast) { 106 if (ast.getType() == TokenTypes.PACKAGE_DEF) { 107 pkgName = FullIdent.createFullIdent( 108 ast.getLastChild().getPreviousSibling()).getText(); 109 } 110 else if (ast.getType() == TokenTypes.IMPORT) { 111 final FullIdent imp = FullIdent.createFullIdentBelow(ast); 112 final String importText = imp.getText(); 113 if (isFromPackage(importText, "java.lang")) { 114 log(ast, MSG_LANG, importText); 115 } 116 // imports from unnamed package are not allowed, 117 // so we are checking SAME rule only for named packages 118 else if (pkgName != null && isFromPackage(importText, pkgName)) { 119 log(ast, MSG_SAME, importText); 120 } 121 // Check for a duplicate import 122 imports.stream().filter(full -> importText.equals(full.getText())) 123 .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), importText)); 124 125 imports.add(imp); 126 } 127 else { 128 // Check for a duplicate static or module import 129 final DetailAST identNode = ast.getLastChild().getPreviousSibling(); 130 final FullIdent importFullIdent = FullIdent.createFullIdent(identNode); 131 final String importText = importFullIdent.getText(); 132 133 staticAndModuleImports 134 .stream() 135 .filter(existingImport -> importText.equals(existingImport.getText())) 136 .forEach(existingImport -> { 137 log(ast, MSG_DUPLICATE, existingImport.getLineNo(), importText); 138 }); 139 140 staticAndModuleImports.add(importFullIdent); 141 } 142 } 143 144 /** 145 * Determines if an import statement is for types from a specified package. 146 * 147 * @param importName the import name 148 * @param pkg the package name 149 * @return whether from the package 150 */ 151 private static boolean isFromPackage(String importName, String pkg) { 152 // imports from unnamed package are not allowed: 153 // https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5 154 // So '.' must be present in member name and we are not checking for it 155 final int index = importName.lastIndexOf('.'); 156 final String front = importName.substring(0, index); 157 return pkg.equals(front); 158 } 159 160}