Object-oriented programs tend to be written using many common coding idioms, such as those captured by design patterns. While design patterns are useful, implementing them is often tedious and repetitive, requiring boilerplate code that distracts the programmer from more essential details. In this paper, we introduce Mason, a tool that synthesizes object-oriented programs from partial program pieces, and we apply it to automatically insert design patterns into programs. At the core of Mason is a novel technique we call type- and name-guided synthesis, in which an enumerative solver traverses a partial program to generate typing constraints; discharges constraints via program transformations guided by the names of constrained types and members; and backtracks when a constraint is violated or a candidate program fails unit tests. We also introduce two extensions to Mason: a non-local backtracking heuristic that uses execution traces, and a language of patterns that impose syntactic restrictions on missing names. We evaluate Mason on a suite of benchmarks to which Mason must add various well-known design patterns implemented as a library of program pieces. We find that Mason performs well when very few candidate programs satisfy its typing constraints and that our extensions can improve Mason's performance significantly when this is not the case. We believe that Mason takes an important step forward in synthesizing multi-class object-oriented programs using design patterns.
翻译:面向对象程序往往采用许多常见的编码惯用法编写,例如设计模式所捕获的那些模式。尽管设计模式很有用,但其实现过程通常繁琐且重复,需要大量样板代码,这使程序员从更本质的细节中分心。本文介绍Mason——一种能够从局部程序片段合成面向对象程序的工具,并将其应用于自动将设计模式插入程序中。Mason的核心是一种我们称为类型与名称引导合成的新技术:枚举求解器通过遍历局部程序生成类型约束;利用受约束类型及成员名称引导的程序变换来消解约束;并在约束被违反或候选程序未通过单元测试时进行回溯。我们还为Mason引入了两项扩展:一种利用执行轨迹的非局部回溯启发式策略,以及一种对缺失名称施加语法限制的模式语言。我们在基准测试集上评估Mason,该测试集要求Mason添加以程序片段库形式实现的各种经典设计模式。实验发现,当极少数候选程序满足其类型约束时,Mason表现良好;而在不满足此条件时,我们的扩展能显著提升Mason的性能。我们相信,Mason在使用设计模式合成多类别面向对象程序方面迈出了重要的一步。