Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/main/java/org/apache/bcel/classfile/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -702,19 +702,6 @@ public static String encode(byte[] bytes, final boolean compress) throws IOExcep
return caw.toString();
}

static boolean equals(final byte[] a, final byte[] b) {
int size;
if ((size = a.length) != b.length) {
return false;
}
for (int i = 0; i < size; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

/**
* Fillup char with up to length characters with char 'fill' and justify it left or right.
*
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/org/apache/bcel/PLSETestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.stream.Stream;

Expand All @@ -38,6 +39,8 @@
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.Type;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class PLSETestCase extends AbstractTestCase {
/**
Expand Down Expand Up @@ -137,19 +140,22 @@ public void testBCEL79() throws ClassNotFoundException {
/**
* Test to improve BCEL tests code coverage for classfile/Utility.java.
*/
@Test
public void testCoverage() throws ClassNotFoundException, java.io.IOException {
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testCoverage(final boolean compress) throws ClassNotFoundException, java.io.IOException {
// load a class with a wide variety of byte codes - including tableswitch and lookupswitch
final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.ConstantPoolX");
for (final Method m : clazz.getMethods()) {
final String signature = m.getSignature();
Utility.methodTypeToSignature(Utility.methodSignatureReturnType(signature), Utility.methodSignatureArgumentTypes(signature)); // discard result
final Code code = m.getCode();
if (code != null) {
final String encoded = Utility.encode(code.getCode(), true);
// TODO: need for real assertions here
final String encoded = Utility.encode(code.getCode(), compress);
assertNotNull(encoded);
// following statement will throw exeception without classfile/Utility.encode fix
Utility.decode(encoded, true); // discard result
code.toString(); // discard result
assertNotNull(Utility.decode(encoded, compress));
assertNotNull(code.toString());
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/apache/bcel/classfile/UtilityTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.bcel.Const;
import org.apache.bcel.Repository;
import org.junit.jupiter.api.Test;
import java.util.Arrays;

public class UtilityTestCase {

Expand Down Expand Up @@ -128,4 +131,44 @@ public void testSignatureToStringWithGenerics() throws Exception {
assertEquals("<K extends Object, V extends Object> extends Object",
Utility.signatureToString("<K:Ljava/lang/Object;V:Ljava/lang/Object;>Ljava/lang/Object;"), "class signature");
}

@Test
public void testCodeToString() throws Exception {
class CodeToString {
int[][] a = new int[0][0];

CodeToString() {
if (a instanceof int[][]) {
System.out.print(Arrays.asList(a).size());
}
}
}
final JavaClass javaClass = Repository.lookupClass(CodeToString.class);
assertNotNull(javaClass);
for (final Method method : javaClass.getMethods()) {
assertEquals("<init>", method.getName());
final String code = method.getCode().toString(false);
assertTrue(code.contains("0: aload_0"), code);
assertTrue(code.contains("1: aload_1"), code);
assertTrue(code.contains("2: putfield\t\torg.apache.bcel.classfile.UtilityTestCase$1CodeToString.this$0 Lorg/apache/bcel/classfile/UtilityTestCase;"), code);
assertTrue(code.contains("5: aload_0"), code);
assertTrue(code.contains("6: invokespecial\tjava.lang.Object.<init> ()V"), code);
assertTrue(code.contains("9: aload_0"), code);
assertTrue(code.contains("10: iconst_0"), code);
assertTrue(code.contains("11: iconst_0"), code);
assertTrue(code.contains("12: multianewarray\t<[[I>\t2"), code);
assertTrue(code.contains("16: putfield\t\torg.apache.bcel.classfile.UtilityTestCase$1CodeToString.a [[I"), code);
assertTrue(code.contains("19: aload_0"), code);
assertTrue(code.contains("20: getfield\t\torg.apache.bcel.classfile.UtilityTestCase$1CodeToString.a [[I"), code);
assertTrue(code.contains("23: instanceof\t<[[I>"), code);
assertTrue(code.contains("26: ifeq\t\t#47"), code);
assertTrue(code.contains("29: getstatic\t\tjava.lang.System.out Ljava/io/PrintStream;"), code);
assertTrue(code.contains("32: aload_0"), code);
assertTrue(code.contains("33: getfield\t\torg.apache.bcel.classfile.UtilityTestCase$1CodeToString.a [[I"), code);
assertTrue(code.contains("36: invokestatic\tjava.util.Arrays.asList ([Ljava/lang/Object;)Ljava/util/List;"), code);
assertTrue(code.contains("39: invokeinterface\tjava.util.List.size ()I1\t0"), code);
assertTrue(code.contains("44: invokevirtual\tjava.io.PrintStream.print (I)V"), code);
assertTrue(code.contains("47: return"), code);
}
}
}