You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
shortenFullyQualifiedTypes produces uncompilable code when the simple name collides with a type in the same compilation unit #3033
shortenFullyQualifiedTypes shortens a fully-qualified reference into the simple name of a type declared in the same compilation unit. The result does not compile.
Reproducer
spotless-maven-plugin 3.10.0, with shortenFullyQualifiedTypes as the only configured step:
$ javac src/main/java/org/codehaus/plexus/archiver/jar/Manifest.java
src/main/java/org/codehaus/plexus/archiver/jar/Manifest.java:3: error: Manifest is already defined in this compilation unit
import java.util.jar.Manifest;
^
1 error
Two separate problems in the output. The single-type-import collides with a top-level type declared in the same file, which JLS 7.5.1 makes a compile-time error on its own, and the class now extends itself.
JarArchiver.java:[226,61] incompatible types:
java.util.jar.Manifest cannot be converted to org.codehaus.plexus.archiver.jar.Manifest
The same run over plexus-utils and plexus-xml was correct and useful, so the trigger is narrow: a project that declares a type whose simple name matches a JDK type it also references by qualified name. That pattern is common in libraries that wrap or extend a JDK class, which is exactly where fully-qualified references tend to be written deliberately.
Suggested behavior
Leave a qualified reference alone when the simple name would resolve to a different type in scope, including a type declared in the same compilation unit. The qualification is carrying meaning in that case rather than being redundant.
Without having tested, the proposed solution misses a similar case that might cause issues: if a class uses two imported classes with the same name from different packages, e.g., domain.Order and dto.Order.
The above scenario results in the Spotless adding an import of java.io.RandomAccessFile and it results in compilation failures, as the test.reprod1.RandomAccessFile should be used in the class definition instead.
UPDATE: Workaround for the above issue
Use fully qualified types for all the RandomAccessFile usages in such class, that way the step recognizes it properly and does not import any of those types.
Activity
klafbang commentedon Aug 25, 2026
Without having tested, the proposed solution misses a similar case that might cause issues: if a class uses two imported classes with the same name from different packages, e.g., domain.Order and dto.Order.
jonesbusy commentedon Aug 25, 2026
I confirm this is an issue
Seen on https://github.com/oras-project/oras-java/pull/857/changes
jonesbusy commentedon Aug 25, 2026
MIght be fixed by #3031
Maciej-Sitarz-IBM commentedon Aug 28, 2026
The above mentioned PR was merged and release:
Unfortunately one issue still remains in the project I tested.
Scenario:
RandomAccessFiledefined intest.reprod1packageClassAdefined intest.reprod1packageClassAextendstest.reprod1.RandomAccessFileClassAusesjava.io.RandomAccessFile(has fields of type)Sample code (not sure if that would compile...):
The above scenario results in the Spotless adding an import of
java.io.RandomAccessFileand it results in compilation failures, as thetest.reprod1.RandomAccessFileshould be used in the class definition instead.UPDATE:
Workaround for the above issue
Use fully qualified types for all the
RandomAccessFileusages in such class, that way the step recognizes it properly and does not import any of those types.Maciej-Sitarz-IBM commentedon Aug 28, 2026
^^ FYI @maxandersen
maxandersen commentedon Aug 28, 2026
I think the or linked fixes that missed cornercase. Give it a try.