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

support exclude source path in relocation #780

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -24,6 +24,7 @@ import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContex
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocatePathContext
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.RelativeArchivePath
import groovy.util.logging.Slf4j
import org.objectweb.asm.commons.Remapper

import java.util.regex.Matcher
Expand All @@ -34,12 +35,14 @@ import java.util.regex.Pattern
*
* @author John Engelman
*/
@Slf4j
class RelocatorRemapper extends Remapper {

private final Pattern classPattern = Pattern.compile("(\\[*)?L(.+)")

List<Relocator> relocators
ShadowStats stats
public String currentFilePath

RelocatorRemapper(List<Relocator> relocators, ShadowStats stats) {
this.relocators = relocators
Expand All @@ -65,7 +68,13 @@ class RelocatorRemapper extends Remapper {
name = m.group(2)
}

for (Relocator r : relocators) {
for (Relocator r : relocators) {
boolean canRelocate = r.canRelocateSourceFile(currentFilePath)
if (!canRelocate) {
String info = String.format("skipped %s as src path %s excluded", r, currentFilePath)
log.debug(info)
continue
}
if (r.canRelocateClass(name)) {
RelocateClassContext classContext = RelocateClassContext.builder().className(name).stats(stats).build()
value = prefix + r.relocateClass(classContext) + suffix
Expand Down Expand Up @@ -97,6 +106,13 @@ class RelocatorRemapper extends Remapper {
}

for (Relocator r : relocators) {
boolean canRelocate = r.canRelocateSourceFile(currentFilePath)
if (!canRelocate) {
String info = String.format("skipped %s as src path %s excluded", r, currentFilePath)
log.debug(info)
continue
}

if (r.canRelocatePath(name)) {
RelocatePathContext pathContext = RelocatePathContext.builder().path(name).stats(stats).build()
value = prefix + r.relocatePath(pathContext) + 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 @@ -54,11 +56,11 @@ class SimpleRelocator implements Relocator {
}

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

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 +89,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 +102,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 +157,17 @@ class SimpleRelocator implements Relocator {
return false
}

private boolean isExcludedSource(String srcPath) {
if (srcPath != null && excludeSources != null && !excludeSources.isEmpty()) {
for (String excludeSource : excludeSources) {
if (srcPath.startsWith(excludeSource)) {
return true
}
}
}
return false
}

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

boolean canRelocateSourceFile(String sourceFilePath) {
return !isExcludedSource(sourceFilePath)
}

String relocatePath(RelocatePathContext context) {
String path = context.path
context.stats.relocate(pathPattern, shadedPathPattern)
Expand Down Expand Up @@ -242,4 +265,14 @@ class SimpleRelocator implements Relocator {
boolean getRawString() {
return rawString
}

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

@Override
public String toString() {
return String.format("Relocate[%s->%s][%s]", pattern, shadedPattern, excludeSources)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ class SimpleRelocatorTest extends TestCase {
stats = new ShadowStats()
}

void testIsExcludedSource() {
SimpleRelocator relocator

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

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
Loading