diff options
author | Eric Bruneton <ebruneton@free.fr> | 2022-04-03 09:15:44 +0000 |
---|---|---|
committer | Eric Bruneton <ebruneton@free.fr> | 2022-04-03 09:15:44 +0000 |
commit | d7888a87d751dbd71298d1a9a2ab1f39e27fdacc (patch) | |
tree | 58115584d2745bf81ce56e38572e78f19abf4683 | |
parent | bb8eb9129e07a7dc16f1be6cbd46c8eb6744997f (diff) | |
parent | 897069b696e06031bcafb1ccff277a9cc14dbdb3 (diff) | |
download | ow2-asm-d7888a87d751dbd71298d1a9a2ab1f39e27fdacc.tar.gz |
Merge branch 'code-smells' into 'master'
Make sure only one invocation in the assertThrows lambda can throw the...
See merge request asm/asm!343
5 files changed, 56 insertions, 30 deletions
diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java index affcc682..c5ca9134 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java @@ -557,7 +557,7 @@ class GeneratorAdapterTest { } @Test - void testIfCmp() { + void testIfCmp() throws GeneratorException { assertEquals("IF_ICMPEQ L0", new Generator().ifCmp(Type.INT_TYPE, EQ, new Label())); assertEquals("IF_ICMPNE L0", new Generator().ifCmp(Type.INT_TYPE, NE, new Label())); assertEquals("IF_ICMPGE L0", new Generator().ifCmp(Type.INT_TYPE, GE, new Label())); @@ -578,12 +578,11 @@ class GeneratorAdapterTest { assertEquals("IF_ACMPEQ L0", new Generator().ifCmp(Type.getType("[I"), EQ, new Label())); assertEquals("IF_ACMPNE L0", new Generator().ifCmp(Type.getType("[I"), NE, new Label())); assertThrows( - IllegalArgumentException.class, () -> new Generator().ifCmp(OBJECT_TYPE, GE, new Label())); + GeneratorException.class, () -> new Generator().ifCmp(OBJECT_TYPE, GE, new Label())); assertThrows( - IllegalArgumentException.class, - () -> new Generator().ifCmp(Type.getType("[I"), GE, new Label())); + GeneratorException.class, () -> new Generator().ifCmp(Type.getType("[I"), GE, new Label())); assertThrows( - IllegalArgumentException.class, () -> new Generator().ifCmp(Type.INT_TYPE, 0, new Label())); + GeneratorException.class, () -> new Generator().ifCmp(Type.INT_TYPE, 0, new Label())); } @Test @@ -592,14 +591,14 @@ class GeneratorAdapterTest { } @Test - void testIfICmp() { + void testIfICmp() throws GeneratorException { assertEquals("IF_ICMPEQ L0", new Generator().ifICmp(EQ, new Label())); assertEquals("IF_ICMPNE L0", new Generator().ifICmp(NE, new Label())); assertEquals("IF_ICMPGE L0", new Generator().ifICmp(GE, new Label())); assertEquals("IF_ICMPGT L0", new Generator().ifICmp(GT, new Label())); assertEquals("IF_ICMPLE L0", new Generator().ifICmp(LE, new Label())); assertEquals("IF_ICMPLT L0", new Generator().ifICmp(LT, new Label())); - assertThrows(IllegalArgumentException.class, () -> new Generator().ifICmp(0, new Label())); + assertThrows(GeneratorException.class, () -> new Generator().ifICmp(0, new Label())); } @Test @@ -633,7 +632,7 @@ class GeneratorAdapterTest { } @Test - void testTableSwitch() { + void testTableSwitch() throws GeneratorException { assertEquals("L0 ICONST_M1 L1", new Generator().tableSwitch(new int[0])); assertEquals( "TABLESWITCH\n" @@ -662,8 +661,7 @@ class GeneratorAdapterTest { + " 4: L2\n" + " default: L1 L0 ICONST_0 L2 ICONST_4 L1 ICONST_M1 L3", new Generator().tableSwitch(new int[] {0, 4}, true)); - assertThrows( - IllegalArgumentException.class, () -> new Generator().tableSwitch(new int[] {1, 0})); + assertThrows(GeneratorException.class, () -> new Generator().tableSwitch(new int[] {1, 0})); } @Test @@ -835,6 +833,15 @@ class GeneratorAdapterTest { .catchException(new Label(), new Label(), Type.getObjectType("pkg/Exception"))); } + private static class GeneratorException extends Exception { + + private static final long serialVersionUID = -7167830120642305483L; + + public GeneratorException(final Throwable cause) { + super(cause); + } + } + private static class Generator implements TableSwitchGenerator { private final Textifier textifier; @@ -1057,13 +1064,22 @@ class GeneratorAdapterTest { return toString(); } - public String ifCmp(final Type type, final int mode, final Label label) { - generatorAdapter.ifCmp(type, mode, label); + public String ifCmp(final Type type, final int mode, final Label label) + throws GeneratorException { + try { + generatorAdapter.ifCmp(type, mode, label); + } catch (IllegalArgumentException e) { + throw new GeneratorException(e); + } return toString(); } - public String ifICmp(final int mode, final Label label) { - generatorAdapter.ifICmp(mode, label); + public String ifICmp(final int mode, final Label label) throws GeneratorException { + try { + generatorAdapter.ifICmp(mode, label); + } catch (IllegalArgumentException e) { + throw new GeneratorException(e); + } return toString(); } @@ -1092,13 +1108,21 @@ class GeneratorAdapterTest { return toString(); } - public String tableSwitch(final int[] keys) { - generatorAdapter.tableSwitch(keys, this); + public String tableSwitch(final int[] keys) throws GeneratorException { + try { + generatorAdapter.tableSwitch(keys, this); + } catch (IllegalArgumentException e) { + throw new GeneratorException(e); + } return toString(); } - public String tableSwitch(final int[] keys, final boolean useTable) { - generatorAdapter.tableSwitch(keys, this, useTable); + public String tableSwitch(final int[] keys, final boolean useTable) throws GeneratorException { + try { + generatorAdapter.tableSwitch(keys, this, useTable); + } catch (IllegalArgumentException e) { + throw new GeneratorException(e); + } return toString(); } diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/InstructionAdapterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/InstructionAdapterTest.java index f2ac32e2..65476eef 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/InstructionAdapterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/InstructionAdapterTest.java @@ -62,8 +62,10 @@ class InstructionAdapterTest extends AsmTest { @Test void testConstructor() { - assertDoesNotThrow(() -> new InstructionAdapter(new MethodNode())); - assertThrows(IllegalStateException.class, () -> new InstructionAdapter(new MethodNode()) {}); + MethodNode methodNode = new MethodNode(); + + assertDoesNotThrow(() -> new InstructionAdapter(methodNode)); + assertThrows(IllegalStateException.class, () -> new InstructionAdapter(methodNode) {}); } @Test diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/JsrInlinerAdapterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/JsrInlinerAdapterTest.java index 7821c83f..a96eff5e 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/JsrInlinerAdapterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/JsrInlinerAdapterTest.java @@ -1509,12 +1509,11 @@ class JsrInlinerAdapterTest extends AsmTest { classReader.accept(new JsrInlinerClassAdapter(apiParameter.value(), classWriter), 0); + ClassFile classFile = new ClassFile(classWriter.toByteArray()); if (classParameter.isNotCompatibleWithCurrentJdk()) { - assertThrows( - UnsupportedClassVersionError.class, - () -> new ClassFile(classWriter.toByteArray()).newInstance()); + assertThrows(UnsupportedClassVersionError.class, () -> classFile.newInstance()); } else { - assertDoesNotThrow(() -> new ClassFile(classWriter.toByteArray()).newInstance()); + assertDoesNotThrow(() -> classFile.newInstance()); } } diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/LocalVariablesSorterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/LocalVariablesSorterTest.java index b86ec0df..bafcedb7 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/LocalVariablesSorterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/LocalVariablesSorterTest.java @@ -59,10 +59,12 @@ class LocalVariablesSorterTest extends AsmTest { @Test void testConstructor() { - assertDoesNotThrow(() -> new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", new MethodNode())); + MethodNode methodNode = new MethodNode(); + + assertDoesNotThrow(() -> new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", methodNode)); assertThrows( IllegalStateException.class, - () -> new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", new MethodNode()) {}); + () -> new LocalVariablesSorter(Opcodes.ACC_PUBLIC, "()V", methodNode) {}); } @Test diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/TryCatchBlockSorterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/TryCatchBlockSorterTest.java index 49e75264..e66689d0 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/TryCatchBlockSorterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/TryCatchBlockSorterTest.java @@ -84,12 +84,11 @@ class TryCatchBlockSorterTest extends AsmTest { classReader.accept(classVisitor, 0); + ClassFile classFile = new ClassFile(classWriter.toByteArray()); if (classParameter.isNotCompatibleWithCurrentJdk()) { - assertThrows( - UnsupportedClassVersionError.class, - () -> new ClassFile(classWriter.toByteArray()).newInstance()); + assertThrows(UnsupportedClassVersionError.class, () -> classFile.newInstance()); } else { - assertDoesNotThrow(() -> new ClassFile(classWriter.toByteArray()).newInstance()); + assertDoesNotThrow(() -> classFile.newInstance()); } } } |