Commit 10011e5e authored by Eric Bruneton's avatar Eric Bruneton

Merge branch 'add-class-writer-unit-test' into 'master'

Add a test to make sure ClassWriter.toByteArray() is updated when new ClassWriter fields are added.

See merge request asm/asm!161
parents f68b2145 6468f153
......@@ -27,12 +27,16 @@
// THE POSSIBILITY OF SUCH DAMAGE.
package org.objectweb.asm;
import static java.util.stream.Collectors.toSet;
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.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import org.junit.jupiter.api.Test;
......@@ -48,6 +52,52 @@ import org.objectweb.asm.test.AsmTest;
*/
public class ClassWriterTest extends AsmTest {
/**
* Tests that the non-static fields of ClassWriter are the expected ones. This test is designed to
* fail each time new fields are added to ClassWriter, and serves as a reminder to update the
* field reset logic in {@link ClassWriter#toByteArray()}, if needed, each time a new field is
* added.
*/
@Test
public void testInstanceFields() {
// IMPORTANT: if this fails, update the string list AND update the logic that resets the
// ClassWriter fields in ClassWriter.toByteArray(), if needed (this logic is used to do a
// ClassReader->ClassWriter round trip to remove the ASM specific instructions due to large
// forward jumps).
assertEquals(
new HashSet<String>(
Arrays.asList(
"version",
"symbolTable",
"accessFlags",
"thisClass",
"superClass",
"interfaceCount",
"interfaces",
"firstField",
"lastField",
"firstMethod",
"lastMethod",
"numberOfClasses",
"classes",
"enclosingClassIndex",
"enclosingMethodIndex",
"signatureIndex",
"sourceFileIndex",
"debugExtension",
"lastRuntimeVisibleAnnotation",
"lastRuntimeInvisibleAnnotation",
"lastRuntimeVisibleTypeAnnotation",
"lastRuntimeInvisibleTypeAnnotation",
"moduleWriter",
"firstAttribute",
"compute")),
Arrays.stream(ClassWriter.class.getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.map(Field::getName)
.collect(toSet()));
}
@Test
public void testNewConst() {
ClassWriter classWriter = new ClassWriter(0);
......
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