shadow icon indicating copy to clipboard operation
shadow copied to clipboard

Support `$` prefix for private/non-importable classes.

Open leventov opened this issue 9 years ago • 6 comments

Is there a way to rename shaded classes (prefix with $), in order to prevent importing on user's side? Like https://github.com/immutables/maven-shade-plugin#1-relocation-with--uglyfication

leventov avatar May 03 '16 15:05 leventov

Have you tried exactly that?

shadowJar {
  relocate 'com.google.common', 'com.my.internal.$guava$'
}

johnrengelman avatar May 04 '16 13:05 johnrengelman

It leads to

Caused by: java.lang.IllegalArgumentException: Illegal group reference
        at java.util.regex.Matcher.appendReplacement(Matcher.java:857)
        at java.util.regex.Matcher.replaceFirst(Matcher.java:1004)
        at java.lang.String.replaceFirst(String.java:2178)
        at java_lang_String$replaceFirst$8.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
        at com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator.relocatePath(SimpleRelocator.groovy:171)
        at com.github.jengelman.gradle.plugins.shadow.relocation.Relocator$relocatePath$0.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at com.github.jengelman.gradle.plugins.shadow.impl.RelocatorRemapper.map(RelocatorRemapper.groovy:95)
        at org.objectweb.asm.commons.Remapper.mapDesc(Unknown Source)
        at org.objectweb.asm.commons.RemappingClassAdapter.visitAnnotation(Unknown Source)
        at org.objectweb.asm.ClassReader.accept(Unknown Source)
        at org.objectweb.asm.ClassReader.accept(Unknown Source)
        at org.objectweb.asm.ClassReader$accept.call(Unknown Source)
        at com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction$StreamAction.remapClass(ShadowCopyAction.groovy:269)
        ... 153 more

leventov avatar May 07 '16 08:05 leventov

Try escaping the symbols - "$"

johnrengelman avatar May 07 '16 12:05 johnrengelman

@johnrengelman

  1. Need double escape like relocate 'com.google.common', 'com.my.internal.\\$guava\\$'
  2. It doesn't rename classes, it merely renames the package to $guava$ (a valid package name), but doesn't prepend class names with $.

leventov avatar May 07 '16 19:05 leventov

That doesn't appear to be a standard feature of Shade. Does having $ in the package not do the same thing?

johnrengelman avatar May 09 '16 13:05 johnrengelman

@johnrengelman no. When I type in IDE Immu... which is not imported yet, IDE suggest me to import com.google.common.collect.ImmutableList, as well as com.my.internal.$guava$.ImmutableList. If in shaded version I have a class name prefixed i. e. $ImmutableList, IDE won't suggest it, because class name prefix doesn't match.

See https://github.com/google/auto/issues/167, https://github.com/google/auto/issues/263

leventov avatar May 09 '16 13:05 leventov

I tested the pattern

relocate('com.google.common', 'com.my.internal.\\$guava\\$')

for the example from https://github.com/immutables/maven-shade-plugin#1-relocation-with--uglyfication

The com.google.common.Main will be relocated to com.my.internal.$guava$.Main. See #1729.

Goooler avatar Sep 10 '25 13:09 Goooler

I tried to address this in #1729. Ideally, there should be an extra flag in SimpleRelocator to control this feature. However, since this is such an edge case, implementing it could introduce more complex scenarios to handle. I don’t believe it’s worth doing at this point.

Goooler avatar Sep 11 '25 02:09 Goooler