Tool crashes when processing classes with abstract or native methods
Description
JavaSlicer crashes with IllegalStateException when attempting to slice code that contains abstract or native method declarations, even when these methods are not part of the slice criterion.
Steps to Reproduce
- Create a test file with abstract or native methods:
abstract class AbstractTest {
abstract int getValue();
native void nativeMethod();
}
class ConcreteTest extends AbstractTest {
int getValue() {
return 42;
}
native void nativeMethod();
public static void main(String[] args) {
ConcreteTest obj = new ConcreteTest();
int result = obj.getValue();
System.out.println(result); // Slice criterion
}
}
Expected Behavior
The tool should skip abstract and native methods (which have no body) and successfully create a slice containing only the concrete implementation of getValue().
Actual Behavior
The tool crashes with:
Exception in thread "main" java.lang.IllegalStateException:
Graph creation is not allowed for abstract or native methods!
at es.upv.mist.slicing.utils.ASTUtils.lambda$getCallableBody$0(ASTUtils.java:111)
at es.upv.mist.slicing.graphs.cfg.CFGBuilder.visitCallableDeclarationBody(CFGBuilder.java:383)
Root Cause Hypothesis
The CFGBuilder attempts to visit all method declarations without checking if they have a body first. While ASTUtils.hasBody() exists to perform this check, it is not called before trying to access the method body.
Suggested Fix
Add a guard check in CFGBuilder before processing method declarations:
@Override
public void visit(MethodDeclaration n, Void arg) {
if (!ASTUtils.hasBody(n)) {
return; // Skip abstract/native methods
}
visitCallableDeclaration(n, arg);
}
Currently, body-less methods are not supported. abstract methods could be supported in the future, but they bring with them the possibility that some calls to user-provided code do not have a target, and this affects more parts of the code than just CFGBuilder. native methods have a similar problem.