Skip to content

Commit b424c33

Browse files
committed
Fix Field Access
1 parent d91f256 commit b424c33

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

liquidjava-example/src/main/java/testSuite/classes/bytebuf_correct/ByteBufTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ public class ByteBufTest {
99

1010
public static final int TEST_BUFFER_SIZE = 128;
1111

12-
// private ByteBuffer mDirectBuffer;
12+
private ByteBuffer mDirectBuffer;
1313

1414
public ByteBufTest() {
1515
// FIX (from accepted answer): mDirectBuffer = ByteBuffer.wrap(new byte[TEST_BUFFER_SIZE]);
1616
// or guard with: if (mDirectBuffer.hasArray()) { ... }
17-
ByteBuffer mDirectBuffer = ByteBuffer.wrap(new byte[TEST_BUFFER_SIZE]);
18-
// VIOLATION: a direct buffer is not array-backed -> array() throws
19-
// UnsupportedOperationException.
17+
mDirectBuffer = ByteBuffer.wrap(new byte[TEST_BUFFER_SIZE]);
18+
// wrap returns an array-backed buffer, so the field retains the state required by array()
2019
byte[] buf = mDirectBuffer.array();
2120
buf[1] = 100;
2221
}

liquidjava-example/src/main/java/testSuite/classes/bytebuf_error/ByteBufTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class ByteBufTest {
99

1010
public static final int TEST_BUFFER_SIZE = 128;
1111

12-
// private ByteBuffer mDirectBuffer;
12+
private ByteBuffer mDirectBuffer;
1313

1414
public ByteBufTest() {
1515
// FIX (from accepted answer): mDirectBuffer = ByteBuffer.wrap(new byte[TEST_BUFFER_SIZE]);
1616
// or guard with: if (mDirectBuffer.hasArray()) { ... }
17-
ByteBuffer mDirectBuffer = ByteBuffer.allocateDirect(TEST_BUFFER_SIZE);
17+
mDirectBuffer = ByteBuffer.allocateDirect(TEST_BUFFER_SIZE);
1818
// VIOLATION: a direct buffer is not array-backed -> array() throws
1919
// UnsupportedOperationException.
2020
byte[] buf = mDirectBuffer.array(); // State Refinement Error

liquidjava-verifier/src/main/java/liquidjava/processor/refinement_checker/object_checkers/AuxStateHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,19 @@ public static String prepareInvocationTarget(TypeChecker tc, CtElement target2,
594594
// v--------- field read
595595
// means invocation is in a form of `t.method(args)`
596596
String name = v.getVariable().getSimpleName();
597+
if (target2 instanceof CtFieldRead<?> fieldRead && fieldRead.getTarget() instanceof CtThisAccess<?>) {
598+
String fieldName = String.format(Formats.THIS, name);
599+
if (tc.getContext().hasVariable(fieldName))
600+
name = fieldName;
601+
}
597602
Optional<VariableInstance> invocationCallee = tc.getContext().getLastVariableInstance(name);
598603
if (invocationCallee.isPresent()) {
599604
invocation.putMetadata(Keys.TARGET, invocationCallee.get());
600605
} else if (target2.getMetadata(Keys.TARGET) == null) {
601606
RefinedVariable var = tc.getContext().getVariableByName(name);
607+
if (var == null)
608+
return name;
609+
602610
String nName = String.format(Formats.INSTANCE, name, tc.getContext().getCounter());
603611
RefinedVariable rv = tc.getContext().addInstanceToContext(nName, var.getType(),
604612
var.getRefinement().substituteVariable(name, nName), target2);

0 commit comments

Comments
 (0)