Skip to content

Commit

Permalink
Fix regression in 529061b breaking ClassInfo equality checks
Browse files Browse the repository at this point in the history
Because the info-importer is can be configured to skip method metadata (which it will by default) this broke the tests where the other model was using the metadata.

Realistically if two JVM classes have the same byte-array then they are equal, and we dont need to do any model checking. Only if the array is different should we fall back to model checks.
  • Loading branch information
Col-E committed Jan 7, 2025
1 parent f803eaa commit c65774e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public int getVersion() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!super.equals(o)) return false;

if (o instanceof JvmClassInfo other) {
if (version != other.getVersion()) return false;
return Arrays.equals(bytecode, other.getBytecode());
} else if (!super.equals(o)) {
return false;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public JvmClassInfoBuilder(@Nonnull ClassReader reader) {
adaptFrom(reader);
}

/**
* Creates a builder with data pulled from the given bytecode.
*
* @param reader
* ASM class reader to read bytecode from.
* @param readerFlags
* Reader flags to use when populating information via {@link ClassReader#accept(ClassVisitor, int)}.
*/
public JvmClassInfoBuilder(@Nonnull ClassReader reader, int readerFlags) {
adaptFrom(reader, readerFlags);
}

/**
* Creates a builder with the given bytecode.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Opcodes;
import software.coley.cafedude.classfile.VersionConstants;
import software.coley.recaf.test.TestClassUtils;
import software.coley.recaf.test.dummy.AccessibleFields;
import software.coley.recaf.util.ByteHeaderUtil;

import java.io.IOException;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -93,8 +95,10 @@ void toBuilder() {
"Direct copy via builder should have same class equality");

// With modification
byte[] modifiedBytecode = Arrays.copyOf(accessibleFields.getBytecode(), accessibleFields.getBytecode().length);
modifiedBytecode[5] = 1; // Change minor version to any non-zero value
JvmClassInfo builderModifiedCopy = accessibleFields.toJvmClassBuilder()
.withName("Modified")
.withBytecode(modifiedBytecode)
.build();
assertNotEquals(accessibleFields, builderModifiedCopy,
"Direct copy via builder should have same class equality");
Expand Down

0 comments on commit c65774e

Please sign in to comment.