JDBC remains a key technology for database access in Java applications. Since the database dictionary and the Java type system have distinct scopes, developers inevitably need to deal with bugs in SQL-to-Java type mappings. We propose an extension of the Java compiler, based on the established Checker Framework, which allows us to bridge this gap. Our approach verifies statically that the correct Java types are used when setting prepared statement parameters or when getting values from result sets. This allows us to lift a practically important class of runtime errors to compile time. Our approach is sound and, therefore, is guaranteed not to produce false negatives. Our prototype implementation also offers a degraded mode for type-checking legacy software, if developers are only interested in a subset of errors. Our experiments show that our approach detects a wide range of type mismatches in realworld application code and can indeed prevent errors which might otherwise surface as runtime errors. From the perspective of the developer, our approach is extremely lightweight: it processes the unmodified Java code, yet developers may add their own annotations. This allows us to perform type-checking even across method boundaries, whereas commercial developer tools are restricted to local checks. Finally, we show that we can type-check real-world JDBC software with reasonable overhead during compilation.
翻译:JDBC仍是Java应用中数据库访问的关键技术。由于数据库字典与Java类型系统具有不同的作用域,开发者不可避免地需要处理SQL与Java类型映射中的缺陷。我们提出一种基于成熟的Checker Framework对Java编译器进行扩展的方法,以弥合这一鸿沟。该方法能在编译时静态验证:设置预处理语句参数或从结果集获取值时,使用了正确的Java类型。这使得我们将实践中重要的一类运行时错误提升至编译阶段。我们的方法具有完备性,因此能保证不产生漏报。针对仅需关注部分错误的开发者,我们的原型实现还提供了用于类型检查遗留代码的降级模式。实验表明,该方法能检测出真实应用代码中广泛存在的类型不匹配问题,并有效预防可能以运行时错误形式暴露的缺陷。从开发者角度而言,该方法极为轻量:它可直接处理未经修改的Java代码,同时允许开发者添加自定义注解。这使得我们能够跨方法边界执行类型检查,而商业开发工具仅局限于局部检查。最后,我们证明该方法能在编译阶段以合理开销对真实JDBC软件进行类型检查。