Commit e5be6d9d authored by Eric Bruneton's avatar Eric Bruneton

Fix a bug with module remapping and add a test for this. Also improve the code...

Fix a bug with module remapping and add a test for this. Also improve the code coverage of SerialVersionUIDAdder.
parent fdd16f22
Pipeline #1299 passed with stage
in 6 minutes and 33 seconds
......@@ -119,7 +119,7 @@ public class ClassRemapper extends ClassVisitor {
@Override
public void visitAttribute(final Attribute attribute) {
if (attribute instanceof ModuleHashesAttribute) {
ModuleHashesAttribute moduleHashesAttribute = new ModuleHashesAttribute();
ModuleHashesAttribute moduleHashesAttribute = (ModuleHashesAttribute) attribute;
List<String> modules = moduleHashesAttribute.modules;
for (int i = 0; i < modules.size(); ++i) {
modules.set(i, remapper.mapModuleName(modules.get(i)));
......
......@@ -35,6 +35,7 @@ import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
......@@ -447,7 +448,18 @@ public class SerialVersionUIDAdder extends ClassVisitor {
throws IOException {
int size = itemCollection.size();
Item[] items = itemCollection.toArray(new Item[size]);
Arrays.sort(items);
Arrays.sort(
items,
new Comparator<Item>() {
@Override
public int compare(final Item item1, final Item item2) {
int result = item1.name.compareTo(item2.name);
if (result == 0) {
result = item1.descriptor.compareTo(item2.descriptor);
}
return result;
}
});
for (Item item : items) {
dataOutputStream.writeUTF(item.name);
dataOutputStream.writeInt(item.access);
......@@ -459,7 +471,7 @@ public class SerialVersionUIDAdder extends ClassVisitor {
// Inner classes
// -----------------------------------------------------------------------------------------------
private static final class Item implements Comparable<Item> {
private static final class Item {
final String name;
final int access;
......@@ -470,27 +482,5 @@ public class SerialVersionUIDAdder extends ClassVisitor {
this.access = access;
this.descriptor = descriptor;
}
public int compareTo(final Item other) {
int result = name.compareTo(other.name);
if (result == 0) {
result = descriptor.compareTo(other.descriptor);
}
return result;
}
@Override
public boolean equals(final Object other) {
if (other instanceof Item) {
Item item = (Item) other;
return name.equals(item.name) && descriptor.equals(item.descriptor);
}
return false;
}
@Override
public int hashCode() {
return (name + descriptor).hashCode();
}
}
}
......@@ -31,6 +31,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.objectweb.asm.test.Assertions.assertThat;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
......@@ -57,6 +59,26 @@ public class ClassRemapperTest extends AsmTest {
assertEquals("new/pkg/C", classNode.name);
}
@Test
public void testRenameModuleHashes() {
ClassNode classNode = new ClassNode();
ClassRemapper classRemapper =
new ClassRemapper(
classNode,
new Remapper() {
@Override
public String mapModuleName(String name) {
return "new." + name;
}
});
classRemapper.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "C", null, "java/lang/Object", null);
classRemapper.visitAttribute(
new ModuleHashesAttribute("algorithm", Arrays.asList("pkg.C"), Arrays.asList(new byte[0])));
assertEquals("C", classNode.name);
assertEquals("new.pkg.C", ((ModuleHashesAttribute) classNode.attrs.get(0)).modules.get(0));
}
/** Tests that classes transformed with a ClassRemapper can be loaded and instantiated. */
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
......
......@@ -72,7 +72,7 @@ public class SerialVersionUIDAdderTest extends AsmTest {
@Test
public void testClass() throws Throwable {
long actualSvuid = computeSerialVersionUID(SerialVersionClass.class.getName());
assertEquals(4737241769335595888L, actualSvuid);
assertEquals(-6502746299017468033L, actualSvuid);
}
@Test
......@@ -121,6 +121,9 @@ class SerialVersionClass implements Serializable {
protected static final int aField = 32;
static {
}
public static Object[] aMethod() {
return null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment