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