@@ -167,7 +167,7 @@ private static boolean isOwnPackage(ImportTree importTree, String name, String c
167167 return false ;
168168 }
169169 String qualifier = name .endsWith (".*" ) ? name .substring (0 , name .length () - 2 ) : name .substring (0 , Math .max (0 , name .lastIndexOf ('.' )));
170- return qualifier .equals (currentPackage ) && !name .endsWith (".*" ) || name .equals (currentPackage + ".*" );
170+ return ( qualifier .equals (currentPackage ) && !name .endsWith (".*" ) ) || name .equals (currentPackage + ".*" );
171171 }
172172
173173 private boolean isUsed (ImportTree importTree , String name ) {
@@ -176,25 +176,31 @@ private boolean isUsed(ImportTree importTree, String name) {
176176 }
177177 boolean onDemand = name .endsWith (".*" );
178178 String qualifier = onDemand ? name .substring (0 , name .length () - 2 ) : name .substring (0 , Math .max (0 , name .lastIndexOf ('.' )));
179- String simple = name .substring (name .lastIndexOf ('.' ) + 1 );
180179 for (IdentifierTree identifier : identifiers ) {
181180 Symbol symbol = identifier .symbol ();
182- if (!symbol .isUnknown () && symbol .declaration () != null ) {
183- // found in the scope of the file, the import is not consulted
184- continue ;
185- }
186- if (onDemand ) {
187- if (declaredIn (symbol , qualifier , importTree .isStatic ())) {
188- return true ;
189- }
190- } else if (identifier .name ().equals (simple )
191- && (symbol .isUnknown () || (importTree .isStatic () ? isMemberOf (symbol , qualifier ) : denotesType (symbol , name )))) {
181+ // a symbol found in the scope of the file does not consult the import
182+ boolean inScope = !symbol .isUnknown () && symbol .declaration () != null ;
183+ if (!inScope && uses (importTree , name , qualifier , identifier )) {
192184 return true ;
193185 }
194186 }
195187 return false ;
196188 }
197189
190+ private boolean uses (ImportTree importTree , String name , String qualifier , IdentifierTree identifier ) {
191+ Symbol symbol = identifier .symbol ();
192+ if (name .endsWith (".*" )) {
193+ return declaredIn (symbol , qualifier , importTree .isStatic ());
194+ }
195+ if (!identifier .name ().equals (name .substring (name .lastIndexOf ('.' ) + 1 ))) {
196+ return false ;
197+ }
198+ if (symbol .isUnknown ()) {
199+ return true ;
200+ }
201+ return importTree .isStatic () ? isMemberOf (symbol , qualifier ) : denotesType (symbol , name );
202+ }
203+
198204 /** A static import also brings in the members the named type inherits. */
199205 private boolean isMemberOf (Symbol symbol , String qualifier ) {
200206 if (!(symbol .owner () instanceof Symbol .TypeSymbol owner )) {
@@ -221,12 +227,15 @@ private static boolean declaredIn(Symbol symbol, String qualifier, boolean isSta
221227 if (isStatic ) {
222228 return symbol .owner () instanceof Symbol .TypeSymbol owner && dottedType (owner ).equals (qualifier );
223229 }
224- Symbol type = symbol .isTypeSymbol () ? symbol : ("<init>" .equals (symbol .name ()) ? symbol .owner () : null );
225- return type instanceof Symbol .TypeSymbol && !type .isUnknown () && dottedType (type ).startsWith (qualifier + "." );
230+ Symbol type = symbol ;
231+ if ("<init>" .equals (symbol .name ())) {
232+ type = symbol .owner ();
233+ }
234+ return type .isTypeSymbol () && !type .isUnknown () && dottedType (type ).startsWith (qualifier + "." );
226235 }
227236
228237 private static String dottedType (Symbol symbol ) {
229- return (( Symbol . TypeSymbol ) symbol ) .type ().fullyQualifiedName ().replace ('$' , '.' );
238+ return symbol .type ().fullyQualifiedName ().replace ('$' , '.' );
230239 }
231240
232241 private static String dotted (Tree tree ) {
0 commit comments