SnR: Constraint-Based Type Inference for Incomplete Java']Java Code Snippets

被引:10
作者
Dong, Yiwen [1 ]
Gu, Tianxiao [2 ]
Tian, Yongqiang [1 ]
Sun, Chengnian [1 ]
机构
[1] Univ Waterloo, David R Cheriton Sch Comp Sci, Waterloo, ON, Canada
[2] Alibaba Grp, Hangzhou, Peoples R China
来源
2022 ACM/IEEE 44TH INTERNATIONAL CONFERENCE ON SOFTWARE ENGINEERING (ICSE 2022) | 2022年
基金
加拿大自然科学与工程研究理事会;
关键词
type inference; constraint satisfaction; automated repair; datalog; STACKOVERFLOW;
D O I
10.1145/3510003.3510061
中图分类号
TP31 [计算机软件];
学科分类号
081202 ; 0835 ;
摘要
Code snippets are prevalent on websites such as Stack Overflow and are effective in demonstrating API usages concisely. However they are usually difficult to be used directly because most code snippets not only are syntactically incomplete but also lack dependency information, and thus do not compile. For example, Java snippets usually do not have import statements or required library names; only 6.88% of Java snippets on Stack Overflow include import statements necessary for compilation. This paper proposes SnR, a precise, efficient, constraint-based technique to automatically infer the exact types used in code snippets and the libraries containing the inferred types, to compile and therefore reuse the code snippets. Initially, SnR builds a knowledge base of APIs, i.e., various facts about the available APIs, from a corpus of Java libraries. Given a code snippet with missing import statements, SnR automatically extracts typing constraints from the snippet, solves the constraints against the knowledge base, and returns a set of APIs that satisfies the constraints to be imported into the snippet. We have evaluated SnR on a benchmark of 267 code snippets from Stack Overflow. SnR significantly outperforms the state-of-the-art tool Coster. SnR correctly infers 91.0% of the import statements, which makes 73.8% of the snippets compile, compared to 36.0% of the import statements and 9.0% of the snippets by Coster.
引用
收藏
页码:1982 / 1993
页数:12
相关论文
共 44 条
[1]   On code reuse from StackOverflow: An exploratory study on Android apps [J].
Abdalkareem, Rabe ;
Shihab, Emad ;
Rilling, Juergen .
INFORMATION AND SOFTWARE TECHNOLOGY, 2017, 88 :148-158
[2]  
Aiken A., 1993, FPCA '93. Conference on Functional Programming Languages and Computer Architecture, P31, DOI 10.1145/165180.165188
[3]  
[Anonymous], 2015, P 4 ACM SIGPLAN INT
[4]  
[Anonymous], 1988, READINGS DATABASE SY
[5]   Usage and attribution of Stack Overflow code snippets in GitHub projects [J].
Baltes, Sebastian ;
Diehl, Stephan .
EMPIRICAL SOFTWARE ENGINEERING, 2019, 24 (03) :1259-1295
[6]  
Bravenboer Martin, 2009, P 24 ACM SIGPLAN C O, P243, DOI [DOI 10.1145/1640089.1640108, 10.1145/1640089.1640108]
[7]  
cianBuckley, 2013, JAV JOD TIM CONV TIM
[8]  
Dagenais B, 2012, PROC INT CONF SOFTW, P47, DOI 10.1109/ICSE.2012.6227207
[9]   Practical program analysis using general purpose logic programming systems - A case study [J].
Dawson, S ;
Ramakrishnan, CR ;
Warren, DS .
ACM SIGPLAN NOTICES, 1996, 31 (05) :117-126
[10]  
De Moor Oege, 2012, DAT REL 1 INT WORKSH, V6702