arXiv:2610.24204cs.SEOct 2026

Manuscript Offprint

arXiv:2610.24204

Pattern-Based Precondition Inference from Method Implementations

Shanyue Zhou, Martin P. Robillard

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].

References (32)