Janina's Design Study

From CSSEMediaWiki
Revision as of 08:56, 30 July 2009 by JaninaVoigt (Talk | contribs)
Jump to: navigation, search

As part of my honours project, I am working on a program to analyse the encapsulation in software. This program uses Wal's JST (at least will use JST once it works :)) to extract information from Java code.

I originally didn't put much effort into my design so it has only two classes and is very ugly in its current state. It just started growing as I added more an more features. Part of the reason why I decided to use this for my design study is that I know that the code isn't too pretty at the moment and I want to improve it. I also chose it because I think the code could become unmanageable if I worked with it a lot more. At least because the current design is so ugly there should be lots I can improve on.

I will first describe the requirements for my program to get my head around exactly what I need my program to do. I will then give a short introduction to JST for those readers who are not familiar with what it is and how it works. Then, I will present and critique my current design and show all the design heuristics I have broken before making an attempt to improve the design.

Contents

Requirements

The program needs to be able to:

  • Visit the JST model of a Java program to extract information about fields, methods and accesses to fields and methods.
  • Analyse the accesses to methods and fields and present information to the user of the program. Part of this is deciding whether the program uses class or object encapsulation or both.
  • Ideally, I would like to be able to add other features like a visitor that goes through the model of the java program and changes it to use only object or only class encapsulation.

Constraints

  • I cannot modify the code for JST.

JST

Java Symbol Table (JST) is a semantic model for Java. It constructs a model of a Java program in memory, capturing various semantic concepts. This includes concepts such as packages, classes, methods, constructors, parameters, fields and local variables. The relationships between these entities are also represented by the model.

JST is a much richer model than other existing Java semantic models like Javasrc. These models often only include simple relationships between entities such as method invocation and commonly struggle to resolve polymorphic and inherited method calls, leading to an inaccurate model.

JST currently accepts valid source code written in any Java version up to Java 1.6.

Despite the size and complexity of JST, information can be extracted from JST quite easily by ‘walking’ the semantic model. This can be done using a Visitor design pattern.

JST reads Java programs in from XML parsetree files which can be obtained by parsing java files using a Java parser. By walking the parsetrees, JST builds up the model in memory.

Initial Design

JaninasOriginalDesign.png

Classes

  • Main: This is the starting point for the program. The Main class contains the main() method which reads in the XML parsetree files from the location it is given as a parameter. It then creates an EncapsulationAnalysisVisitor which walks the model of the program to report back on the encapsulation used. It then creates an AccessTighteningVisitor which goes through the model and tightens the access of methods and fields as far as possible and generates new java code from the resulting parsetrees.
  • EncapsulationAnalysisVisitor: The EncapsulationAnalysisVisitor visits relevant parts of the Java program's model and records information about the encapsulation used. In particular, it visits classes, operations (methods and constructors), fields And blocks of code. It contains a large number of fields to record various information and various private utility methods.


Collaborations

  • Main creates an instance of EncapsulationAnalysisVisitor and starts the visiting process by passing the visitor to the default package of the java program model. The default package contains all other program entities. When the visiting process has finished, Main asks the EncapsulationAnalysisVisitor to print its results.

UML Sequence Diagram to come soon.

Design Critique

There are a number of problems with the initial design of my program. The code is quite complex, with long methods and large classes which hints at the fact that I should really refactor and break the visitors up into several classes.

Specific design maxims that are violated by the initial design:

  • Avoid downcasting / Beware type switches: In quite a few places in my program, I check if I am looking at a method or a field using the instanceof operator. I then downcast from a Decl to an OperationDecl or FieldDecl before carrying on my analysis. This smells fishy to me. The downcasting suggests that maybe I should use subclassing instead.
  • Large class smell / Split large classes: EncapsulationAnalysisVisitor has a lot of code in it, partly because it tries to record a lot of different data. As such, it has a large number of instance variables and long but simple methods which record data in these instance variables.
  • Long method smell / Reduce the size of methods: Some of the methods in the visitors are very long. In EncapsulationAnalysisVisitor, some methods are long but quite simple, consisting of a number of conditional statements to make decisions about what data should be recorded.
  • Duplicate code smell / Don't repeat yourself / Once and only once: In EncapsulationAnalysisVisitor, there is code to analyse accesses to fields and methods, which is very similar to each other. Rather than having all this duplicated code, I should write code which works for fields and methods at the same time.
  • Single responsibility principle / One responsibility rule / One key abstraction: EncapsulationAnalysisVisitor is so large because it tries to do too much. It not only walks the model of the Java program to extract information about fields but also records data that it has collected and then writes that data out to file at the end. It collects and records a whole lot data, some of which is unrelated to other bits of data collected.
  • Distribute system intelligence: This is closely related to the last point about EncapsulationAnalysisVisitor having too much responsibility. I don't think that I have divided system intelligence properly but instead concentrated it in this one large class.
  • Keep related data and behavior in one place: In my design, the behavior for visiting the model has been separated from the model of the Java program. This violates Riel's heuristic or keeping related data and behavior in one place. However, one of the constraints of the project is that I cannot change the JST class so that I cannot put that behavior with the data it acts on so there is little I can do about this heuristics breach.

Aims for my design

  • Break up the large visitor class and its methods so that each class has one clear responsibility and system intelligence is distributed better.
  • Get rid of all the duplicated code and put it somewhere sensible.
  • Try to use subclassing to avoid downcasts in the code. This means somehow handling fields and methods separately.
  • Try to hide away some of the ugly code for analysing parts of JST that the rest of the system doesn't need to know about.

New Design

JaninasNewDesign.png

Personal tools