2
* Copyright 2009 the original author or authors.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.gmetrics.metric.abc
18
import org.gmetrics.util.AstUtil
19
import org.codehaus.groovy.ast.expr.*
20
import org.codehaus.groovy.ast.stmt.*
21
import org.gmetrics.metric.AbstractAstVisitor
22
import org.codehaus.groovy.ast.MethodNode
25
* AST Visitor for calculating the ABC Metric for a class/method.
29
* See http://www.softwarerenovation.com/ABCMetric.pdf
32
* @version $Revision: 107 $ - $Date: 2010-06-05 07:23:27 -0400 (Sat, 05 Jun 2010) $
34
class AbcAstVisitor extends AbstractAstVisitor {
36
private static final ASSIGNMENT_OPERATIONS =
37
['=', '++', '--', '+=', '-=', '/=', '*=', '%=', '<<=', '>>=', '>>>=', '&=', '|=', '^=']
38
private static final COMPARISON_OPERATIONS = ['<', '>', '>=', '<=', '==', '!=', '<=>', '=~', '==~']
39
private static final BOOLEAN_LOGIC_OPERATIONS = ['&&', '||']
41
int numberOfAssignments = 0
42
int numberOfBranches = 0
43
int numberOfConditions = 0
44
final boolean visited = false
46
void visitMethod(MethodNode methodNode) {
47
if (!isSyntheticNonRunMethod(methodNode) && !methodNode.isAbstract()) {
49
super.visitMethod(methodNode)
53
void visitBinaryExpression(BinaryExpression expression) {
54
handleExpressionContainingOperation(expression)
55
super.visitBinaryExpression(expression)
58
void visitPrefixExpression(PrefixExpression expression) {
59
handleExpressionContainingOperation(expression)
60
super.visitPrefixExpression(expression)
63
void visitPostfixExpression(PostfixExpression expression) {
64
handleExpressionContainingOperation(expression)
65
super.visitPostfixExpression(expression)
68
void visitMethodCallExpression(MethodCallExpression call) {
70
super.visitMethodCallExpression(call)
73
void visitPropertyExpression(PropertyExpression expression) {
74
// Treat a property access as a method call
76
super.visitPropertyExpression(expression)
79
void visitConstructorCallExpression(ConstructorCallExpression call) {
81
super.visitConstructorCallExpression(call)
84
void visitIfElse(IfStatement ifElse) {
85
if (isNotEmptyStatement(ifElse.elseBlock)) {
88
super.visitIfElse(ifElse)
91
void visitSwitch(SwitchStatement statement) {
92
numberOfConditions += statement.caseStatements.size()
93
if (isNotEmptyStatement(statement.defaultStatement)) {
96
super.visitSwitch(statement)
99
void visitTryCatchFinally(TryCatchStatement statement) {
100
numberOfConditions ++ // for the 'try'
101
numberOfConditions += statement.catchStatements.size() // for each 'catch'
102
super.visitTryCatchFinally(statement)
105
void visitTernaryExpression(TernaryExpression expression) {
106
numberOfConditions ++
107
super.visitTernaryExpression(expression)
110
void visitBooleanExpression(BooleanExpression booleanExpression) {
111
if (isSingleVariable(booleanExpression.expression)) {
114
super.visitBooleanExpression(booleanExpression)
117
void visitNotExpression(NotExpression notExpression) {
118
if (isSingleVariable(notExpression.expression)) {
121
super.visitNotExpression(notExpression)
124
//--------------------------------------------------------------------------
125
// Internal helper methods
126
//--------------------------------------------------------------------------
128
private void handleExpressionContainingOperation(Expression expression) {
129
def operationName = expression.operation.text
130
if (operationName in ASSIGNMENT_OPERATIONS && !isFinalVariableDeclaration(expression)) {
131
numberOfAssignments ++
133
if (operationName in COMPARISON_OPERATIONS) {
134
numberOfConditions ++
136
if (operationName in BOOLEAN_LOGIC_OPERATIONS) {
137
numberOfConditions += countUnaryConditionals(expression)
141
// Use Groovy dynamic dispatch to achieve pseudo-polymorphism.
142
// Call appropriate countUnaryConditionals() logic based on type of expression
144
private int countUnaryConditionals(BinaryExpression binaryExpression) {
146
def operationName = binaryExpression.operation.text
147
if (operationName in BOOLEAN_LOGIC_OPERATIONS) {
148
if (isSingleVariable(binaryExpression.leftExpression)) {
151
if (isSingleVariable(binaryExpression.rightExpression)) {
158
private int countUnaryConditionals(Expression expression) { // Not necessary?
162
private boolean isSingleVariable(expression) {
163
return expression instanceof VariableExpression
166
private boolean isFinalVariableDeclaration(expression) {
167
return expression instanceof DeclarationExpression &&
168
AstUtil.isFinalVariable(expression, sourceCode)
171
private boolean isNotEmptyStatement(Statement statement) {
172
statement.class != EmptyStatement
b'\\ No newline at end of file'