Information Hiding and Visibility in Interface Specifications Gary T
Total Page:16
File Type:pdf, Size:1020Kb
Computer Science Technical Reports Computer Science 9-2006 Information Hiding and Visibility in Interface Specifications Gary T. Leavens Iowa State University Peter Müller ETH Follow this and additional works at: http://lib.dr.iastate.edu/cs_techreports Part of the Software Engineering Commons, and the Theory and Algorithms Commons Recommended Citation Leavens, Gary T. and Müller, Peter, "Information Hiding and Visibility in Interface Specifications" (2006). Computer Science Technical Reports. 339. http://lib.dr.iastate.edu/cs_techreports/339 This Article is brought to you for free and open access by the Computer Science at Iowa State University Digital Repository. It has been accepted for inclusion in Computer Science Technical Reports by an authorized administrator of Iowa State University Digital Repository. For more information, please contact [email protected]. Information Hiding and Visibility in Interface Specifications Abstract Information hiding controls which parts of a class are visible to non-privileged and privileged clients (e.g., subclasses). This affects detailed design specifications in two ways. First, specifications should not expose hidden class members. As noted in previous work, this is important because such hidden members are not meaningful to all clients. But it also allows changes to hidden implementation details without invalidating correctness proofs for client code, which is important for maintaining verified programs. Second, to enable sound modular reasoning, certain specifications must be visible to clients. We present rules for information hiding in specifications for Java-like languages, and demonstrate their application to the specification language JML. These rules restrict proof obligations to only mention visible class members, but retain soundness. This allows maintenance of implementations and their specifications without affecting client reasoning. Keywords Information hiding, visibility, behavioral interface specification language, proof obligation, public, protected, private, client, JML language Disciplines Software Engineering | Theory and Algorithms This article is available at Iowa State University Digital Repository: http://lib.dr.iastate.edu/cs_techreports/339 Information Hiding and Visibility in Interface Specifications Gary T. Leavens and Peter Müller TR #06-28 September 2006 Keywords: Information hiding, visibility, behavioral interface specification language, proof obligation, public, protected, private, client, JML language. 2006 CR Categories: D.2.1 [Software Engineering] Requirements/Specifications — Languages; D.2.4 [Software Engi- neering] Software/Program Verification — Assertion checkers, class invariants, correctness proofs, formal methods, pro- gramming by contract, reliability, tools, Spec#, Eiffel, JML; D.3.3 [Programming Languages] Language Constructs and Features — Modules, Packages; F.3.1 [Logics and Meanings of Programs] Specifying and Verifying and Reasoning about Programs — Assertions, invariants, logics of programs, pre- and post-conditions, specification techniques. Copyright © 2006 by Gary T. Leavens and Peter Müller Submitted for publication Department of Computer Science 226 Atanasoff Hall Iowa State University Ames, Iowa 50011-1041, USA Information Hiding and Visibility in Interface Specifications Gary T. Leavens∗ Peter Müller† Iowa State University ETH Zurich Ames, Iowa, USA Switzerland [email protected] [email protected] Abstract its syntax has visibility modifiers for specification con- structs, such as invariants and method specification cases. Information hiding controls which parts of a class are These modifiers allow one to specify a class’s public (non- visible to non-privileged and privileged clients (e.g., sub- privileged client), protected (subclass), package (friend), classes). This affects detailed design specifications in two and private (implementation) interfaces [10, 13, 29, 30]. ways. First, specifications should not expose hidden class Our contribution is a set of rules for the modular use of members. As noted in previous work, this is important visibility modifiers in specifications. Formalization allows because such hidden members are not meaningful to all us to precisely describe the subtle interactions between pro- clients. But it also allows changes to hidden implemen- grams, specifications, and proofs and to prove soundness. tation details without invalidating correctness proofs for Our rules could also be applied to similar artifacts. For client code, which is important for maintaining verified pro- example, they could be applied to the weak (incomplete) grams. Second, to enable sound modular reasoning, certain specifications embodied in unit test cases and to the infor- specifications must be visible to clients. We present rules mal specifications embodied in documentation. Like for- for information hiding in specifications for Java-like lan- mal specifications they could also be specialized for differ- guages, and demonstrate their application to the specifica- ent visibility levels. For example, a unit test case could be tion language JML. These rules restrict proof obligations to marked as public, which would imply that changes to hid- only mention visible class members, but retain soundness. den implementation details would not affect its type correct- This allows maintenance of implementations and their spec- ness or meaning. Hence, it would not have to be changed ifications without affecting client reasoning. when hidden details change. Similarly, a class could have documentation marked as protected, which describes how its methods affect its protected members. 1 Introduction Information hiding affects specifications in two ways. First, specifications should not expose hidden implementa- When following information hiding, clients (including tion details. Such details cannot be fully understood by all subclasses) of each class are provided with the information clients [23]. Also they should not be used in a client’s cor- they need to use that class, but nothing more [28]. This aids rectness proof, since otherwise the proof would be invali- maintenance because hidden implementation details can be dated when they change. For example, suppose method add changed without affecting clients. However, information of a class BoundedList has a public precondition count < hiding and its benefits apply not only to code but also to capacity, where count and capacity are protected fields. other artifacts, such as documentation and specifications. Then non-privileged clients do not know what this pre- In this paper, we focus on formal interface specifications condition means exactly; for instance, they do not know and correctness proofs. Formal interface specifications in- whether count is the number of elements in the list (count- clude contracts written in Eiffel [22], the Java Modeling ing from one) or an array index (counting from zero). Such Language (JML) [14], and Spec# [3]. We use JML exam- details are hidden from clients to enhance maintainability, ples for concreteness, but the rules we present can also be which includes maintainability of correctness proofs. applied to Eiffel and Spec#. We mainly discuss JML since Second, to enable sound modular reasoning, certain specifications must be visible to clients. For instance, spec- ∗ Supported in part by the US NSF under grant CCF-0429567. ifications of virtual (overrideable) methods must be visible †Funded in part by the Information Society Technologies program of the European Commission, Future and Emerging Technologies under the to overriding subclass methods, otherwise the overriding IST-2005-015905 MOBIUS project. method cannot respect behavioral subtyping [1, 5, 15, 21]. 1 Another example of the need to make some specifica- class T is visible in a type S if and only if T and S are in tions visible is related to object invariants. Suppose a class the same package. Therefore, only types in T ’s package are BoundedList contains a private invariant to express that its potentially affected by changes to f (e.g., renaming it). protected capacity field is non-negative. Suppose further Our definition of visibility is more restrictive than Java’s that an analysis (e.g., code review or verification) has shown [8] since we require an explicit import, whereas Java per- that the program maintains this invariant. Now if the im- mits fully-qualified type names. In particular, a public plementor of BoundedList strengthens the private invari- member of a type T is not visible in all types of the pro- ant, for instance, to require a capacity of at least 1000 ele- gram but only in those that directly or indirectly import T . ments, the analysis would have to be repeated to show that Our more restrictive definition improves maintainability be- the strengthened invariant is still maintained. The analysis cause one does not have to inspect the full text of each type must be repeated because methods in BoundedList’s pack- S to determine whether it uses T . However, the rules we age and in subclasses of BoundedList might break the in- present are also sound for Java’s notion of visibility. variant by assigning to the protected capacity field. Such a repeated analysis illustrates a maintenance (and modular Outline. The rest of this paper is structured as follows. reasoning) problem. Making the invariant protected would Sec. 2 addresses the first information hiding problem de- make it visible to clients that might break it, clarifying what scribed above. The following sections address the second proofs might break when