Software developers study and reuse existing source code to understand how to properly use application programming interfaces (APIs). However, manually finding sufficient and adequate code examples for a given API is a difficult and a time-consuming activity. Existing approaches to find or generate examples assume availability of a reasonable set of client code that uses the API. This assumption does not hold for newly released API libraries, non-widely used APIs, nor private ones. In this work we reuse the important information that is naturally present in test code to circumvent the lack of usage examples for an API when other sources of client code are not available. We propose an approach for automatically identifying the most representative API uses within each unit test case. We then develop an approach to synthesize API usage examples by extracting relevant statements representing the usage of such APIs. We compare the output of a prototype implementation of our approach to both human-written examples and to a state-of-the-art approach. The obtained results are encouraging; the examples automatically generated with our approach are superior to the state-of-the-art approach and highly similar to the manually constructed examples.
翻译:软件开发商研究并重新使用现有的源代码,以了解如何正确使用应用程序编程界面。然而,人工为特定 API 找到充足和适当的代码示例是一项困难和耗时的活动。现有的查找或生成示例的方法假定有一套使用API 的合理客户代码。这一假设对新发行的API 图书馆、非普遍使用的 API 图书馆、非普遍使用的 API 图书馆或私人图书馆都无影响。在这项工作中,我们重新使用测试代码中自然存在的重要信息,以避免在客户代码的其他来源不存在时缺乏对 API 使用的示例。我们提出了在每一个单位测试案例中自动确定最具代表性的 API 使用情况的方法。我们随后通过提取代表使用这种API 的相关信息来综合使用示例的方法。我们比较了我们的方法原型执行人文实例和最先进的方法的输出。获得的结果令人鼓舞;我们的方法自动生成的示例优于最先进的方法,与手工制作的实例非常相似。