Design Analyzer 2.0 (DA2) improves Design Analyzer 1.0 by analyzing Java source files (.java) instead of Java class files (.class).
Recall that class A explicitly references class (or interface) B if:
A extends/implements B
A contains a field of type B
A contains a method that explicity references B
A method, m, explicitly references B if m contains a parameter or local variable of type B.
(The actual definition is a bit more complex. We must replace B by a type T that references B. This would include examples such as Collection<B> and B[].)
Without access to the source file, it is impossible to tell if a method references a class.
In addition, cohesion metrics (LCOM1, LCOM2, LCOM3) track the fields referenced by each method. This too requires access to the implementation of the method contained in the source file.
DA2 takes a directory path name as input. For example:
java DesignAnalyzer c:\\packages\\banking
It parses all of the .java files contained in this directory, producing a collection of abstract syntax trees.
(DA2 implicitly assumes that the specified directory corresponds to a package—banking, for example-- and all of the classes declared in this directory belong to this package.)
Using this collection, DA2 computes and displays the following metrics:
LCOM2(A)
deviance(A)
couplingDegree(A, B)
For every class A and B declared in the directory.
For example:
class Transaction
LCOM2 = .3
deviance = .1
couplingDegree to class
Account = .7
Customer = .5
class Account
LCOM2 = .2
deviance = .6
couplingDegree to class
Transation = .5
Customer = .6
class Customer
LCOM2 = .8
deviance = .2
couplingDegree to class
Account = .4
Transaction = .6
To analyze a Java source file you will need a parser. You could use a parser generator such as ANTLR, but this would require a complete grammar for the latest version of Java.
A simpler idea is to use the parser built into Eclipse. This parser is easy to use, but it means that your design analyzer must have access to the core package and sub-packages in Eclipse's Java Development Tools (JDT) library (org.eclipse.jdt.core.*).
Here's an example of a simple Design Analyzer that lists all of the classes declared in the Java source file:
It also counts the number of loop statements (while, for, do) in this file.
JDT makes use of the Visitor Design Pattern to visit the nodes in an abstract syntax tree. (See the Alpha Interpreter for an example of how this pattern works.)
My design analyzer uses two visitors:
Of course DesignAnalyzer and LoopCounter will have to be declared inside of an Eclipse project. The project will have to import the necessary JDT plug-ins. In addition, these plug-ins will have to be placed on the project's build path. Currently, I don't know an easy way to do this. Read Importing Plug-Ins to see how to do this.
The input of a parser is a file containing declarations. The output is an abstract syntax tree (AST). The root node of an AST represents the file. The children of the root represent the declarations contained in the file. In the case of a Java source file, these would be package, class, interface, and import daclarations.
The children of a class declaration would be nodes representing field and method declarations.
The children of a method declaration node would represent the component of a method declaration: parameters, return type, implementing block, etc..
Here's how the DesignAnalyzer parses a Java source file:
public CompilationUnit
parse(String fileName) {
ASTParser
parser = ASTParser.newParser(AST.JLS3);
parser.setSource(getChars(fileName));
return (CompilationUnit)parser.createAST(null);
}
Here's the start of the Java grammar used by JDT:
ASTNode ::= Statement | Expression | CompilationUnit |
BodyDeclaration |
VariableDeclaration| Type | etc.
Each of these non-terminals corresponds to a subclass of ASTNode.
Eclipse can be downloaded at:
The Java Development Tools (JDT) Application Programmer Interfaces (APIs) can be found in the Eclipse documentation that comes with Eclipse (see help) or online (at http://help.eclipse.org/help33/index.jsp).
To get started, look at:
JDT Plug-in Developer Guide
Reference
API Reference
org.eclipse.jdt.core.dom
Here are a couple of interesting examples of parsing that I found on the web:
http://www-128.ibm.com/developerworks/opensource/library/os-ast/
(See "Parsing Existing Code")