From 73d3da8b871f9b1ef27d93e8fe2fa76adeaf14dc Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 6 Jan 2024 17:24:16 -0500 Subject: [PATCH] Remove trailing whitespace - Use final - Use compact array creation syntax - Merge if-else test --- .../apache/bcel/classfile/StackMapEntry.java | 8 +--- .../org/apache/bcel/classfile/Visitor.java | 2 +- .../ConstantPoolModuleToStringTestCase.java | 2 +- .../bcel/classfile/ConstantPoolTestCase.java | 4 +- .../apache/bcel/classfile/TestJira368.java | 4 +- .../org/apache/bcel/generic/JavaHome.java | 2 +- .../org/apache/bcel/generic/TypeTestCase.java | 2 +- .../apache/bcel/util/BCELifierTestCase.java | 2 +- .../bcel/verifier/JiraBcel369TestCase.java | 2 +- .../bcel/verifier/JiraBcel370TestCase.java | 20 ++++----- .../statics/Pass3aVerifierTestCase.java | 44 +++++++++---------- .../InstConstraintVisitorTestCase.java | 18 ++++---- 12 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/apache/bcel/classfile/StackMapEntry.java b/src/main/java/org/apache/bcel/classfile/StackMapEntry.java index 5b8a7f5149..4afe9c0f4f 100644 --- a/src/main/java/org/apache/bcel/classfile/StackMapEntry.java +++ b/src/main/java/org/apache/bcel/classfile/StackMapEntry.java @@ -71,9 +71,7 @@ public final class StackMapEntry implements Node, Cloneable { } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { byteCodeOffset = dataInput.readUnsignedShort(); typesOfStackItems = new StackMapType[] { new StackMapType(dataInput, constantPool) }; - } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) { - byteCodeOffset = dataInput.readUnsignedShort(); - } else if (frameType == Const.SAME_FRAME_EXTENDED) { + } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) { byteCodeOffset = dataInput.readUnsignedShort(); } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { byteCodeOffset = dataInput.readUnsignedShort(); @@ -186,9 +184,7 @@ public void dump(final DataOutputStream file) throws IOException { } else if (frameType == Const.SAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED) { file.writeShort(byteCodeOffset); typesOfStackItems[0].dump(file); - } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX) { - file.writeShort(byteCodeOffset); - } else if (frameType == Const.SAME_FRAME_EXTENDED) { + } else if (frameType >= Const.CHOP_FRAME && frameType <= Const.CHOP_FRAME_MAX || frameType == Const.SAME_FRAME_EXTENDED) { file.writeShort(byteCodeOffset); } else if (frameType >= Const.APPEND_FRAME && frameType <= Const.APPEND_FRAME_MAX) { file.writeShort(byteCodeOffset); diff --git a/src/main/java/org/apache/bcel/classfile/Visitor.java b/src/main/java/org/apache/bcel/classfile/Visitor.java index 9a3388cf0d..4f4b5c2116 100644 --- a/src/main/java/org/apache/bcel/classfile/Visitor.java +++ b/src/main/java/org/apache/bcel/classfile/Visitor.java @@ -230,7 +230,7 @@ default void visitNestMembers(final NestMembers obj) { * @param obj object to visit * @since 6.8.0 */ - default void visitStackMapType(StackMapType obj) { + default void visitStackMapType(final StackMapType obj) { // empty } diff --git a/src/test/java/org/apache/bcel/classfile/ConstantPoolModuleToStringTestCase.java b/src/test/java/org/apache/bcel/classfile/ConstantPoolModuleToStringTestCase.java index 8048b705cb..e7f08c96eb 100644 --- a/src/test/java/org/apache/bcel/classfile/ConstantPoolModuleToStringTestCase.java +++ b/src/test/java/org/apache/bcel/classfile/ConstantPoolModuleToStringTestCase.java @@ -385,7 +385,7 @@ public void visitModuleRequires(final ModuleRequires constantModule) { append(constantModule); append(constantModule.toString(pool)); final String s = constantModule.toString(pool).trim(); - boolean condition = StringUtils.startsWithAny(s, + final boolean condition = StringUtils.startsWithAny(s, "jdk.", "java.", "org.junit", diff --git a/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java index 3c154facb1..3454a1a8ee 100644 --- a/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java +++ b/src/test/java/org/apache/bcel/classfile/ConstantPoolTestCase.java @@ -66,7 +66,7 @@ public void testClassWithDoubleConstantPoolItem() throws ClassNotFoundException, // Next constant pool entry will be invalid so skip it i++; } - } catch (Throwable t) { + } catch (final Throwable t) { t.printStackTrace(); fail(); } @@ -92,7 +92,7 @@ public void testClassWithLongConstantPoolItem() throws ClassNotFoundException, I // Next constant pool entry will be invalid so skip it i++; } - } catch (Throwable t) { + } catch (final Throwable t) { t.printStackTrace(); fail(); } diff --git a/src/test/java/org/apache/bcel/classfile/TestJira368.java b/src/test/java/org/apache/bcel/classfile/TestJira368.java index 83cc510eb0..427276b913 100644 --- a/src/test/java/org/apache/bcel/classfile/TestJira368.java +++ b/src/test/java/org/apache/bcel/classfile/TestJira368.java @@ -35,7 +35,7 @@ private JavaClass parseJavaClass() throws IOException { @Test public void testInstructionListStringBrief() throws Exception { - for (Method method : parseJavaClass().getMethods()) { + for (final Method method : parseJavaClass().getMethods()) { if (!method.isAbstract() && !method.isNative()) { final InstructionList instructionList = new InstructionList(method.getCode().getCode()); final String string = instructionList.toString(false); @@ -46,7 +46,7 @@ public void testInstructionListStringBrief() throws Exception { @Test public void testInstructionListStringVerbose() throws Exception { - for (Method method : parseJavaClass().getMethods()) { + for (final Method method : parseJavaClass().getMethods()) { if (!method.isAbstract() && !method.isNative()) { final InstructionList instructionList = new InstructionList(method.getCode().getCode()); final String string = instructionList.toString(true); diff --git a/src/test/java/org/apache/bcel/generic/JavaHome.java b/src/test/java/org/apache/bcel/generic/JavaHome.java index e4d04d45ba..f23be5537a 100644 --- a/src/test/java/org/apache/bcel/generic/JavaHome.java +++ b/src/test/java/org/apache/bcel/generic/JavaHome.java @@ -68,7 +68,7 @@ public class JavaHome { private static Stream find(final Path start, final int maxDepth, final BiPredicate matcher, final FileVisitOption... options) { - // TODO Apache Commons 2.14.0: Use FilesUncheck + // TODO Apache Commons 2.14.0: Use FilesUncheck return Files.exists(start) ? Uncheck.apply(Files::find, start, maxDepth, matcher, options) : Stream.empty(); } diff --git a/src/test/java/org/apache/bcel/generic/TypeTestCase.java b/src/test/java/org/apache/bcel/generic/TypeTestCase.java index 8378058990..303a75ba4c 100644 --- a/src/test/java/org/apache/bcel/generic/TypeTestCase.java +++ b/src/test/java/org/apache/bcel/generic/TypeTestCase.java @@ -91,7 +91,7 @@ public void testLDC(final String className) throws Exception { for (final InstructionHandle instructionHandle : instructionList) { instructionHandle.accept(new EmptyVisitor() { @Override - public void visitLDC(LDC obj) { + public void visitLDC(final LDC obj) { assertNotNull(obj.getValue(cpg)); } }); diff --git a/src/test/java/org/apache/bcel/util/BCELifierTestCase.java b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java index 1cdeff2d9d..8ab68d6c6d 100644 --- a/src/test/java/org/apache/bcel/util/BCELifierTestCase.java +++ b/src/test/java/org/apache/bcel/util/BCELifierTestCase.java @@ -174,7 +174,7 @@ public void testHelloWorld() throws Exception { /* * Dumps a class using "javap" and compare with the same class recreated using BCELifier, "javac", "java" and dumped with "javap". - * + * * TODO: detect if JDK present and skip test if not */ @ParameterizedTest diff --git a/src/test/java/org/apache/bcel/verifier/JiraBcel369TestCase.java b/src/test/java/org/apache/bcel/verifier/JiraBcel369TestCase.java index bfd84f28ce..5a8502d173 100644 --- a/src/test/java/org/apache/bcel/verifier/JiraBcel369TestCase.java +++ b/src/test/java/org/apache/bcel/verifier/JiraBcel369TestCase.java @@ -109,7 +109,7 @@ public void testCompileAndVerify() throws ClassNotFoundException { at org.apache.bcel.verifier.structurals.Pass3bVerifier.do_verify(Pass3bVerifier.java:386) ... 74 more * } - * + * * @throws ClassNotFoundException */ @Test diff --git a/src/test/java/org/apache/bcel/verifier/JiraBcel370TestCase.java b/src/test/java/org/apache/bcel/verifier/JiraBcel370TestCase.java index 659a9abe25..a92a92240f 100644 --- a/src/test/java/org/apache/bcel/verifier/JiraBcel370TestCase.java +++ b/src/test/java/org/apache/bcel/verifier/JiraBcel370TestCase.java @@ -46,19 +46,19 @@ public class JiraBcel370TestCase extends AbstractTestCase { public void testLdcGetType(final String classFileName) throws Exception { try (FileInputStream file = new FileInputStream(classFileName)) { final ClassParser parser = new ClassParser(file, new File(classFileName).getName()); - JavaClass clazz = parser.parse(); - - Method[] methods = clazz.getMethods(); - - ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool()); - MethodGen methodGen = new MethodGen(methods[0], classFileName, cp); - + final JavaClass clazz = parser.parse(); + + final Method[] methods = clazz.getMethods(); + + final ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool()); + final MethodGen methodGen = new MethodGen(methods[0], classFileName, cp); + // The first instruction is an LDC CONSTANT_Dynamic added by Jacoco - Instruction instruction = methodGen.getInstructionList().getInstructions()[0]; + final Instruction instruction = methodGen.getInstructionList().getInstructions()[0]; instruction.accept(new EmptyVisitor() { @Override - public void visitLDC(LDC ldc) { + public void visitLDC(final LDC ldc) { // Without the change to LDC.getType() this fails because the tag is CONSTANT_Dynamic ldc.getType(cp); } @@ -72,7 +72,7 @@ public void visitLDC(LDC ldc) { "com.foo.Foo" }) // @formatter:on - public void testVerify(String className) throws ClassNotFoundException { + public void testVerify(final String className) throws ClassNotFoundException { // Without the changes to the verifier this fails because it doesn't allow LDC CONSTANT_Dynamic Verifier.verifyType(className); } diff --git a/src/test/java/org/apache/bcel/verifier/statics/Pass3aVerifierTestCase.java b/src/test/java/org/apache/bcel/verifier/statics/Pass3aVerifierTestCase.java index 7cda6a874f..c368c392c4 100644 --- a/src/test/java/org/apache/bcel/verifier/statics/Pass3aVerifierTestCase.java +++ b/src/test/java/org/apache/bcel/verifier/statics/Pass3aVerifierTestCase.java @@ -72,62 +72,62 @@ public static Stream constantsNotSupportedByLdc() { } @AfterAll public static void restoreRepository() { - // We have set our mock repository, revert the change + // We have set our mock repository, revert the change Repository.setRepository(SyntheticRepository.getInstance()); } private Verifier verifier; private org.apache.bcel.util.Repository repository; private ConstantPool cp; - + private JavaClass javaClass; - + @ParameterizedTest @MethodSource("constantsNotSupportedByLdc") - public void rejectLdcConstant(Constant constant) { + public void rejectLdcConstant(final Constant constant) { // LDC the constant 0 and then return - byte[] methodCode = new byte[] { + final byte[] methodCode = { Const.LDC, 0, 0, (byte) Const.RETURN, }; - - Code code = new Code(0, 0, 0, 0, methodCode, new CodeException[0], new Attribute[0], cp); + + final Code code = new Code(0, 0, 0, 0, methodCode, new CodeException[0], new Attribute[0], cp); when(cp.getConstantPool()).thenReturn(new Constant[] {constant}); - - Attribute[] attributes = new Attribute[] {code}; - Method method = new Method(0, 0, 0, attributes, cp); - + + final Attribute[] attributes = {code}; + final Method method = new Method(0, 0, 0, attributes, cp); + when(javaClass.getMethods()).thenReturn(new Method[] {method}); - - Pass3aVerifier pass3aVerifier = new Pass3aVerifier(verifier, 0); - VerificationResult verificationResult = pass3aVerifier.do_verify(); - + + final Pass3aVerifier pass3aVerifier = new Pass3aVerifier(verifier, 0); + final VerificationResult verificationResult = pass3aVerifier.do_verify(); + assertThat(verificationResult.getStatus()).isEqualTo(VerificationResult.VERIFIED_REJECTED); assertThat(verificationResult.getMessage()).startsWith("Instruction ldc[18](2) 0 constraint violated: Operand of LDC"); } - + @BeforeEach void setup() throws ClassNotFoundException { - String className = "org.apache.bcel.verifier.statics.Pass3aVerifierTestCase.foo"; - + final String className = "org.apache.bcel.verifier.statics.Pass3aVerifierTestCase.foo"; + verifier = spy(VerifierFactory.getVerifier(className)); repository = mock(org.apache.bcel.util.Repository.class); cp = mock(ConstantPool.class); javaClass = mock(JavaClass.class); - + // Mock the verifier doReturn(VerificationResult.VR_OK).when(verifier).doPass2(); - + // Mock the repository Repository.setRepository(repository); when(repository.loadClass(className)).thenReturn(javaClass); - + // Mock the constant pool when(cp.getConstantPool()).thenReturn(new Constant[] {new ConstantModule(0)}); - + // Mock the java class when(javaClass.getConstantPool()).thenReturn(cp); } diff --git a/src/test/java/org/apache/bcel/verifier/structurals/InstConstraintVisitorTestCase.java b/src/test/java/org/apache/bcel/verifier/structurals/InstConstraintVisitorTestCase.java index 3ebc5e29a7..72d4c04b6e 100644 --- a/src/test/java/org/apache/bcel/verifier/structurals/InstConstraintVisitorTestCase.java +++ b/src/test/java/org/apache/bcel/verifier/structurals/InstConstraintVisitorTestCase.java @@ -29,22 +29,22 @@ public class InstConstraintVisitorTestCase { private ConstantPoolGen cp; - + @ParameterizedTest @MethodSource("org.apache.bcel.verifier.statics.Pass3aVerifierTestCase#constantsNotSupportedByLdc") - public void rejectLdcConstantModule(Constant constant) { - InstConstraintVisitor visitor = new InstConstraintVisitor(); - + public void rejectLdcConstantModule(final Constant constant) { + final InstConstraintVisitor visitor = new InstConstraintVisitor(); + cp = mock(ConstantPoolGen.class); when(cp.getConstant(0)).thenReturn(constant); - + visitor.setConstantPoolGen(cp); - - LDC ldc = new LDC(0); - + + final LDC ldc = new LDC(0); + assertThatCode(() -> visitor.visitLDC(ldc)).hasMessageStartingWith("Instruction LDC constraint violated: Referenced constant should be a"); } - + @BeforeEach public void setup() { cp = mock(ConstantPoolGen.class);