-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jarjar shouldn't mangle classes when no meaningful transformation app…
…lied (#52) Fixes #47 In case big module and all it dependent jars are going through jarjar shading, there will probably be dependent jars which are not actually modified by shading. However because they go through jarjar, it unpacks, revisits, and repacks the class back resulting a technically different class from before shading (even class size can be different from original). This upsets classpath checkers (i.e. checking that runtime classpath has at most 1 unique class version). I propose to add a change so that jarjar can detect whether the meaningful transformation to class file has happened and if not, the class is unmodified. This pull request introduces following new concepts: - TracingRemapper: extension of Remapper with capability of tracking if the change was applied; - RemappingJarProcessor: extension of JarProcessor focusing on TracingRemapper-based processors. It assumes ownership of TracingRemapper, and will verify if the mentioned remapper did any transformation.
- Loading branch information
Showing
14 changed files
with
177 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
jarjar/src/main/java/com/eed3si9n/jarjar/TracingRemapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.eed3si9n.jarjar; | ||
|
||
import org.objectweb.asm.commons.Remapper; | ||
|
||
public abstract class TracingRemapper extends Remapper { | ||
/** | ||
* Obtain the fresh copy of this object. | ||
*/ | ||
public abstract TracingRemapper copy(); | ||
|
||
/** | ||
* Check if this instance remapped something already. | ||
*/ | ||
public abstract boolean hasChanges(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
jarjar/src/main/java/com/eed3si9n/jarjar/util/RemappingJarProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.eed3si9n.jarjar.util; | ||
|
||
|
||
import com.eed3si9n.jarjar.TracingRemapper; | ||
import org.objectweb.asm.commons.Remapper; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Extension of JarProcessor for building processors around TracingRemapper. | ||
* | ||
* This class will track if TracingRemapper recorded doing any changes, and if it didn't, | ||
* it will make sure that the underlying bytes are unmodified. | ||
* | ||
* This helps to avoid bytecode modifications when there was no actual changes | ||
* to do. | ||
*/ | ||
public abstract class RemappingJarProcessor implements JarProcessor { | ||
private TracingRemapper remapper; | ||
|
||
public RemappingJarProcessor(TracingRemapper remapper) { | ||
this.remapper = remapper; | ||
} | ||
|
||
public final boolean process(EntryStruct struct) throws IOException { | ||
TracingRemapper structRemapper = remapper.copy(); | ||
EntryStruct origStruct = struct.copy(); | ||
|
||
if (!processImpl(struct, structRemapper)) { | ||
return false; | ||
} | ||
|
||
if (!structRemapper.hasChanges()) { | ||
struct.data = origStruct.data; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected abstract boolean processImpl(EntryStruct struct, Remapper remapper) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
jarjar/src/test/java/com/eed3si9n/jarjar/UnmodifiedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.eed3si9n.jarjar; | ||
|
||
import com.eed3si9n.jarjar.util.EntryStruct; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static com.eed3si9n.jarjar.MethodRewriterTest.readInputStream; | ||
|
||
public class UnmodifiedTest { | ||
@Test | ||
public void testNotModified() throws Exception { | ||
Rule rule = new Rule(); | ||
rule.setPattern("com.abc"); | ||
rule.setResult("com.def"); | ||
|
||
MainProcessor mp = new MainProcessor(Arrays.asList(rule), false, false, "move"); | ||
|
||
EntryStruct entryStruct = new EntryStruct(); | ||
entryStruct.name = "BigtableIO$Write.class"; | ||
entryStruct.skipTransform = false; | ||
entryStruct.time = 0; | ||
entryStruct.data = | ||
readInputStream( | ||
getClass().getResourceAsStream("/com/eed3si9n/jarjar/BigtableIO$Write.class")); | ||
|
||
EntryStruct orig = entryStruct.copy(); | ||
|
||
mp.process(entryStruct); | ||
|
||
Assert.assertEquals(entryStruct.data, orig.data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters