Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add relocator source exclusion #890

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ yarn-error.log
src/docs/.vuepress/dist/
.DS_Store
jd-gui.cfg
lib/
bin/
.vscode/
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RelocatorRemapper extends Remapper {

List<Relocator> relocators
ShadowStats stats
String currentFilePath

RelocatorRemapper(List<Relocator> relocators, ShadowStats stats) {
this.relocators = relocators
Expand All @@ -66,6 +67,7 @@ class RelocatorRemapper extends Remapper {
}

for (Relocator r : relocators) {
if (!r.canRelocateSourceFile(currentFilePath)) continue
if (r.canRelocateClass(name)) {
RelocateClassContext classContext = RelocateClassContext.builder().className(name).stats(stats).build()
value = prefix + r.relocateClass(classContext) + suffix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ interface Relocator {
String relocateClass(RelocateClassContext context)

String applyToSourceContent(String sourceContent)

boolean canRelocateSourceFile(String sourceFilePath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class SimpleRelocator implements Relocator {
private final Set<String> includes

private final Set<String> excludes

private final Set<String> excludeSources

private final boolean rawString

Expand All @@ -57,8 +59,12 @@ class SimpleRelocator implements Relocator {
this(patt, shadedPattern, includes, excludes, false)
}

SimpleRelocator(String patt, String shadedPattern, List<String> includes, List<String> excludes, boolean rawString) {
this(patt, shadedPattern, includes, excludes, [], rawString)
}

SimpleRelocator(String patt, String shadedPattern, List<String> includes, List<String> excludes,
boolean rawString) {
List<String> excludeSources, boolean rawString) {
this.rawString = rawString

if (rawString) {
Expand Down Expand Up @@ -87,6 +93,7 @@ class SimpleRelocator implements Relocator {

this.includes = normalizePatterns(includes)
this.excludes = normalizePatterns(excludes)
this.excludeSources = normalizePatterns(excludeSources)
}

SimpleRelocator include(String pattern) {
Expand All @@ -99,6 +106,11 @@ class SimpleRelocator implements Relocator {
return this
}

SimpleRelocator excludeSource(String pattern) {
this.excludeSources.addAll normalizePatterns([pattern])
return this
}

private static Set<String> normalizePatterns(Collection<String> patterns) {
Set<String> normalized = null

Expand Down Expand Up @@ -149,6 +161,17 @@ class SimpleRelocator implements Relocator {
return false
}

private boolean isExcludedSource(String srcPath) {
if (excludeSources != null && !excludeSources.isEmpty() && srcPath != null) {
for (String excludeSource : excludeSources) {
if (SelectorUtils.matchPath(excludeSource, srcPath, '/', true)) {
return true
}
}
}
return false
}

boolean canRelocatePath(String path) {
if (rawString) {
return Pattern.compile(pathPattern).matcher(path).find()
Expand Down Expand Up @@ -182,6 +205,18 @@ class SimpleRelocator implements Relocator {
canRelocatePath(className.replace('.', '/'))
}

boolean canRelocateSourceFile(String srcPath) {
if (srcPath != null && srcPath.endsWith(".class")) {
// Safeguard against strings containing only ".class"
if (srcPath.length() == 6) {
return false
}
srcPath = srcPath.substring(0, srcPath.length() - 6)
Comment on lines +209 to +214
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract .class to a const value and eliminate these magic numbers?

}

return !isExcludedSource(srcPath)
}

String relocatePath(RelocatePathContext context) {
String path = context.path
context.stats.relocate(pathPattern, shadedPathPattern)
Expand Down Expand Up @@ -238,6 +273,11 @@ class SimpleRelocator implements Relocator {
return excludes
}

@Input
Set<String> getExcludeSources() {
return excludeSources
}

@Input
boolean getRawString() {
return rawString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ class ShadowCopyAction implements CopyAction {
// that use the constant pool to determine the dependencies of a class.
ClassWriter cw = new ClassWriter(0)

remapper.currentFilePath = path

ClassVisitor cv = new ClassRemapper(cw, remapper)

try {
Expand All @@ -355,6 +357,7 @@ class ShadowCopyAction implements CopyAction {
throw new GradleException("Error in ASM processing class " + path, ise)
} finally {
is.close()
remapper.currentFilePath = null
}

byte[] renamedClass = cw.toByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ class SimpleRelocatorTest extends TestCase {
stats = new ShadowStats()
}

void testCanRelocateSourceFile() {
SimpleRelocator relocator

relocator = new SimpleRelocator("org.foo", null, null, null)
relocator.excludeSource "org/apache/iceberg/spark/parquet/**"
relocator.excludeSource "org/apache/spark/sql/execution/datasources/parquet/**"

assertEquals(false, relocator.canRelocateSourceFile("org/apache/iceberg/spark/parquet/SparkNativeParquet.class"))
assertEquals(false, relocator.canRelocateSourceFile("org/apache/iceberg/spark/parquet/SparkNativeParquet\$.class"))
assertEquals(false, relocator.canRelocateSourceFile("org/apache/spark/sql/execution/datasources/parquet/v1.class"))
assertEquals(true, relocator.canRelocateSourceFile("org/foo/Class.class"))
}

void testCanRelocateSourceFileWithRegex() {
SimpleRelocator relocator

relocator = new SimpleRelocator("org.foo", null, null, null)
relocator.excludeSource "%regex[org/apache/iceberg/.*]"
relocator.excludeSource "%regex[org/apache/spark/.*]"

assertEquals(false, relocator.canRelocateSourceFile("org/apache/iceberg/spark/parquet/SparkNativeParquet.class"))
assertEquals(false, relocator.canRelocateSourceFile("org/apache/iceberg/spark/parquet/SparkNativeParquet\$.class"))
assertEquals(false, relocator.canRelocateSourceFile("org/apache/spark/sql/execution/datasources/parquet/v1.class"))
assertEquals(true, relocator.canRelocateSourceFile("org/foo/Class.class"))
}

void testCanRelocatePath() {
SimpleRelocator relocator

Expand Down