Uploaded image for project: 'Maven Shade Plugin'
  1. Maven Shade Plugin
  2. MSHADE-228

Shade can incorrectly re-write an InterfaceMethodRef in the constant pool to a MethodRef

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.3
    • Fix Version/s: 3.0.0
    • Labels:
      None

      Description

      Given an interface such as:

      public interface InvalidMethodRefExample
          {
          String getLocation();
      
          static InvalidMethodRefExample autoDetect()
              {
              return () -> "$Default$";
              }
          }
      

      javac correctly produces a bootstrap method that has a MethodHandle for its second argument to LambdaMetafactory.metafactory, and the MethodHandle is to an InterfaceMethodref.

      After running shade the InterfaceMethodref becomes a Methodref.

      This seems to have been the result for some time now but with Java 9 it appears that the java team have decided to enforce this CP type and will be a real problem when using shade and Java 9.

        Issue Links

          Activity

          Hide
          harveyraja Harvey Raja added a comment -

          Unfortunately I can not attach the maven project I have that shows the issue, but other than the interface in the description there is the pom file:

          <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>com.screwed</groupId>
            <artifactId>constant-pool</artifactId>
            <name>Screwed Constant Pool</name>
            <version>1.0-SNAPSHOT</version>
            <build>
              <plugins>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.5.1</version>
                  <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                  </configuration>
                </plugin>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-shade-plugin</artifactId>
                  <version>2.4.3</version>
                  <executions>
                    <execution>
                      <id>shade</id>
                      <phase>package</phase>
                      <goals>
                        <goal>shade</goal>
                      </goals>
                      <configuration>
                        <finalName>screwed-jar</finalName>
                        <shadedArtifactAttached>false</shadedArtifactAttached>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
                        <filters>
                          <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                              <exclude>META-INF/maven/**/*</exclude>
                            </excludes>
                          </filter>
                        </filters>
                        <!-- the presence of relocation will result in a ClassFile
                             re-write with an invalid MethodRef -->
                        <relocations>
                          <relocation>
                            <pattern>org.something</pattern>
                            <shadedPattern>com.screwed.something</shadedPattern>
                          </relocation>
                        </relocations>
                      </configuration>
                    </execution>
                  </executions>
                </plugin>
              </plugins>
            </build>
          </project>
          
          Show
          harveyraja Harvey Raja added a comment - Unfortunately I can not attach the maven project I have that shows the issue, but other than the interface in the description there is the pom file: <project xmlns= "http: //maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http: //maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelVersion>4.0.0</modelVersion> <groupId>com.screwed</groupId> <artifactId>constant-pool</artifactId> <name>Screwed Constant Pool</name> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <id>shade</id> <phase> package </phase> <goals> <goal>shade</goal> </goals> <configuration> <finalName>screwed-jar</finalName> <shadedArtifactAttached> false </shadedArtifactAttached> <createDependencyReducedPom> true </createDependencyReducedPom> <promoteTransitiveDependencies> true </promoteTransitiveDependencies> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/maven/**/*</exclude> </excludes> </filter> </filters> <!-- the presence of relocation will result in a ClassFile re-write with an invalid MethodRef --> <relocations> <relocation> <pattern>org.something</pattern> <shadedPattern>com.screwed.something</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
          Hide
          harveyraja Harvey Raja added a comment -

          You will be able to see the difference in the constant pool in the ClassFile generated by javac and the ClassFile generated by shade.

          You can also run against java 9 to see the runtime error:

          Exception in thread "main" java.lang.BootstrapMethodError: java.lang.IncompatibleClassChangeError: Inconsistent constant data for com/screwed/constantpool/InvalidMethodRefExample.lambda$autoDetect$0()Ljava/lang/String; at index 26
                  at com.screwed.constantpool.InvalidMethodRefExample.autoDetect(InvalidMethodRefExample.java:10)
                  at Test.main(Test.java:5)
          Caused by: java.lang.IncompatibleClassChangeError: Inconsistent constant data for com/screwed/constantpool/InvalidMethodRefExample.lambda$autoDetect$0()Ljava/lang/String; at index 26
                  ... 2 more
          
          Show
          harveyraja Harvey Raja added a comment - You will be able to see the difference in the constant pool in the ClassFile generated by javac and the ClassFile generated by shade. You can also run against java 9 to see the runtime error: Exception in thread "main" java.lang.BootstrapMethodError: java.lang.IncompatibleClassChangeError: Inconsistent constant data for com/screwed/constantpool/InvalidMethodRefExample.lambda$autoDetect$0()Ljava/lang/ String ; at index 26 at com.screwed.constantpool.InvalidMethodRefExample.autoDetect(InvalidMethodRefExample.java:10) at Test.main(Test.java:5) Caused by: java.lang.IncompatibleClassChangeError: Inconsistent constant data for com/screwed/constantpool/InvalidMethodRefExample.lambda$autoDetect$0()Ljava/lang/ String ; at index 26 ... 2 more
          Hide
          harveyraja Harvey Raja added a comment - - edited

          BTW - looks like I can trivially work around it by bumping the ASM version:

                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-shade-plugin</artifactId>
                  <dependencies>
                    <dependency>
                      <groupId>org.ow2.asm</groupId>
                      <artifactId>asm</artifactId>
                      <version>5.1</version>
                    </dependency>
                    <dependency>
                      <groupId>org.ow2.asm</groupId>
                      <artifactId>asm-commons</artifactId>
                      <version>5.1</version>
                    </dependency>
                  </dependencies>
          
          Show
          harveyraja Harvey Raja added a comment - - edited BTW - looks like I can trivially work around it by bumping the ASM version: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <dependencies> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>5.1</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-commons</artifactId> <version>5.1</version> </dependency> </dependencies>
          Hide
          rfscholte Robert Scholte added a comment -

          Is fixed as part of MSHADE-222

          Show
          rfscholte Robert Scholte added a comment - Is fixed as part of MSHADE-222

            People

            • Assignee:
              rfscholte Robert Scholte
              Reporter:
              harveyraja Harvey Raja
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development