Table of Contents
Fetching ...

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.

iJTyper: An Iterative Type Inference Framework for Java by Integrating Constraint- and Statistically-based Methods

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.
Paper Structure (25 sections, 6 equations, 8 figures, 10 tables)

This paper contains 25 sections, 6 equations, 8 figures, 10 tables.

Figures (8)

  • Figure 1: Example code snippet annotated with four kinds of API elements from the SO post '442496'.
  • Figure 2: Example code snippet with syntax errors from the SO post '1318732'. A syntax error is that the constructor name, CountryFilterView, is not the same as the class name, MyView.
  • Figure 3: Example of the type inference results improved by leveraging the augmented code snippet using a code snippet from the SO post '3954392'. The types with a brown background are inferred by applying SnR to the original code snippet. The types in the yellow background box are predicted by MLMTyper before and after augmenting the original code snippet with the inferred types of SnR.
  • Figure 4: Example code snippet from the SO post '8746084'.
  • Figure 5: The framework of iJTyper.
  • ...and 3 more figures