iJTyper: An Iterative Type Inference Framework for Java by Integrating Constraint- and Statistically-based Methods
Zhixiang Chen, Anji Li, Neng Zhang, Jianguo Chen, Yuan Huang, Zibin Zheng
TL;DR
iJTyper addresses the challenge of inferring API element types in incomplete Java code snippets by uniting constraint-based and statistically-based type inference within an iterative framework. It alternates constraint-based inferences, code-context augmentation with inferred types, and statistically-based predictions, while progressively reducing the constraint KB to refine results; final inferences are produced by combining both methods. Evaluations on StatType-SO and Short-SO show iJTyper achieving high precision and recall and outperforming SnR, MLMTyper, and ChatGPT, demonstrating the practical viability of integrated iterative inference for API usage in real-world snippets. The approach promises significant benefits for API usage mining, code search, and repair tasks in scenarios where code fragments are incomplete or under-specified.
Abstract
Inferring the types of API elements in incomplete code snippets (e.g., those on Q&A forums) is a prepositive step required to work with the code snippets. Existing type inference methods can be mainly categorized as constraint-based or statistically-based. The former imposes higher requirements on code syntax and often suffers from low recall due to the syntactic limitation of code snippets. The latter relies on the statistical regularities learned from a training corpus and does not take full advantage of the type constraints in code snippets, which may lead to low precision. In this paper, we propose an iterative type inference framework for Java, called iJTyper, by integrating the strengths of both constraint- and statistically-based methods. For a code snippet, iJTyper first applies a constraint-based method and augments the code context with the inferred types of API elements. iJTyper then applies a statistically-based method to the augmented code snippet. The predicted candidate types of API elements are further used to improve the constraint-based method by reducing its pre-built knowledge base. iJTyper iteratively executes both methods and performs code context augmentation and knowledge base reduction until a termination condition is satisfied. Finally, the final inference results are obtained by combining the results of both methods. We evaluated iJTyper on two open-source datasets. Results show that 1) iJTyper achieves high average precision/recall of 97.31% and 92.52% on both datasets; 2) iJTyper significantly improves the recall of two state-of-the-art baselines, SnR and MLMTyper, by at least 7.31% and 27.44%, respectively; and 3) iJTyper improves the average precision/recall of the popular language model, ChatGPT, by 3.25% and 0.51% on both datasets.
