~ubuntu-branches/ubuntu/trusty/gmetrics/trusty

« back to all changes in this revision

Viewing changes to .pc/ftbfs_613266.diff/src/main/groovy/org/gmetrics/metric/abc/AbcAstVisitor.groovy

  • Committer: Package Import Robot
  • Author(s): Miguel Landaeta, Miguel Landaeta, tony mancill
  • Date: 2012-01-18 20:57:50 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120118205750-68fv86p7fs8xz470
Tags: 0.5-1
[Miguel Landaeta]
* New upstream release.
* Remove patch ftbfs_613266.diff since it was merged upstream.
* Update dates in copyright file.

[tony mancill]
* Set DMUA flag.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* Copyright 2009 the original author or authors.
3
 
*
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
7
 
*
8
 
*      http://www.apache.org/licenses/LICENSE-2.0
9
 
*
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.
15
 
*/
16
 
package org.gmetrics.metric.abc
17
 
 
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
23
 
 
24
 
/**
25
 
 * AST Visitor for calculating the ABC Metric for a class/method.
26
 
 *
27
 
 * @see AbcMetric
28
 
 *
29
 
 * See http://www.softwarerenovation.com/ABCMetric.pdf
30
 
 *
31
 
 * @author Chris Mair
32
 
 * @version $Revision: 107 $ - $Date: 2010-06-05 07:23:27 -0400 (Sat, 05 Jun 2010) $
33
 
 */
34
 
class AbcAstVisitor extends AbstractAstVisitor {
35
 
 
36
 
    private static final ASSIGNMENT_OPERATIONS =
37
 
        ['=', '++', '--', '+=', '-=', '/=', '*=', '%=', '<<=', '>>=', '>>>=', '&=', '|=', '^=']
38
 
    private static final COMPARISON_OPERATIONS = ['<', '>', '>=', '<=', '==', '!=', '<=>', '=~', '==~']
39
 
    private static final BOOLEAN_LOGIC_OPERATIONS = ['&&', '||']
40
 
 
41
 
    int numberOfAssignments = 0
42
 
    int numberOfBranches = 0
43
 
    int numberOfConditions = 0
44
 
    final boolean visited = false
45
 
 
46
 
    void visitMethod(MethodNode methodNode) {
47
 
        if (!isSyntheticNonRunMethod(methodNode)  && !methodNode.isAbstract()) {
48
 
            this.visited = true
49
 
            super.visitMethod(methodNode)
50
 
        }
51
 
    }
52
 
 
53
 
    void visitBinaryExpression(BinaryExpression expression) {
54
 
        handleExpressionContainingOperation(expression)
55
 
        super.visitBinaryExpression(expression)
56
 
    }
57
 
 
58
 
    void visitPrefixExpression(PrefixExpression expression) {
59
 
        handleExpressionContainingOperation(expression)
60
 
        super.visitPrefixExpression(expression)
61
 
    }
62
 
 
63
 
    void visitPostfixExpression(PostfixExpression expression) {
64
 
        handleExpressionContainingOperation(expression)
65
 
        super.visitPostfixExpression(expression)
66
 
    }
67
 
 
68
 
    void visitMethodCallExpression(MethodCallExpression call)  {
69
 
        numberOfBranches ++
70
 
        super.visitMethodCallExpression(call)
71
 
    }
72
 
 
73
 
    void visitPropertyExpression(PropertyExpression expression) {
74
 
        // Treat a property access as a method call
75
 
        numberOfBranches ++
76
 
        super.visitPropertyExpression(expression)
77
 
    }
78
 
 
79
 
    void visitConstructorCallExpression(ConstructorCallExpression call) {
80
 
        numberOfBranches ++
81
 
        super.visitConstructorCallExpression(call)
82
 
    }
83
 
 
84
 
    void visitIfElse(IfStatement ifElse) {
85
 
        if (isNotEmptyStatement(ifElse.elseBlock)) {
86
 
            numberOfConditions ++
87
 
        }
88
 
        super.visitIfElse(ifElse)
89
 
    }
90
 
 
91
 
    void visitSwitch(SwitchStatement statement) {
92
 
        numberOfConditions += statement.caseStatements.size()
93
 
        if (isNotEmptyStatement(statement.defaultStatement)) {
94
 
            numberOfConditions ++
95
 
        }
96
 
        super.visitSwitch(statement)
97
 
    }
98
 
 
99
 
    void visitTryCatchFinally(TryCatchStatement statement) {
100
 
        numberOfConditions ++                                   // for the 'try'
101
 
        numberOfConditions += statement.catchStatements.size()  // for each 'catch'
102
 
        super.visitTryCatchFinally(statement)
103
 
    }
104
 
 
105
 
    void visitTernaryExpression(TernaryExpression expression) {
106
 
        numberOfConditions ++
107
 
        super.visitTernaryExpression(expression)
108
 
    }
109
 
 
110
 
    void visitBooleanExpression(BooleanExpression booleanExpression) {
111
 
        if (isSingleVariable(booleanExpression.expression)) {
112
 
            numberOfConditions++
113
 
        }
114
 
        super.visitBooleanExpression(booleanExpression)
115
 
    }
116
 
 
117
 
    void visitNotExpression(NotExpression notExpression) {
118
 
        if (isSingleVariable(notExpression.expression)) {
119
 
            numberOfConditions++
120
 
        }
121
 
        super.visitNotExpression(notExpression)
122
 
    }
123
 
 
124
 
    //--------------------------------------------------------------------------
125
 
    // Internal helper methods
126
 
    //--------------------------------------------------------------------------
127
 
 
128
 
    private void handleExpressionContainingOperation(Expression expression) {
129
 
        def operationName = expression.operation.text
130
 
        if (operationName in ASSIGNMENT_OPERATIONS && !isFinalVariableDeclaration(expression)) {
131
 
            numberOfAssignments ++
132
 
        }
133
 
        if (operationName in COMPARISON_OPERATIONS) {
134
 
            numberOfConditions ++
135
 
        }
136
 
        if (operationName in BOOLEAN_LOGIC_OPERATIONS) {
137
 
            numberOfConditions += countUnaryConditionals(expression)
138
 
        }
139
 
    }
140
 
 
141
 
    // Use Groovy dynamic dispatch to achieve pseudo-polymorphism.
142
 
    // Call appropriate countUnaryConditionals() logic based on type of expression
143
 
 
144
 
    private int countUnaryConditionals(BinaryExpression binaryExpression) {
145
 
        def count = 0
146
 
        def operationName = binaryExpression.operation.text
147
 
        if (operationName in BOOLEAN_LOGIC_OPERATIONS) {
148
 
            if (isSingleVariable(binaryExpression.leftExpression)) {
149
 
                count ++
150
 
            }
151
 
            if (isSingleVariable(binaryExpression.rightExpression)) {
152
 
                count ++
153
 
            }
154
 
        }
155
 
        return count
156
 
    }
157
 
 
158
 
    private int countUnaryConditionals(Expression expression) {     // Not necessary?
159
 
        return 0
160
 
    }
161
 
 
162
 
    private boolean isSingleVariable(expression) {
163
 
        return expression instanceof VariableExpression
164
 
    }
165
 
 
166
 
    private boolean isFinalVariableDeclaration(expression) {
167
 
        return expression instanceof DeclarationExpression &&
168
 
            AstUtil.isFinalVariable(expression, sourceCode)
169
 
    }
170
 
 
171
 
    private boolean isNotEmptyStatement(Statement statement) {
172
 
        statement.class != EmptyStatement
173
 
    }
174
 
 
175
 
}
 
 
b'\\ No newline at end of file'