JavaSlicer icon indicating copy to clipboard operation
JavaSlicer copied to clipboard

Tool crashes when processing classes with abstract or native methods

Open bystc opened this issue 1 month ago • 1 comments

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

  1. 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);
}

bystc avatar Oct 23 '25 06:10 bystc