Skip to content

Commit

Permalink
Remove trailing whitespace
Browse files Browse the repository at this point in the history
- Use final
- Use compact array creation syntax
- Merge if-else test
  • Loading branch information
garydgregory committed Jan 6, 2024
1 parent f3bcd0a commit 73d3da8
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 57 deletions.
8 changes: 2 additions & 6 deletions src/main/java/org/apache/bcel/classfile/StackMapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/bcel/classfile/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/apache/bcel/classfile/TestJira368.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/apache/bcel/generic/JavaHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class JavaHome {

private static Stream<Path> find(final Path start, final int maxDepth, final BiPredicate<Path, BasicFileAttributes> 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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/apache/bcel/generic/TypeTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/apache/bcel/util/BCELifierTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testCompileAndVerify() throws ClassNotFoundException {
at org.apache.bcel.verifier.structurals.Pass3bVerifier.do_verify(Pass3bVerifier.java:386)
... 74 more
* }</pre>
*
*
* @throws ClassNotFoundException
*/
@Test
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/apache/bcel/verifier/JiraBcel370TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,62 +72,62 @@ public static Stream<Constant> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 73d3da8

Please sign in to comment.