Manuscript Offprint
arXiv:2610.24204Pattern-Based Precondition Inference from Method Implementations
Shanyue Zhou and Martin P. Robillard
School of Computer Science
McGill University
Montréal, QC, Canada
ORCID: 0009-0004-3875-3275, 0000-0002-0248-1384
Abstract—Method preconditions define the conditions that must be satisfied before a method is called. Often, such conditions are not documented and must be inferred. We present Prex, a pattern-based static analysis approach that infers preconditions from explicit checks in Java source code. For each precondition, Prex reports the condition required for a valid call and the reported exception in the case of a violation. Prex also attempts code transformations to convert the condition detected within a method's implementation into an expression that can be evaluated by client code. It leverages the concept of a mutation boundary to separate precondition checks from exceptions that may occur after the method changes the program state. We evaluated Prex from 46 open source Java projects and manually reviewed independent samples of 150 reports. Among the 150 Prex reports, 149 were correct and 125 of the correct reports were executable by client code. In a systematic comparison with an alternative approach based on path expansion, Prex provided more precise results. In total, 122 of the 149 correct reports had no relevant method documentation, and 41 required nontrivial source code analysis to recover manually. Overall, explicit source-code checks provide precondition information that can be absent from method documentation, and Prex can help recover it.
Index Terms—Static analysis, preconditions, design by contract
I. INTRODUCTION
Developers routinely define preconditions on the execution of methods. Preconditions are an important enabler of software understandability and quality as they provide a precise definition of the input that is valid to provide a method [1], [2]. In Java, developers can indicate preconditions through documentation, source code and, in a limited sense, annotations [3]. However, in many software projects, there is no formal support for documenting and reasoning about preconditions, and usage is haphazard [4].
In this context, users of software packages are often left uncertain about the preconditions for calling a method. If the documentation is lacking or incorrect, violated preconditions can lead to exceptions and undefined behaviors. Avoiding this eventuality can require defensive programming in user code [5]. The goal of our research is to provide automated, precise, and deterministic support for identifying preconditions in existing source code. This paper contributes a solution for the Java language based on general principles intended to carry over to comparable programming languages.
We present Prex (Precondition Extractor), a tool-supported static analysis approach to extract explicit precondition checks from Java source code and convert them into precondition descriptors. Each descriptor records the guard condition required for a valid call, the violation signal, and a mapping from guard symbols to their method-boundary representations. The analysis recognizes common Java precondition patterns, including assertions and conditional throws. Prex leverages the concept of a mutation boundary to exclude runtime failures that can occur only after the method has started changing program state. Finally, it attempts to express guards using method arguments and public query methods so that client code, which calls the analyzed method, can evaluate them before invocation. We call such guards testable. Some preconditions depend on hidden state for which no public expression exists; Prex identifies such expressions as untestable.
The design of Prex is conservative. If it cannot establish that a candidate is an explicit precondition check before the mutation boundary, it discards the candidate. Every reported descriptor can be traced to the source expressions and control-flow decisions used to construct it. This makes the analysis deterministic and allows its results to be checked and reproduced. The approach does not rely on generated text, training data, or runtime executions.
The closest alternative to the Prex approach is WIT, which uses bounded path analysis to extract conditions that lead Java methods to throw exceptions [6]. WIT targets exception behavior: a reported condition explains when an exception can be reached. Our goal is more specific, as we seek explicit checks that define obligations on the user before method execution. An exception path is therefore excluded if it crosses the mutation boundary, even if its path condition is valid. Other work mines preconditions from repeated guards at API call sites or infers broader program properties from executions [7], [8]. In contrast, our evidence comes from explicit checks in the implementation of the target method and its helpers. This choice limits recovery to explicit preconditions, but it ties every precondition report to a specific check in the source code and does not require a usage corpus or test suite.
We evaluated Prex on methods from 46 open-source Java projects and manually reviewed two independent samples of 150 precondition descriptors: one sampled from Prex and one sampled from WIT. Of the 150 Prex reports, 149 (99.3%) were correct. Among these correct reports, 125 were testable by users. This includes 12 cases in which the source check used hidden state but Prex recovered a public expression for the same state. In contrast, only 57 of the 150 sampled WIT reports were true and correctly-expressed preconditions. Most of the false positives were caused by exception paths that crossed the mutation boundary. Our results also show that precondition extraction is important to support developers, as we found that 122 of the 149 correct Prex reports identified preconditions that were not documented.
This paper makes the following contributions:
-
A novel static analysis algorithm for extracting preconditions from the computation of a mutation boundary and the detection of recognized patterns in source code;
-
A technique to transform guard condition expressions to use method-boundary elements and to determine whether the resulting conditions are testable by client code;
• An empirical evaluation of the precision and testability of the output of the approach on 46 Java projects and a systematic comparison with a path expansion approach;
• Empirical data about the gap between implicitly checked and documented preconditions.
The rest of the paper is organized as follows. In Section II, we review research on exception behavior and precondition mining and explains how our work differs. Section III formulates the problem and describes the design and implementation of Prex. Section IV presents our evaluation dataset, manual review process, and results. Finally, in Section V we place the findings in perspective and discuss future work.
Data availability: We released Prex as a free and open source software project [9] and the research data we collected is available on Zenodo [10].
A. Exception Behavior Extraction
Several prior works extract exception conditions by reconstructing paths that lead to thrown exceptions. Marcilio and Furia presented WIT, a tool that extracts the exception preconditions of Java methods and constructors. WIT statically analyzes the paths in a method's implementation that lead to an exception being thrown [6]. WIT is different in that it seeks to identify exception preconditions, i.e., an input configuration that triggers an exception. To do so, WIT builds paths that end in explicit throws or exceptional calls, inlines callees when their source code is available, and checks whether the resulting path condition is feasible. It uses bounded path exploration to extract the full path of the condition without building it. For this purpose, it relies on an encoding of paths limited to a core subset of Java features and library methods.
WIT reports conditions that can lead a method to throw an exception. In contrast, we report a condition only when it acts as a precondition that users should satisfy before calling the method. For example, an exception may happen only after the method has already changed state, or the extracted formula may refer to internal state that users cannot directly test. We therefore use the mutation boundary and testability analysis to decide what and how to report as a precondition. In contrast to WIT, we aim to report only preconditions that users need to understand and validate before they call the method.
Buse et al. developed a technique to generate exception documentation based on interprocedural exception propagation analysis with symbolic execution-like path reasoning [11]. The main connection to our research is that it statically extracts exception-related paths and turns them into documentation, again without the focus on producing precondition descriptors testable by client code.
Related work on software testing addresses exception conditions from a different perspective: rather than inferring conditions from code, it translates documented @throws conditions or specification templates into executable checks for tests. Even though that work is not a code-based inference technique, it shows that exception conditions and explicit predicates can be interchangeable [12], [13].
Overall, work on exception behavior extraction can be placed in the broader context of exception flow and error-handling behavior analysis [14]–[17]. This work investigates exception-triggering paths, propagation, and error-handling behavior in method implementations.
B. Precondition Mining from Client-Side Patterns
Another line of work extracts preconditions and constraints from recurring guard patterns at API call sites.
Prior work infers API preconditions by mining guard conditions in client code across large Java corpora [18]. The basic idea is that if many call sites use the same guard before a call, that condition may reflect a precondition of the target API [7]. Follow-up work further develops this consensus-based view that recurring client-side guards can reveal likely API preconditions [19], [20].
Other empirical work studies parameter constraints in Java libraries and where they appear, such as in Javadoc or implementation code [21], [22]. The parameter rules are similar to preconditions, but may not have explicit guards in the code. In this case, the evidence comes from recurring client-side guard patterns, and the recovered conditions are inferred from usage rather than from the target method itself. In contrast, we recover explicit checks directly from method implementations, which makes the result deterministic even when usage data is limited.
C. Contract, Annotation, and Invariant Inference
Beckman et al. developed ProtocolFinder, a tool that identifies code locations that may indicate object protocols [23]. ProtocolFinder is related to our work because it also uses source code patterns as evidence of usage constraints in Java programs. ProtocolFinder reports object protocols, which specify the temporal ordering of method calls, while Prex reports preconditions that must hold before each individual method call.
Flanagan et al. introduced Houdini, which infers candidate annotations with the modular static checker ESC/Java [24], [25]. Houdini generates a large set of candidate annotations, then iteratively runs ESC/Java to refute and remove candidates until a fixed point is reached. Even though Houdini is not focused on preconditions, it provides a scalable way to run template-like filters to infer annotations.
It is also possible to infer invariants through dynamic inference systems such as Daikon [8], [26]. These systems infer likely invariants, including procedure preconditions, from observed executions. They show that precondition-like properties can be recovered from program behavior, but they depend on execution traces rather than on explicit checks in code. Numerous other works infer specifications based on code and documentation, but they all are less directly related to our focus on explicit precondition extraction triggered by exceptions [27]–[30]. These works extract evidence from inferred protocols, annotations, or execution traces, and the goal is to recover broader contracts rather than explicit method-entry checks.
III. APPROACH
We developed Prex, a source code analysis tool to detect preconditions from the source code of Java methods and produce guard expressions testable by client code.
A. Scope and Terminology
Generally speaking, a precondition is any predicate that must be true before a method is executed. In principle, executing a method without respecting all of its preconditions leads to undefined behavior. We refer to the execution of a method with one or more precondition violations as an illegal execution. In practice, the result of an illegal execution is often a runtime error indicated through Java's exception-handling mechanism. Other possible results include silently incorrect behavior and system crashes.
In the implementation of a method, a precondition can be either checked or unchecked. This distinction does not relate to the type of exception raised, but rather to whether there is code explicitly checking that the precondition holds. A precondition is checked if the method's implementation includes an explicit validation of input arguments or object state that aborts the method execution upon failure. In Java this check is typically implemented via a throw or assert statement. For example, the first statement of many of the implementations of List#getFirst check whether the list is empty and explicitly throw an exception if it is. We refer to such code fragments as precondition checks. We define a precondition check as a predicate on the input to a method that occurs before the method has any side effect and that results in the execution of the method becoming disabled. Here, a side effect is a change to state outside the method's local variables [31], [32]. This includes updates to object fields. We call the mutation boundary the first reachable control-flow node on each path from the method entry point that is estimated to correspond to code with a side effect. For direct state mutations, this node is the first write to an instance or static field. A precondition check comprises two parts: a guard, which must hold for a valid call, and a violation signal, which is the type of exception raised when the guard does not hold. A precondition can also be unchecked in the sense that, although it is documented and/or its violation results in an exception being thrown, it is not possible to unambiguously identify a dedicated check for it.
The guards in precondition checks can be further categorized as testable guards or untestable guards. A testable guard can be evaluated by client code before calling the method. An untestable guard depends on information that is inaccessible outside the class.
Finally, preconditions can be documented with various degrees of quality. Precise documentation includes both a testable guard and signal. Partial documentation includes only fragments of information or untestable code. In many cases, only a @throws clause is provided in the Javadoc. Finally, preconditions can, of course, remain undocumented.
Given the above terminology, we define the scope of our approach as a tool aimed at converting precondition checks into precise documentation, i.e., Javadoc that include testable guards and the violation signal.
B. Problem Formulation
We define the problem of precondition extraction as a function that takes as input a Java method and produces a set of precondition descriptors. We model a precondition descriptor p as a tuple , where:
-
g is the precondition guard, i.e., the boolean predicate that must be true to avoid invalidating the method's execution. In documentation, the guard is typically negated to describe the condition under which the violation signal occurs.
-
s is the violation signal, expressed as the fully-qualified type of the exception (or error) thrown if the guard evaluates to false in an execution of the method. This element refers to a subtype of Throwable.
-
V records how client code can obtain the values used in g before the call. A parameter is marked DIRECT because its value is directly accessible by the method caller. Other symbols map to equivalent public field accesses or public query expressions when available, and to HIDDEN when no accessible equivalent is found.
Listing 1 shows the precondition descriptor extracted for method ArrayList#add of the Java Development Kit (JDK, version 25). The field input identifies the input method, and the guard, signal, and map correspond to elements g, s, V, respectively. The map shows that the public getter method size() has been identified for symbol size and that symbol index refers to a parameter. The field description combines the elements into a precise piece of documentation.
"input" : "ArrayList#add(index, E)", "guard" : "(index > size || index < 0)", "signal" : "IndexOutOfBoundsException", "map" : { "size" : "size()", "index" : "DIRECT" }, "description" : "@Throws IndexOutOfBoundsException if index > size → () || index < 0", "testability" : "TESTABLE",
Listing 1. Example precondition output produced by Prex. Package names of Java elements are omitted for readability
C. Analysis Algorithm
Prex extracts precondition descriptors from a collection of Java source files. In principle, there is no requirement for the input files to logically correspond to a single project. However, the input collection is considered as a unit for the purpose of resolving dependencies, as described below. Henceforth, we refer to the input collection of files simply as a Java program.
The analysis begins by parsing the program source code into a collection of abstract syntax tree (ASTs) using the JavaParser library. Algorithm 1 presents the overall strategy to extract precondition descriptors from the input program. In addition to a Java program, the analysis takes as input a customizable catalog of common precondition-checking methods that are not part of the input Java program (e.g., Objects#requireNonNull).
Before processing individual methods, the extractTypeHierarchy function performs a preliminary pass over the ASTs of all compilation units to create a data structure that comprises all classes, their fields and visibility, and inheritance relations between classes (Line 1). This global context information is required to accurately resolve whether instance fields are accessible to client code.
The analysis then proceeds in a modular fashion by inspecting each method individually (Line 4). Prex first identifies potential precondition candidates in the method (Line 6). At this stage, a precondition candidate is either a recognized code pattern that results in an exception, or any method call (which could result in a downstream precondition check). The function removeNonPreconditionFailures filters these candidates using the estimated mutation boundary of each method and removes all non-precondition checks that lie beyond the boundary (Line 7).
For each candidate, we then inline any precondition check from downstream methods into the calling method's context, ensuring that the precondition is attributed to the method that exposes it. As part of this propagation, the algorithm performs argument substitution so that the downstream method's local conditions are accurately expressed in terms of the calling method's scope. Once downstream methods are inlined, the
Algorithm 1: Overall Analysis Strategy
Input: J: Java program as ASTs Input: L: Precondition catalog Output: P: Set of all precondition descriptors for J 1 T ← extractTypeHierarchy(J); 2 P ← 0; 3 foreach method do 4 C ← 0; // Potential Preconditions 5 ; 6 C ← preconditionCandidates(m, T, L); 7 C ← removeNonPreconditionFailures(C); 8 end 9 foreach candidate do 10 propagateDownstreamChecks(c, C, L); 11 if isPublic(declaringMethod(c')) then 12 extractSignal(c'); 13 extractGuard(c'); 14 resolveVariableAccess(c', T); 15 ; 16 ; 17 end 18 end 19 ;
candidate is considered a complete precondition check ( , Line 10).
In the final stage of the algorithm, we check if the precondition check's declaring method is public and, in the affirmative, we convert our internal representation of a precondition check into a precondition descriptor (Lines 12-15) and add it to the output (Lines 16 and 19).
D. Implementation Details
Prex relies on several dedicated strategies to transform AST nodes into precondition descriptors and classify their testability.
The precondition catalog (L) has two roles: identifying API methods that perform checks, such as Objects#requireNonNull, and identifying public state-querying methods used in guards, such as List#size. Every method listed in the catalog is assumed to have been manually verified as non-mutating. Prex includes selected methods from java.util and java.lang and allows users to extend the list with any method, including from project-specific code.
Recognized Patterns
In preconditionCandidates, we use three patterns to cover all explicit precondition checks that throw exceptions using throw or assert statements. A precondition is either always violated, or detected through a conditional check or an assertion statement. We convert the precondition checking process into three patterns: (1) unconditional throw, (2) assertion-based check, and (3) conditional throw. Prex is capable of handling variations in expressions of these general patterns, including early returns and preconditions raised in downstream method calls.
https://javaparser.org/. We used version 3.27.1.
Unconditional Throw: This pattern corresponds to a throw statement in the direct path of the method entry that raises an exception whenever the method is executed. This structure is typically used in abstract classes, or for unsupported interface methods. For example, in Listing 2, the method set from AbstractList always throws an UnsupportedOperationException. This pattern corresponds to a precondition that can never be met.
public E set(int index, E element) { throw new UnsupportedOperationException(); }
Listing 2. Unconditional throw in AbstractList
Assert-based Checks: The second recognized pattern is the assertion statement. Each assert statement evaluates a boolean expression and throws an AssertionError if the condition is not satisfied (when assertion checking is enabled).
Conditional Throw: This more general form of precondition check uses an if-throw pattern. Listing 3 shows an abstract representation of this pattern. The preGuardExecution() and post-GuardExecution() are respectively the code that executes before and after the precondition check. The expression in the if statement is the throw condition. Prex negates this condition when needed to produce the guard g required for a valid call. The mutation boundary of preGuardExecution() will be discussed in the next section.
methodName(inputArgs) { preGuardExecution(); if (throwCondition) { throw Exception e; } postGuardExecution(); }
Listing 3. Abstraction of conditional throw pattern
If the pre-guard execution does not cross the mutation boundary, we consider the if-throw block to represent a precondition. Prex then attempts to inline the elements used in the guard. The inlining logic aims to report the most accessible method or field that can be used to test the precondition. The depth of inlining is determined by the accessibility of the elements involved. For example, in Listing 4, len uses the value of size(). Since size() is a public method without state mutation, the tool replaces the access to len with a call to size() and negates the throw condition to recover the testable guard size() != 0. In contrast, in Listing 5, the throw condition contains a private method computeJ that is not accessible, so the tool inlines the helper methods logic to recover a testable guard.
public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); }
Listing 4. Conditional throw pattern precondition from class Stack
public void getResult(int i) { if (computeJ(i) > 10) { throw new IllegalArgumentException(); } }
private int computeJ(int num) { return num + 5; }
Listing 5. Input validation
Prex supports variations in expressions of the conditional. In Listing 6, the exception is placed in the else block. To extract the precondition, preconditionCandidates traverses the if-else branches upward from the throw statement, logically negating conditions to recover the required state. In this example, the extracted throw condition is
!(initialCapacity > 0) && initialCapacity != 0
which the tool simplifies and negates to produce the guard initialCapacity >= 0.
public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); } }
Listing 6. Cases where throw statement in the else block
The same traversal is applied to nested if statements and early returns. Starting from the throw statement, Prex walks through the control flow and collects the conditions that must hold to reach the violation signal. When a condition must be false to reach the signal, Prex negates the corresponding condition before adding it to the recovered guard.
Mutation Boundary
A precondition must be evaluated before a method performs any externally observable state mutation. We define the mutation boundary as the first reachable control-flow node on each path from the method entry point that is estimated to correspond to code with a side effect.
Due to aliasing, abstraction, and polymorphism, it is practically impossible to determine precisely whether a statement has a side-effect or not. To ensure that our approach remains conservative, our algorithm (removeNonPreconditionFailures) assumes that any statement has, by default, a side effect, and reverses this decision only if it can be verified that the statement has no side effect. To verify whether a statement is side-effect free, our analysis separately considers mutating operators and method calls expressions.
If a statement involves one or more mutating operators, such as assignment, increment, or decrement, Prex checks the target of the operation. Mutation operations applied to method-local variables are allowed, but all others are considered to be mutating. For non-primitive local references, the analysis also tracks name aliases introduced before the check, such as a local variable assigned from a field. If the alias is later used to update the referenced object, the update is also considered crossing the mutation boundary.
For statements involving method calls, Prex checks whether each call can be treated as non-mutating before the guard. Calls in the precondition catalog are considered non-mutating by definition, as they must be manually validated as such. Calls to methods defined outside the analyzed program and outside the precondition catalog are conservatively considered to be mutating. This approach also covers unresolved calls that may perform file or network I/O, which the analysis does not separately model those resources. For calls to methods within the analyzed program, we first determine whether they are statically bound or not (i.e., calls to static, final, or private methods). If a method is dynamically dispatched, we conservatively consider it to be mutating. If the method is statically dispatched, we recursively traverse its body to determine whether we can consider it non-mutating using the same procedure as described above. Finally, as a special case, if a call expression is to a method of a non-final class defined in the analyzed program and that has a single implementation, we analyze the single implementation instead of considering the method non-mutating, and issue a warning in the tool's output. During the development of the approach, we determined that excluding all dynamically dispatched calls was too conservative with respect to the mostly theoretical possibility that an external class could be injected by a third party.
Resolving Variable Access
The resolveVariableAccess function evaluates the extracted guard to construct the variable mapping V and determine the precondition's testability. The function iterates through every variable and method in the guard. If a variable corresponds to a method input argument, it is mapped as DIRECT. Fields that are public or exposed via a getter method are mapped to their respective representations, while internal fields lacking public accessibility are flagged as HIDDEN. A method is considered a getter method for a field only if it is public, takes no parameters, does not cross the mutation boundary, and directly returns a field from the same class. For example, the guard for ArrayList#getFirst refers to the private field in size != 0. Mapping size to the getter method size() produces the testable guard size() != 0. The default implementation in List#getFirst already uses the public expression !this.isEmpty(), so its guard is directly testable.
E. Limitations and Exclusions
There are hypothetical cases of precondition checks that Prex intentionally excludes from analysis. Silent failures are excluded, as the method does not throw an exception when a precondition is violated, but instead silently fails or produces undefined behavior. It is difficult to statically detect silent failures as there is no clear indication of failure. We also ignore exceptions handled in catch blocks, since they represent internal error handling rather than checks before execution. We also exclude predicates evaluated in loops because of the difficulty of precisely identifying the exact conditions under which the precondition holds using only static analysis.
Besides these intentional exclusions, the tool has a few known limitations. The extractTypeHierarchy function tracks arrays and collections as a whole, without tracking which element or nested object is mutated. This may make guards that depend on such state harder to recover. The static handling of downstream calls may miss checks that depend on dynamic dispatch or external code. Lastly, our detection of the mutation boundary can be imprecise when the method contains complex loops. To prevent false positives, we treat all loops before the precondition check as potentially mutating. Read-only loops may therefore cause valid precondition checks to be discarded.
IV. EVALUATION
We evaluated Prex through a benchmarking study and systematic comparison with a path-expansion approach. We analyzed precondition descriptors extracted from methods in 46 Java projects from the WIT dataset. The goal of our study was to provide a quantitative assessment of the estimated operational performance of pattern-based precondition extraction. We investigated the following questions:
• RQ1 (Precision): What proportion of preconditions reported by Prex are correct?
• RQ2 (Testability): What proportion of correct preconditions reported by Prex is testable by client code?
• RQ3 (Comparison): How does a pattern-based approach compare to a systematic path expansion approach?
• RQ4 (Value): How does pattern-based precondition extraction complement existing documentation?
A. Sampling Approach and Dataset
We draw conclusions from the application of Prex to a sample of Java methods. We had three main requirements for our sampling approach. First, for the ecological validity of our results, methods in the sample should constitute examples of production code developers could reasonably expect to encounter in the wild. Second, to ensure the internal validity of our results, we aimed to sample to an extent that would enable the detailed manual review of each precondition in its code context. Finally, to ensure that our results were comparable to an alternative approach, we needed to draw from an existing public dataset. Given these requirements, we leveraged the dataset of Marcilio and Furia [6] and subsampled from it to reduce the scale to one that can be manually reviewed. We refer to this dataset as the WIT dataset, from the name of the corresponding tool.
The dataset comprises preconditions inferred from methods sampled from two sources: (1) classes from five modules of the Java Development Kit (JDK) version 11, and (2) classes from 46 open source Java projects (OSS). The paper reports two kinds of extracted exception preconditions: expres, whose feasibility is fully checked, and maybes, which are reported without the same guarantee. We used the 46 OSS project portion of the WIT dataset and excluded the JDK modules because we had used JDK classes as input data to develop
TABLE I EVALUATION DATASET
| Approach | Output | Sample | Projects | Classes |
|---|---|---|---|---|
| Prex | 18 412 | 150 | 31 | 145 |
| WIT | 53 222 | 150 | 31 | 144 |
the approach. The 46 OSS projects of the WIT dataset thus constitute unseen data for the purpose of our evaluation. Henceforth, references to the WIT dataset are to the OSS portion of the dataset. Excluding the JDK classes, we collected 53 222 precondition descriptors from the reports in the WIT replication package.
To produce our sample, we applied Prex to all Java methods in the WIT dataset. This operation produced 18412 precondition descriptors, or only about of the amount yielded by WIT. From this list, we randomly sampled 150 precondition reports. These reports cover 31 projects, 145 classes, and 150 distinct method exception entries. WIT reports a condition for 87 of these entries. We then randomly sampled another 150 precondition descriptors from WIT's 53,222 results. These reports cover 31 projects, 144 classes, and 150 distinct method exception entries. Table I summarizes our evaluation dataset.
The two main parameters in this sampling approach are (1) the decision to sample per precondition descriptor as opposed to per method, and (2) the decision to sample 150 precondition descriptors per approach. The justification for our choice of precondition descriptor as the unit of analysis is simply that in operational conditions these are the data items developers would be inspecting. The justification for our selection of 150 items per approach is that we budgeted 25 hours for the focused manual review of results (300 items an estimated average of five minutes per item).
Our associated dataset [10] contains the raw Prex JSON reports for the 46 projects, the two manually reviewed 150-report samples, and the manual labeling conducted to map the two samples.
B. RQ1: Precision of Prex
We first investigated RQ1: What proportion of preconditions reported by Prex are correct?
Method: We manually inspected each of the 150 preconditions reported by Prex in detail. We checked each reported precondition against the implementation from which it was extracted, tracing through downstream method calls when needed. We identified a precondition report as correct only if it satisfies the following requirements: (1) There is a path through the control flow of a method leading to a throw statement (called an exception path); (2) the exception path does not cross the mutation boundary; (3) the reported guard preserves the condition required by the source check, and (4) the variables and expressions in the reported guard refer to valid program entities: method inputs, instance variables accessible to the method, or expressions over them.
Results: In the Prex sample, 149 of the 150 reported conditions are correct. This observation corresponds to a sampled precision of , with a Wilson binomial confidence interval of at the level. The one incorrect report was due to a missing corner case in our implementation. The precondition is in a getter method, and Prex transforms the access to a private field into a call to the method containing the precondition, which creates a circular dependency.
Answer to RQ1 (Precision): 149 of the 150 preconditions reported by Prex and randomly-sampled for inspection were correct. The single incorrect result was due to an implementation oversight, as opposed to an inherent limitation of the approach.
Discussion: The answer to RQ1 validates our assumption that pattern-based precondition extraction can produce precise output by targeting code patterns that align with developer intent check preconditions in method implementations. However, the inherent trade-off for a high-precision solution is the potential presence of false negative (i.e., missed preconditions). We do not measure recall as part of RQ1. Recall would measure the proportion of all explicit preconditions in the studied methods that Prex reports, which requires a ground truth of all such preconditions. Nevertheless, comparison with a path expansion approach (see Section IV-D) confirmed that Prex did no miss any case identified through this alternative.
C. RQ2: Testability
Our answer to RQ1 validated that pattern-based precondition inference, as implemented through Prex, identifies preconditions with almost no false positives. The question is then, can we use these reports for software development? We thus sought to answer: RQ2: What proportion of correct preconditions reported by Prex is testable by client code?
Method: As part of our systematic review of the results (see Section IV-B), we also manually assessed to what extent the reported precondition was testable. A tool condition is testable if every element (variable or method) needed to evaluate the reported precondition is accessible to client code outside the package. If a precondition check in source code uses hidden state, Prex attempts to recover a public query method for the same state as described in Section III-C. When such a method is found, Prex can report a testable condition. If no expression visible to client code is available, the descriptor remains untestable. For convenience to developers and for the purpose of the research, Prex automatically reports whether the reported precondition descriptor is testable.
Results: Among the 149 correct reports, 125 are testable by client code. In 113 cases, the source condition is already testable and Prex preserves that testability. In 12 cases, the condition relies on hidden state, but Prex recovers a public expression for the same state. The remaining 24 correct reports depend on inaccessible state and remain untestable.
TABLE II PRECISION COMPARISON ON INDEPENDENT SAMPLES FROM EACH TOOL.
| Sample | Correct | Incorrect | Precision | 95% CI |
|---|---|---|---|---|
| Prex | 149 | 1 | 99.3% | [96.3, 99.9]% |
| WIT | 57 | 93 | 38.0% | [30.6, 46.0]% |
Answer to RQ2 (Testability): Among the 149 correct reports, 125 are testable by users. Prex preserves testability in 113 reports whose source condition is already testable and recovers a testable form in 12 reports whose source condition is inaccessible.
Discussion: Correctness and testability describe different properties of a report. A condition can preserve the required condition from the source check but still depend on state that users cannot observe before calling the method. Most correct Prex reports preserve a condition that is already expressed using inputs or public state. The 12 recovered cases show the additional value of variable mapping: the source check uses hidden state, but a public query method provides an equivalent expression that users can test. In the remaining 24 cases, no testable mapping was found, suggesting that these preconditions cannot be easily leveraged for design by contract. This observation motivates improvements in development practices, whereby precondition checks are expressed in terms of code that can be evaluated outside the method. By automatically identifying the testability of precondition checks, Prex supports efforts to improve code to make it more suitable for design by contract practices.
D. RQ3: Comparison
We manually reviewed the 150 reports in the WIT sample and compared the results with those from the Prex sample. The analysis answers RQ3: How does a pattern-based approach compare to a systematic path expansion approach?
Method: The samples were drawn independently from the output of each tool. We used the same systematic review process as described in Sections IV-B and IV-C, including the same definitions of correctness and testability. WIT targets the broader concept of exception behavior, so a report classified as incorrect under our criteria may still correctly describe an exception-triggering condition. The comparison therefore measures the quality of reports that a user may encounter in each output.
Results: In the WIT sample, only 57 of the 150 precondition descriptors are correct according to our precondition definition. Most incorrect descriptors are due to mutation boundary violations: the exception-raising statement can be reached after the method has crossed a conservatively-estimated mutation boundary, so the condition is arguably not a clear precondition. Table II presents the two independent samples side by side.
The comparison with WIT clearly shows the impact of considering the mutation boundary when extracting preconditions. Among the 93 WIT reports that do not satisfy our precondition definition, 69 describe exception paths that cross the mutation boundary. Prex does not report a condition for any of these 69 entries. The mutation boundary therefore removes many exception paths that are valid descriptions of exceptional behavior but are not method-entry preconditions under our definition.
The difference is also visible in the form of the reported guard condition. WIT may inline the code of downstream methods and expose an internal value even when the source condition is already expressed through a public getter. For example, in JavaBeanDescriptor#getPrimitiveProperty(), the source check uses isPrimitiveType(), which is a public method of the same class. Prex reports the condition as !(isPrimitiveType()) . WIT reports type != 6, exposing the internal encoding used by the downstream method instead of the public method accessible to client code. Another case where WIT fails relates to inlining the behavior of lower level library methods. For example, in BitmapCharacterSet#setStyle(int), the source guard checks the size of the receiver's characters collection. WIT reports the condition as size > 1, which drops the receiver object expression and retains an unbound variable size. In contrast, Prex reports characters.size() > 1, preserving the value used by the source.
In terms of overlap, Prex reports a condition for 40 of the 57 correct entries in the WIT sample, and all 40 Prex reports are correct. The 17 correct WIT reports not covered by Prex (i.e., the false negatives) are all explained by our conservative design decisions to (1) only analyze the input program (excluding dependencies), and (2) assume methods have side effect by default. Specifically, some precondition candidates were eliminated because they call methods outside the analyzed program and outside the precondition catalog. In the other cases, dynamically-bound method were conservatively discarded but the manual inspection shows that potential bindings did not result in state mutation. In the Prex sample, WIT reports a condition for 87 entries. All 87 Prex reports are correct, while 82 of the corresponding WIT reports are also correct under our definition. The remaining five WIT reports do not preserve a correct precondition due to variables that are not in scope at the method entry point.
Answer to RQ3 (Comparison): A total of 93 out of 150 (62%) exception paths reported by the WIT path-expansion approach do not describe likely preconditions because they occur after the method may have mutated the state of the running program and/or involve symbols that are not in scope at the method entry point. Comparison with WIT identified 17 false negatives out of precondition reports, all of which due to conservative assumptions in the design of Prex.
Discussion: WIT reports feasible conditions that trigger exceptions, including conditions reached after the method has started executing. Prex reports explicit checks that act as obligations on the user before execution. The difference in output size follows from the different focus of each approaches. First, the mutation boundary removes exception paths that occur after state changes, including the 69 cases identified in the WIT sample. Second, Prex follows checks within the analyzed program and methods listed in its precondition catalog, but does not expand exception behavior from other Java base-library methods. WIT follows such lower-level behavior when constructing exception paths. The smaller output therefore reflects a more targeted analysis scope of Prex, rather than a direct measure of recall. The answer to RQ3 thus illustrates how pattern-based precondition extraction is a solution that offers near-perfect precision at the cost of some false negatives in the presence of precondition checks with complex logic or delegation to dynamically-bound and/or externally-implemented methods.
E. RQ4: Value
Finally, we sought to better understand the value proposition of our proposed approach. We thus investigated RQ4: How does pattern-based precondition extraction complement existing documentation?
Method: To evaluate how Prex complements documentation, we examined whether the preconditions reported by Prex are documented and how much source code analysis is needed to recover them manually. For each method in the Prex sample, the first author reviewed its documentation and implementation. We labeled documentation based on the relevant @throws entry and its description. We separately labeled recovery effort based on the control flow and downstream calls that must be followed to recover the guard from the implementation. Table III defines both dimensions. NONE corresponds to undocumented preconditions, UNEXPLAINED and UNSTRUCTURED to fragmentary or unstructured documentation, and PRECISE to precise documentation. The quantitative results include only the 149 correct Prex reports.
Results: Table IV reports documentation quality and source recovery effort for the 149 correct Prex reports. These labels measure orthogonal dimensions. A precondition can be undocumented but easy to recover if it appears as a direct if statement followed by throw, or a downstream validation method. A documented exception can still be hard to understand if the source condition is spread across downstream method calls or control flow.
The documentation categorization show evidence of poor documentation quality. In 122 of the 149 correct Prex reports, the relevant exception or condition is not even documented. In another 17 cases, there is only partial documentation: six cases are UNEXPLAINED, and 11 cases are UNSTRUCTURED. Only 10 preconditions have corresponding documentation that names the relevant state and the triggering relation. Overall, 139 of the 149 correct reports are missing precise documentation.
The categorization of manual recovery effort shows that many preconditions can be retrieved directly via inspection, but not all of them are easy for users to recover. The 108 DIRECT cases include direct null checks, simple input checks, assertions, and validation helper methods. The 21 INTERMEDIATE cases require simple code inlining, substitution, or local calculations. The 20 COMPLEX cases require significantly more effort to extract. Some guards appear only after reconstructing nested if/else-if/else branches, where the exception-throwing branch is reached only after combining earlier branch conditions. Other guards are hidden inside helper methods that also perform setup, input normalization, or queries of the object state. A method call may look like normal setup code, but the callee may perform an additional precondition check before returning. In such cases, a user has to follow the call, mentally inline the relevant guard, and convert it back into input from the client code.
Answer to RQ4 (Value) Our sample provides evidence of 122 cases of undocumented and 17 cases of partially-documented preconditions out of 150 inspected. Manually recovering the precondition can require challenging code inspection and reasoning in at least 20 cases.
Discussion: The documentation gap is not limited to preconditions that are difficult to express. Many direct checks are also undocumented, even though their conditions are already present in a form close to what could be shown to users. This suggests that precondition checks and method documentation may be maintained as separate concerns. When checks are placed in helper methods or spread across the control flow, keeping the documentation complete becomes challenging. Prex can help connect documentation and code relating to precondition checks. Its descriptors bind the violation signal to the extracted guard and its source check, and express the condition using values available at the method boundary when possible. For direct checks, this information can be collected without requiring users to inspect the implementation. For checks obscured by local calculations, branches, or helper methods, the analysis performs the substitutions needed to recover the condition. These descriptors can therefore provide a basis for completing or checking documentation when the source and its documentation have become inconsistent.
F. Tradeoffs and Threats to Validity
Our experimental design is centered around a detailed expert inspection of a small sample of results as opposed to a quantitative analysis of large amounts of unvalidated data. This trade-off provides us high confidence that the data items we report on are meaningful, but at the cost that we have not verified how the approach operates on all possible input scenarios in our sampling frame of 46 OSS projects.
One potential threat to the internal validity comes from the manual labeling required by various components of the evaluation, including of the correctness testability, documentation, and recovery effort associated with precondition descriptors. We design our classification schemes to minimize subjective judgement, but there remains the possibility of human error in any manual inspection process. However, given that each precondition report in the set of 300 had to be reviewed multiple time under different perspectives as part of the research, we
TABLE III CODING GUIDE FOR PRECONDITION DOCUMENTATION
| Code | Description | Example |
|---|---|---|
| Dimension: Documentation (ordinal scale) | ||
| NONE | The relevant exception or precondition is not documented. | No relevant @throws entry. |
| UNEXPLAINED | The exception is documented, but the condition is not explained. | @throws IllegalArgumentException |
| UNSTRUCTURED | The condition is described in natural language without identifying the exact program values and relations. | @throws IllegalArgumentException if the list is empty. |
| PRECISE | The documentation identifies the relevant program value and the relation that causes the exception. | @throws IllegalArgumentException if index < 0. |
| Dimension: Recovery Effort (ordinal scale) | ||
| DIRECT | The guard appears directly in the method or in a dedicated validation call. | assert input != null; |
| INTERMEDIATE | Recovering the guard requires a simple substitution, calculation, or helper lookup. | int i = input * 2; assert i > 5; |
| COMPLEX | Recovering the guard requires combining branches or following nested helper logic. | if (conditionA)...else if (conditionB)...else {throw Exception} |
TABLE IV DOCUMENTATION QUALITY AND SOURCE RECOVERY EFFORT AMONG CORRECT PREX REPORTS.
| Reports | Documentation | Recovery effort | |||||
|---|---|---|---|---|---|---|---|
| NONE | UNEXPLAINED | UNSTRUCTURED | PRECISE | DIRECT | INTERMEDIATE | COMPLEX | |
| Correct Prex reports | 122 | 6 | 11 | 10 | 108 | 21 | 20 |
estimate the risk of error to be very low and we include our raw data for independent verification. We did not conduct an inter-rater agreement evaluation on the classification because of the high level of effort and expertise involved. In our case, an inter-rater agreement metrics would be more indicative of reviewer motivation and focus during the task than inherent subjectivity in the classification scheme.
V. CONCLUSION
We presented Prex, a static analysis approach for extracting explicit precondition checks from recognized patterns in Java source code. Each precondition descriptor returned by Prex records the guard required for a valid call and the violation signal. To the extent possible, the guard expression is transformed into an expression that can be called by client code to check whether the precondition holds. A key aspect of our approach is the computation of a mutation boundary within methods. With this mutation boundary identified, we limit the analysis to checks that occur before the method changes program state. This important functionality allows us to distinguish likely preconditions from general exception-causing paths through the code, reported by previous approaches.
Our evaluation shows that pattern-based extraction can recover explicit preconditions with high precision. A total of 149 of the 150 sampled reports from our current prototype of Prex were correct. For several preconditions originally expressed in terms of hidden state, our approach was able to recover the equivalent publicly-testable checks. The remaining untestable conditions revealed contract design issues in which client code must satisfy a precondition without access to the relevant state information. The comparison with an existing path expansion approach also confirmed that our novel approach produces higher precision and more useful results, at the cost of some potential false negatives caused by a conservative approach to estimating state mutation. Finally, inspection of the documentation of sampled methods showed that the preconditions we extract were seldomly documented at the same level of detail as the code checks. Manually recovering these preconditions is nontrivial, especially when the recovery requires inspecting nested method calls.
Prex shows that explicit precondition checks contain contract information that is often missing from documentation or hard to understand. Its output can support documentation and can also identify untestable preconditions that may require changes to the method implementation. Future work can improve the analysis of collections, loops, and dynamic dispatch to improve recall while preserving the precision of the reported conditions. Another direction is to use Prex to migrate precondition checks into assert statements, or to inject checks in client code, thus helping developers validate preconditions before a method call.
ACKNOWLEDGEMENTS
This work is funded by the Natural Sciences and Engineering Research Council of Canada (NSERC). The authors are grateful to Bhagya Chembakottu and Edwin Kim for feedback on the paper.
REFERENCES
[1] B. Meyer, “Applying ‘design by contract’,” Computer, vol. 25, no. 10, pp. 40–51, 1992.
[2] C. Casalnuovo, P. Devanbu, A. Oliveira, V. Filkov, and B. Ray, “Assert use in GitHub projects,” in Proceedings of the 37th IEEE/ACM International Conference on Software Engineering, vol. 1, 2015, pp. 755–766.
[3] M. Monperrus, M. Eichberg, E. Tekes, and M. Mezini, “What should developers be aware of? an empirical study on the directives of API documentation,” Empirical Software Engineering, vol. 17, no. 6, p. 703–737, Dec. 2012.
[4] S. H. Tan, D. Marinov, L. Tan, and G. T. Leavens, “@tcomment: Testing javadoc comments to detect comment-code inconsistencies,” in Proceedings of the 5th IEEE International Conference on Software Testing, Verification and Validation, 2012, pp. 260–269.
[5] N. Polikarpova, I. Ciupa, and B. Meyer, “A comparative study of programmer-written and automatically inferred contracts,” in Proceedings of the 18th ACM SIGSOFT International Symposium on Software Testing and Analysis, 2009, p. 93–104. [Online]. Available: https://doi.org/10.1145/1572272.1572284
[6] D. Marcilio and C. A. Furia, “Lightweight precise automatic extraction of exception preconditions in Java methods,” Empirical Software Engineering, vol. 29, no. 1, p. 30, Dec. 2023.
[7] H. A. Nguyen, R. Dyer, T. N. Nguyen, and H. Rajan, “Mining preconditions of APIs in large-scale code corpus,” in Proceedings of the 22nd ACM SIGSOFT International Symposium on Foundations of Software Engineering, Nov. 2014, p. 166–177. [Online]. Available: https://dl.acm.org/doi/10.1145/2635868.2635924
[8] M. D. Ernst, J. H. Perkins, P. J. Guo, S. McCamant, C. Pacheco, M. S. Tschantz, and C. Xiao, “The Daikon system for dynamic detection of likely invariants,” Science of Computer Programming, vol. 69, no. 1–3, p. 35–45, Dec. 2007.
[9] S. Zhou and M. P. Robillard, “Prex Release 0.1,” 2026. [Online]. Available: https://codeberg.org/mpr/Prex
[10] —, “Dataset for “Pattern-Based Precondition Inference from Method Implementations”, Zenodo, 2026, https://doi.org/10.5281/zenodo.23423787.
[11] R. P. Buse and W. R. Weimer, “Automatic documentation inference for exceptions,” in Proceedings of the 17th ACM SIGSOFT International Symposium on Software Testing and Analysis, 2008, p. 273–282. [Online]. Available: https://doi.org/10.1145/1390630.1390664
[12] A. Goffi, A. Gorla, M. D. Ernst, and M. Pezzè, “Automatic generation of oracles for exceptional behaviors,” in Proceedings of the 25th SIGSOFT International Symposium on Software Testing and Analysis, Jul. 2016, p. 213–224. [Online]. Available: https://dl.acm.org/doi/10.1145/2931037.2931061
[13] M. Nassif, A. Hernandez, A. Sridharan, and M. P. Robillard, “Generating unit tests for documentation,” IEEE Transactions on Software Engineering, vol. 48, no. 9, pp. 3268–3279, 2022.
[14] M. P. Robillard and G. C. Murphy, “Static analysis to support the evolution of exception structure in object-oriented systems,” ACM Transactions on Software Engineering and Methodology, vol. 12, no. 2, p. 191–221, Apr. 2003. [Online]. Available: https://doi.org/10.1145/941566.941569
[15] M. Acharya and T. Xie, “Mining API error-handling specifications from source code,” in Proceedings of the 12th International Conference on Fundamental Approaches to Software Engineering, 2009, p. 370–384.
[16] W. Weimer and G. C. Necula, “Mining temporal specifications for error detection,” in Proceedings of the 11th International Conference on Tools and Algorithms for the Construction and Analysis of Systems, 2005, p. 461–476.
[17] M. P. Robillard and G. C. Murphy, “Analyzing exception flow in Java™ programs,” in Proceedings of the 7th European Software Engineering Conference and 7th ACM SIGSOFT Symposium on the Foundations of Software Engineering, 1999, pp. 322–337.
[18] M. K. Ramanathan, A. Grama, and S. Jagannathan, “Static specification inference using predicate mining,” SIGPLAN Not., vol. 42, no. 6, p. 123–134, Jun. 2007. [Online]. Available: https://doi.org/10.1145/1273442.1250749
[19] H. A. Nguyen, R. Dyer, T. N. Nguyen, and H. Rajan, “Consensus-based mining of API preconditions in big code,” in Companion Proceedings of the 2015 ACM SIGPLAN International Conference on Systems, Programming, Languages and Applications: Software for Humanity, ser. SPLASH Companion 2015, Oct. 2015, p. 5–6. [Online]. Available: https://dl.acm.org/doi/10.1145/2814189.2816271
[20] H. A. Nguyen, T. N. Nguyen, H. Rajan, and R. Dyer, “Towards combining usage mining and implementation analysis to infer API preconditions,” in Proceedings of the 1st ACM SIGSOFT International Workshop on Automated Specification Inference, 2018, p. 15–16. [Online]. Available: https://doi.org/10.1145/3278177.3278182
[21] H. Zhong, N. Meng, Z. Li, and L. Jia, “An empirical study on API parameter rules,” in Proceedings of the 42nd ACM/IEEE International Conference on Software Engineering, ser. ICSE '20, Oct. 2020, p. 899–911. [Online]. Available: https://dl.acm.org/doi/10.1145/3377811.3380922
[22] R. Dyer, H. A. Nguyen, H. Rajan, and T. N. Nguyen, “Boa: Ultra-large-scale software repository and source-code mining,” ACM Transactions on Software Engineering and Methodology, vol. 25, no. 1, Dec. 2015. [Online]. Available: https://doi.org/10.1145/2803171
[23] N. E. Beckman, D. Kim, and J. Aldrich, “An empirical study of object protocols in the wild,” in Proceedings of the 25th SIGPLAN SIGSOFT European Conference on Object-Oriented Programming, 2011, pp. 2–26.
[24] C. Flanagan and K. R. M. Leino, “Houdini, an annotation assistant for ESC/Java,” in Proceedings of the 10th International Symposium of Formal Methods for Increasing Software Productivity, 2001, p. 500–517.
[25] C. Flanagan, K. R. M. Leino, M. Lillibridge, G. Nelson, J. B. Saxe, and R. Stata, “Extended static checking for Java,” ACM SIGPLAN Notices, vol. 37, no. 5, p. 234–245, May 2002. [Online]. Available: https://doi.org/10.1145/543552.512558
[26] M. D. Ernst, J. Cockrell, W. G. Griswold, and D. Notkin, “Dynamically discovering likely program invariants to support program evolution,” in Proceedings of the 21st IEEE/ACM International Conference on Software Engineering, 1999, p. 213–224. [Online]. Available: https://doi.org/10.1145/302405.302467
[27] P. Cousot, R. Cousot, and F. Logozzo, “Precondition inference from intermittent assertions and application to contracts on collections,” in Proceedings of the 12th ACM International Conference on Verification, Model Checking, and Abstract Interpretation, 2011, pp. 150–168.
[28] C. Flanagan, R. Joshi, and K. Leino, “Annotation inference for modular checkers,” Information Processing Letters, vol. 77, no. 2, pp. 97–108, 2001, in honor of Edsger W. Dijkstra. [Online]. Available: https://doi.org/10.1016/S0020-0190(00)00196-4
[29] Y. Zhou, C. Wang, X. Yan, T. Chen, S. Panichella, and H. Gall, "Automatic detection and repair recommendation of directive defects in Java API documentation," IEEE Transactions on Software Engineering, vol. 46, no. 9, pp. 1004–1023, 2020.
[30] P. Cousot, R. Cousot, M. Fähndrich, and F. Logozzo, “Automatic inference of necessary preconditions,” in Proceedings of the 14th International Conference on Verification, Model Checking, and Abstract Interpretation, 2013, pp. 128–148.
[31] A. Sălcianu and M. Rinard, “Purity and side effect analysis for java programs,” in Verification, Model Checking, and Abstract Interpretation. Springer Berlin Heidelberg, 2005, pp. 199–215.
[32] D. Helm, F. Kübler, M. Eichberg, M. Reif, and M. Mezini, "A unified lattice model and framework for purity analyses," in Proceedings of the 33rd ACM/IEEE International Conference on Automated Software Engineering, ser. ASE '18. Association for Computing Machinery, 2018, p. 340–350. [Online]. Available: https://doi.org/10.1145/3238147.3238226