Skip to content

Commit

Permalink
fix bytecode cleanup of inner classes - simplified code and updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 29, 2023
1 parent 885e062 commit f23b33d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.bcel.classfile.InnerClass;
import org.apache.bcel.classfile.InnerClasses;
import org.apache.bcel.classfile.Method;
import org.apache.commons.lang3.Validate;
import org.jd.core.v1.api.loader.Loader;
import org.jd.core.v1.model.classfile.ClassFile;
import org.jd.core.v1.util.DefaultList;
Expand All @@ -28,31 +29,11 @@
public final class ClassFileDeserializer {

public ClassFile loadClassFile(Loader loader, String internalTypeName) throws IOException {
ClassFile classFile = innerLoadClassFile(loader, internalTypeName);

if (classFile == null) {
throw new IllegalArgumentException("Class '" + internalTypeName + "' could not be loaded");
}
for (Method method : classFile.getMethods()) {
Code methodCode = method.getCode();
if (methodCode == null) {
continue;
}
ByteCodeUtil.cleanUpByteCode(methodCode.getCode());
}
return classFile;
}

private ClassFile innerLoadClassFile(Loader loader, String internalTypeName) throws IOException {
if (!loader.canLoad(internalTypeName)) {
return null;
}
Validate.isTrue(loader.canLoad(internalTypeName), "Class %s could not be loaded", internalTypeName);

byte[] data = loader.load(internalTypeName);

if (data == null) {
return null;
}
Validate.notNull(data, "Class %s could not be loaded", internalTypeName);

try (DataInputStream reader = new DataInputStream(new ByteArrayInputStream(data))) {

Expand All @@ -72,7 +53,7 @@ private ClassFile innerLoadClassFile(Loader loader, String internalTypeName) thr
String outerTypeName = ic.getOuterClassIndex() == 0 ? null : cp.getConstantString(ic.getOuterClassIndex(), Const.CONSTANT_Class);

if (!internalTypeName.equals(innerTypeName) && (internalTypeName.equals(outerTypeName) || innerTypeName.startsWith(innerTypePrefix))) {
ClassFile innerClassFile = innerLoadClassFile(loader, innerTypeName);
ClassFile innerClassFile = loadClassFile(loader, innerTypeName);
int flags = ic.getInnerAccessFlags();
int length;

Expand Down Expand Up @@ -101,6 +82,13 @@ private ClassFile innerLoadClassFile(Loader loader, String internalTypeName) thr
classFile.setInnerClassFiles(innerClassFiles);
}
}
for (Method method : classFile.getMethods()) {
Code methodCode = method.getCode();
if (methodCode == null) {
continue;
}
ByteCodeUtil.cleanUpByteCode(methodCode.getCode());
}
return classFile;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/jd/core/v1/ClassFileDeserializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public byte[] load(String internalName) throws IOException {
fail("Expected exception");
}
// Expecting exception because class cannot be loaded
catch (IllegalArgumentException expected) { }
catch (IllegalArgumentException expected) {
assertEquals("Class DoesNotExist could not be loaded", expected.getMessage());
}
}

@Test
Expand Down

0 comments on commit f23b33d

Please sign in to comment.