1 / 31

Nija Shi shini@cs.ucdavis

Reverse Engineering of Design Patterns in Java Source Code. Ron Olsson olsson@cs.ucdavis.edu. Nija Shi shini@cs.ucdavis.edu. Department of Computer Science University of California, Davis. Introduction Current Approaches A Motivating Example Reclassification of the GoF patterns

celestina
Download Presentation

Nija Shi shini@cs.ucdavis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reverse Engineering of Design Patterns in Java Source Code Ron Olsson olsson@cs.ucdavis.edu Nija Shi shini@cs.ucdavis.edu Department of Computer Science University of California, Davis

  2. Introduction Current Approaches A Motivating Example Reclassification of the GoF patterns Our Initial Prototype – PINOT Results Future Work Outline

  3. Design Patterns • What are they? • A design pattern offers guidelines on when, how, and why an implementation can be created to solve a general problem in a particular context. • What are they not? • Reusable libraries, a framework

  4. Intent: Ensure a class has only one instance and provide a global point of access to it. Example: public class Singleton { private static Singleton INSTANCE = null; private Singleton() {} public static Singleton getInstance() { if (INSTANCE == null) INSTANCE = new Singleton(); return INSTANCE; } } The Singleton Pattern

  5. Design Patterns • The Gang of Four (GoF) Patterns • Based on use, design patterns are categorized into • Creational Patterns • Focus on how objects get created • Structural Patterns • Focus on the ways to decouple classes by role • Behavioral Patterns • Focus on separating object responsibilities based on polymorphism and delegation

  6. Design Pattern Examples

  7. Reverse Engineering • Why Reverse Engineering? • Legacy code • Program understanding • Source code upgrade/migration • Insufficient documentation • Current program understanding tools • Debugging tools • Program analysis tools: CSCOPE, SourceNavigator, SourceInsight, etc. • Improve current program understanding tools • Bring program understanding to the design level

  8. Component Toolkit private static Toolkit toolkit; public static synchronized Toolkit getDefaultToolkit() protected abstract ButtonPeer createButton(Button target) protected abstract TextFieldPeer createTextField(TextField target) protected abstract LabelPeer createLabel(Label target) protected abstract ScrollbarPeer createScrollbar(Scrollbar target) Container TextField Label Button ComponentPeer TextFieldPeer LabelPeer ButtonPeer LayoutManager ComponentPeer Reverse Engineering Design Patterns Singleton Composite layoutMgr 1 AbstractFactory 1 Strategy Bridge

  9. RepresentativeCurrent Approaches

  10. Current Approaches • Can be grouped as the following: • Targeting structural aspects • Targeting behavioral aspects

  11. Targeting Structural Aspects • Method • Extract structural relationships (inter-class analysis) • For a pattern, check for certain structural properties • Drawback • Relies only on structural relationships, which are not the only distinction between patterns

  12. Targeting Behavioral Aspects • Method • Narrow down search space • using inter-class relationships • Verify behavior in method bodies • Dynamic analysis • Machine learning • Static program analysis

  13. Targeting Behavioral Aspects • Drawback • Dynamic analysis: • Requires good data coverage • Verifies program behavior but does not verify the intent • Complicates the task for detecting patterns that involve concurrency • Machine learning: • Most patterns have concrete definitions, thus does not solve the fundamental problem.

  14. Detecting the Singleton Pattern: As detected by FUJABA Common search criteria private Singleton() privatestatic Singleton instance public static Singleton getInstance() Problem No behavioral analysis on getInstance() Solution? Inaccurately recognized as Singleton A Motivating Example public classSingleton { private static Singleton instance; private Singleton(){} public static Singleton getInstance() { if (instance == NULL) instance = new Singleton(); return instance; instance = new Singleton(); return instance; returnnew Singleton(); } }

  15. GoF Patterns Reclassified

  16. Language-provided Patterns • Patterns provided in the language or library • The Iterator Pattern • “Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation” • In Java: • Enumeration since Java 1.0 • Iterator since Java.1.2 • The for-each loop since Java 1.5 • The Prototype Pattern • “Specify the kinds of objects to create using a prototypical instance, and create new objects based on this prototype” • In Java: • The clone() method in java.lang.Object • Pattern Detection • Recognizing variants in legacy code

  17. Structure-driven Patterns • Patterns that are driven by software architecture. • Can be identified by inter-class relationships • The Template Method, Composite, Decorator, Bridge, Adapter, Proxy, Facade Patterns • Inter-class Relationships • Accessibility • Declaration • Inheritance • Delegation • Aggregation • Method invocation

  18. Behavior-driven Patterns • Patterns that are driven by system behavior. • Can be detected using inter-class and program analyses. • The Singleton, Abstract Factory, Factory Method, Flyweight, CoR, Visitor, Observer, Mediator, Strategy, and State patterns. • Program analysis techniques: • Program slicing • Data-flow analysis • Call trace analysis

  19. Domain-specific Patterns • Patterns applied in a domain-specific context • The Interpreter Pattern • “Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language” • Commonly based on the Composite and Visitor patterns • The Command Pattern • “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations” • A use of combining the Bridge and Composite patterns to separate user interface and actual command execution. The Memento pattern is also used to store a history of executed commands • Pattern Detection • Requires domain-specific knowledge

  20. Generic Concepts • Patterns that are generic concepts • The Builder Pattern • “Separate the construction of a complex object from its representation so that the same construction can create different representation” • System bootstrapping pattern, object creation is not necessary • The Memento Pattern • “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later” • Implementation of memo pool and representation of states are not specifically defined. • Pattern detection • Lack implementation trace

  21. Recognizing the Singleton Pattern • Structural aspect • private Singleton() • privatestatic Singleton instance • public static SingletongetInstance() • Behavioral aspect • Analyze the behavior in getInstance() • Check if lazy-instantiation is used • Check if instance is returned • Slice the method body for instance and analyze the sliced program recall

  22. Recognizing the Singleton Pattern public class SingleSpoon { private SingleSpoon(); privatestatic SingleSpoon theSpoon; publicstatic SingleSpoon getTheSpoon() { if (theSpoon == null) theSpoon = new SingleSpoon(); return theSpoon; } } control-flow graph

  23. PatternINference andrecOveryTool • PINOT • A fully automated pattern detection tool • Designed to be faster and more accurate • How PINOT works Pattern Instances Source Code Text Pattern Instances view XMI U M L editors

  24. Implementation Alternatives • Program analysis tools • Extract basic information of the source code • Class, method, and variable declarations • Class inheritance • Method invocations, call trace • Variable refers-to and refers-by relationships • Parsers • Extract the abstract syntax tree (AST) • Compilers • Extract the AST and provide related symbol tables and built-in functions operating on the AST

  25. PatternINference andrecOveryTool • Implementation Overview • A modification of Jikes (open source C++ Java compiler) • Analysis using Jikes abstract syntax tree (AST) and symbol tables • Identifying Structure-driven patterns • Considers Java language constructs • Considers commonly used Java utility classes: java.util.Collection and java.util.Iterator • Identifying Behavior-driven patterns • Applies data-flow analysis, inter-procedural analysis, alias analysis • PINOT considers related patterns • Speed up the process of pattern recognition • E.g., Strategy and State Patterns, CoR and Decorator, etc.

  26. Yes. The tool provides recognition for the pattern and correctly identifies it. No.The tool provides recognition for the pattern but fails to identify it. Blank.The tool does not provide recognition for the pattern.

  27. Benchmarks • Java AWT (GUI toolkit) • javac (Sun Java Compiler) • JHotDraw (GUI framework) • Apache Ant (Build tool) • Swing (Java Swing library) • ArgoUML (UML editor tool)

  28. More Results

  29. Future Work • Investigate other domain-specific patterns • High performance computing (HPC) patterns • Real-time patterns • Security patterns • Extend usability of PINOT • Formalize pattern definitions • Visualizing detection results

  30. PINOT +Eclipse

  31. Conclusion • Reverse engineering design patterns • Reclassifying the GoF patterns for reverse-engineering • About PINOT • Future work

More Related