Safer typing of complex API usage through Java generics

W. Harrison, David Lievens, Fabio Simeoni
{"title":"Safer typing of complex API usage through Java generics","authors":"W. Harrison, David Lievens, Fabio Simeoni","doi":"10.1145/1596655.1596666","DOIUrl":null,"url":null,"abstract":"When several incompatible implementations of a single API are in use in a Java program, the danger exists that instances from different implementations may inadvertently be mixed, leading to errors. In this paper we show how to use generics to prevent such mixing. The core idea of the approach is to add a type parameter to the interfaces of the API, and tie the classes that make up an implementation to a unique choice of type parameter. In this way methods of the API can only be invoked with arguments that belong to the same implementation. We show that the presence of a type parameter in the interfaces does not violate the principle of interface-based programming: clients can still completely abstract over the choice of implementation. In addition, we demonstrate how code can be reused between different implementations, how implementations can be defined as extensions of other implementations, and how different implementations may be mixed in a controlled and safe manner. To explore the feasibility of the approach, gauge its usability, and identify any issues that may crop up in practical usage, we have refactored a fairly large existing API-based application suite, and we report on the experience gained in the process.","PeriodicalId":169989,"journal":{"name":"Principles and Practice of Programming in Java","volume":"19 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2009-08-27","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"5","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"Principles and Practice of Programming in Java","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/1596655.1596666","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 5

Abstract

When several incompatible implementations of a single API are in use in a Java program, the danger exists that instances from different implementations may inadvertently be mixed, leading to errors. In this paper we show how to use generics to prevent such mixing. The core idea of the approach is to add a type parameter to the interfaces of the API, and tie the classes that make up an implementation to a unique choice of type parameter. In this way methods of the API can only be invoked with arguments that belong to the same implementation. We show that the presence of a type parameter in the interfaces does not violate the principle of interface-based programming: clients can still completely abstract over the choice of implementation. In addition, we demonstrate how code can be reused between different implementations, how implementations can be defined as extensions of other implementations, and how different implementations may be mixed in a controlled and safe manner. To explore the feasibility of the approach, gauge its usability, and identify any issues that may crop up in practical usage, we have refactored a fairly large existing API-based application suite, and we report on the experience gained in the process.
通过Java泛型对复杂API的使用进行更安全的类型化
当在Java程序中使用单个API的几个不兼容的实现时,存在这样的危险,即来自不同实现的实例可能无意中混合在一起,从而导致错误。在本文中,我们展示了如何使用泛型来防止这种混合。该方法的核心思想是向API的接口添加类型参数,并将组成实现的类绑定到唯一的类型参数选择。通过这种方式,API的方法只能用属于同一实现的参数调用。我们表明,在接口中存在类型参数并不违反基于接口的编程原则:客户端仍然可以完全抽象实现的选择。此外,我们还演示了如何在不同的实现之间重用代码,如何将实现定义为其他实现的扩展,以及如何以受控和安全的方式混合不同的实现。为了探索该方法的可行性,评估其可用性,并确定在实际使用中可能出现的任何问题,我们重构了一个相当大的现有基于api的应用程序套件,并报告了在此过程中获得的经验。
本文章由计算机程序翻译,如有差异,请以英文原文为准。
求助全文
约1分钟内获得全文 求助全文
来源期刊
自引率
0.00%
发文量
0
×
引用
GB/T 7714-2015
复制
MLA
复制
APA
复制
导出至
BibTeX EndNote RefMan NoteFirst NoteExpress
×
提示
您的信息不完整,为了账户安全,请先补充。
现在去补充
×
提示
您因"违规操作"
具体请查看互助需知
我知道了
×
提示
确定
请完成安全验证×
copy
已复制链接
快去分享给好友吧!
我知道了
右上角分享
点击右上角分享
0
联系我们:info@booksci.cn Book学术提供免费学术资源搜索服务,方便国内外学者检索中英文文献。致力于提供最便捷和优质的服务体验。 Copyright © 2023 布克学术 All rights reserved.
京ICP备2023020795号-1
ghs 京公网安备 11010802042870号
Book学术文献互助
Book学术文献互助群
群 号:604180095
Book学术官方微信