Context: Algorithms for generating a safe fluent API have been actively studied in recent years. A safe fluent API is a fluent API that reports incorrect chaining of the API methods as a type error to the API users. Although such a safety property improves the productivity of users, the construction of a safe fluent API is excessively complicated for developers. Therefore, generation algorithms are studied to reduce the development costs of a safe fluent API. Such studies may benefit many programmers, as a fluent API is a popular design in the real world. Inquiry: The generation of a generic fluent API remains unaddressed. A generic fluent API refers to a fluent API that provides generic methods (those that contain type parameters in their definitions). The Stream API in Java is an example of a generic fluent API. Previous studies on safe fluent API generation have focused on the grammar classes that the algorithms can deal with for syntax checking. The key concept of such studies is using nested generics to represent a stack structure for the parser built on top of the type system. Within this context, the role of a type parameter has been limited to the internal representation of a stack element of that parser on the type system. Library developers cannot use type parameters to include a generic method in their API so that the semantic constraints for their API will be statically checked; for example, the type constraint on the items passed through a stream. Approach: We propose an algorithm for generating a generic fluent API. Our translation algorithm is modeled as the construction of deterministic finite automaton (DFA) with type parameter information. Each state of the DFA holds information regarding which type parameters are already bound in that state. This information is used to identify whether a method invocation in a chain newly binds a type to a type parameter or refers to a previously bound type. This identification is necessary because a type parameter in a chain is bound at a particular method invocation, and the bound type is referenced in the following method invocations. Our algorithm constructs the DFA by analyzing the binding times of the type parameters and their propagation among the states in a DFA that is naively constructed using the given grammar. Knowledge: With our algorithm, the generation of a safe fluent API can be introduced into practical use. Grounding: We implemented a generator named Protocool to demonstrate our algorithm. Moreover, we generated several libraries using Protocool to demonstrate the ability and limitations of our algorithm. Importance: Our algorithm can aid library developers to develop a generic fluent API, which is essential for bringing safe fluent API generation to the real world, as the use of type parameters is a common technique in library API design.
ACM CCS 2012Software and its engineering → Domain-specific languages; Source code generation;