Crash on Array Element Field Assignment
Description
JavaSlicer crashes with IllegalStateException when encountering field assignments on array elements, such as arr[0].field = value or arr[0].field += 5.
Steps to Reproduce
- Create a test file with array element field assignment:
public class TestArrayFieldAssignment {
static class Data {
int value;
Data(int v) { this.value = v; }
}
public static void main(String[] args) {
Data[] arr = new Data[3];
arr[0] = new Data(10);
arr[0].value += 5; // This line causes crash
int result = arr[0].value;
System.out.println(result);
}
}
Actual Behavior
Exception in thread "main" java.lang.IllegalStateException:
only valid assignments are this[.<field>]+ =, and <var>[.<field>]+
at es.upv.mist.slicing.nodes.VariableVisitor$1.visit(VariableVisitor.java:370)
at es.upv.mist.slicing.nodes.VariableVisitor$1.visit(VariableVisitor.java:322)
at com.github.javaparser.ast.expr.FieldAccessExpr.accept(FieldAccessExpr.java:96)
Expected Behavior
The tool should successfully analyze the code and include:
- The
Dataclass definition - Array creation and initialization
- The field assignment to
arr[0].value - All dependencies leading to the slicing criterion
Root Cause
In VariableVisitor.java lines 344-395, the code only handles 4 types of field assignment scopes:
-
var.field = value(NameExpr) -
this.field = value(ThisExpr) -
foo().field = value(MethodCallExpr) -
new Foo().field = value(ObjectCreationExpr)
Missing: arr[0].field = value (ArrayAccessExpr)
// VariableVisitor.java:360-370
if (scope.isMethodCallExpr() || scope.isObjectCreationExpr()) {
// Handle method call or object creation scope
} else if (scope.isNameExpr() || scope.isThisExpr()) {
// Handle variable or this scope
} else {
// ArrayAccessExpr falls here and throws exception!
throw new IllegalStateException(
"only valid assignments are this[.<field>]+ =, and <var>[.<field>]+");
}
Suggested Fix
Add support for ArrayAccessExpr as a scope in the field assignment handler:
else if (scope.isArrayAccessExpr()) {
// Handle array element field access: arr[idx].field = value
ArrayAccessExpr arrayAccess = scope.asArrayAccessExpr();
// Process array access and register appropriate dependencies
arrayAccess.accept(VariableVisitor.this, action);
// ... (build realName and root appropriately)
}
@bystc I'm glad you're using the slicer, and although the issue reports are very informative, I'm not going to be able to address them one by one. Each change can be much more complex, and currently this software is not being actively maintained. It was created to implement algorithms shown in various scientific papers, and it will be developed further when new papers and algorithms come out, but I do not have the resources to provide a feature-complete slicer for all of Java.
I would suggest that you fork the repository, if you want to apply further fixes. Otherwise, you might want to try other slicers, such as WALA or Indus-Kaveri. Of course, anything you want to report to this project will be welcomed, but I don't currently have time to implement and properly test the issues you're proposing.
@cargaji Thanks so much for taking the time to reply and clarify the project’s current status—it totally makes sense, and I really appreciate you being transparent about the maintenance resources and the tool’s original focus on scientific algorithm implementation. I want to reassure you that my goal in submitting these issues is purely to document and share the defect findings for research purposes, not to push for immediate fixes. I completely understand that each change would require significant effort, and I have no expectations for them to be addressed one by one. That said, if you ever happen to have a little spare time down the line, I’d be incredibly grateful if you could briefly take a look at the issues I’ve submitted to confirm whether they’re valid bugs. It would mean a lot for grounding my research conclusions, but please don’t feel any pressure—only whenever it’s convenient for you. Again, thanks for building this tool—it’s been really valuable for my research on slicer defects. If I come across any new findings down the line, I’ll still be happy to share them via issues (as you mentioned they’re welcomed), but I’ll make sure to keep things low-pressure. All the best!