Commit 5c61bc1c authored by Eric Bruneton's avatar Eric Bruneton

Merge branch '317785-non-regression-test' into 'master'

Add a non-regression test for issue #317785.

See merge request !123
parents 5751cebc 0f24cf32
Pipeline #772 passed with stage
in 6 minutes and 2 seconds
......@@ -251,38 +251,12 @@ public class ClassRemapperTest extends AsmTest implements Opcodes {
/** Tests that classes transformed with a ClassRemapper can be loaded and instantiated. */
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
public void testRemapLoadAndInstantiate(PrecompiledClass classParameter, Api apiParameter) {
String internalName = classParameter.getInternalName();
String remappedInternalName =
internalName.equals("module-info") ? internalName : internalName.toUpperCase();
public void testRemapLoadAndInstantiate(
final PrecompiledClass classParameter, final Api apiParameter) {
ClassReader classReader = new ClassReader(classParameter.getBytes());
ClassWriter classWriter = new ClassWriter(0);
Remapper upperCaseRemapper =
new Remapper() {
@Override
public String mapMethodName(String owner, String name, String desc) {
if (name.equals("<init>") || name.equals("<clinit>")) {
return name;
}
return owner.equals(internalName) ? name.toUpperCase() : name;
}
@Override
public String mapInvokeDynamicMethodName(String name, String desc) {
return name.toUpperCase();
}
@Override
public String mapFieldName(String owner, String name, String desc) {
return owner.equals(internalName) ? name.toUpperCase() : name;
}
@Override
public String map(String typeName) {
return typeName.equals(internalName) ? remappedInternalName : typeName;
}
};
UpperCaseRemapper upperCaseRemapper = new UpperCaseRemapper(classParameter.getInternalName());
ClassRemapper classRemapper =
new ClassRemapper(apiParameter.value(), classWriter, upperCaseRemapper);
if (classParameter.isMoreRecentThan(apiParameter)) {
......@@ -291,8 +265,75 @@ public class ClassRemapperTest extends AsmTest implements Opcodes {
}
classReader.accept(classRemapper, 0);
byte[] classFile = classWriter.toByteArray();
assertThat(() -> loadAndInstantiate(remappedInternalName.replace('/', '.'), classFile))
assertThat(() -> loadAndInstantiate(upperCaseRemapper.getRemappedClassName(), classFile))
.succeedsOrThrows(UnsupportedClassVersionError.class)
.when(classParameter.isMoreRecentThanCurrentJdk());
}
/**
* Tests that classes transformed with a ClassNode and ClassRemapper can be loaded and
* instantiated.
*/
@ParameterizedTest
@MethodSource(ALL_CLASSES_AND_ALL_APIS)
public void testRemapLoadAndInstantiateWithTreeApi(
final PrecompiledClass classParameter, final Api apiParameter) {
ClassNode classNode = new ClassNode();
new ClassReader(classParameter.getBytes()).accept(classNode, 0);
ClassWriter classWriter = new ClassWriter(0);
UpperCaseRemapper upperCaseRemapper = new UpperCaseRemapper(classParameter.getInternalName());
ClassRemapper classRemapper =
new ClassRemapper(apiParameter.value(), classWriter, upperCaseRemapper);
if (classParameter.isMoreRecentThan(apiParameter)) {
assertThrows(RuntimeException.class, () -> classNode.accept(classRemapper));
return;
}
classNode.accept(classRemapper);
byte[] classFile = classWriter.toByteArray();
assertThat(() -> loadAndInstantiate(upperCaseRemapper.getRemappedClassName(), classFile))
.succeedsOrThrows(UnsupportedClassVersionError.class)
.when(classParameter.isMoreRecentThanCurrentJdk());
}
static class UpperCaseRemapper extends Remapper {
private final String internalClassName;
private final String remappedInternalClassName;
public UpperCaseRemapper(final String internalClassName) {
this.internalClassName = internalClassName;
this.remappedInternalClassName =
internalClassName.equals("module-info")
? internalClassName
: internalClassName.toUpperCase();
}
String getRemappedClassName() {
return remappedInternalClassName.replace('/', '.');
}
@Override
public String mapMethodName(final String owner, final String name, final String descriptor) {
if (name.equals("<init>") || name.equals("<clinit>")) {
return name;
}
return owner.equals(internalClassName) ? name.toUpperCase() : name;
}
@Override
public String mapInvokeDynamicMethodName(final String name, final String descriptor) {
return name.toUpperCase();
}
@Override
public String mapFieldName(final String owner, final String name, final String descriptor) {
return owner.equals(internalClassName) ? name.toUpperCase() : name;
}
@Override
public String map(final String typeName) {
return typeName.equals(internalClassName) ? remappedInternalClassName : typeName;
}
}
}
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