Skip to content

Class name $Gson$Preconditions breaks module system when shading #2836

@overheadhunter

Description

@overheadhunter

Gson version

2.12.1

Java / Android version

JDK 21

Used tools

  • Maven 3.9.9
  • maven-shade-plugin 3.6.0

Description

In a downstream project we're trying to shade gson.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
        <shadedArtifactAttached>false</shadedArtifactAttached>
        <artifactSet>
            <includes>
                <include>com.google.code.gson:gson</include>
            </includes>
        </artifactSet>
        <relocations>
            <relocation>
                <pattern>com.google.gson</pattern>
                <shadedPattern>com.nimbusds.jose.shaded.gson</shadedPattern>
            </relocation>
        </relocations>
        <filters>
            <filter>
                <artifact>com.google.code.gson:gson</artifact>
                <excludes>
                    <exclude>**/module-info.class</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The class in question is correctly moved to com.nimbusds.jose.shaded.gson.internal.$Gson$Preconditions. However classes importing this are now broken and incorrectly do this:

import com.nimbusds.jose.shaded.gson.internal..Gson.Preconditions;

This leads to all kinds of follow-up bugs when trying to load these classes from the module path. Loading from class path is fine for some reason (I suspect the com.nimbusds.jose.shaded.gson.internal.. can't be matched by a corresponding exports statement in the module-info)

Expected behavior

relocated classes have a proper name

Actual behavior

the naming scheme breaks the shade plugin.

Reproduction steps

  1. Build https://bitbucket.org/connect2id/nimbus-jose-jwt/src/10.1/ (mvn package -DskipTests)
  2. Unpack resulting jar
  3. Inspect com.nimbusds.jose.shaded.gson.GsonBuilder

Exception stack trace

N/A

Clarification / Justification

I'm posting this in support of #1744. I am aware that you could argue this is an issue with the shade plugin. However, since other users reported this as a JLS violation I believe it is up to GSON to fix it.

According to 4efb133 the whole point of this naming scheme is to affect behaviour of some "IDE's auto import functionality". All modern IDEs have been respecting the module-info.java for several years now, so this rationale is obsolete, since com.nimbusds.jose.shaded.gson.internal is not exported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions