001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2006 Nomair A. Naeem
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.toolkits.astmetrics;
021:
022: import polyglot.ast.Block;
023: import polyglot.ast.Do;
024: import polyglot.ast.For;
025: import polyglot.ast.If;
026: import polyglot.ast.Labeled;
027: import polyglot.ast.Node;
028: import polyglot.ast.Stmt;
029: import polyglot.ast.While;
030: import polyglot.visit.NodeVisitor;
031:
032: /*
033: * Calculate the number of different Java Constructs present in the
034: * code
035: */
036: public class ConstructNumbersMetric extends ASTMetric {
037:
038: private int numIf, numIfElse;
039:
040: private int numLabeledBlocks;
041:
042: private int doLoop, forLoop, whileLoop, whileTrue;
043:
044: public ConstructNumbersMetric(Node node) {
045: super (node);
046: }
047:
048: public void reset() {
049: numIf = numIfElse = 0;
050: numLabeledBlocks = 0;
051: doLoop = forLoop = whileLoop = whileTrue = 0;
052: }
053:
054: public void addMetrics(ClassData data) {
055: // TODO Auto-generated method stub
056: //conditionals
057: data.addMetric(new MetricData("If", new Integer(numIf)));
058: data
059: .addMetric(new MetricData("IfElse", new Integer(
060: numIfElse)));
061: data.addMetric(new MetricData("Total-Conditionals",
062: new Integer(numIf + numIfElse)));
063:
064: //labels
065: data.addMetric(new MetricData("LabelBlock", new Integer(
066: numLabeledBlocks)));
067:
068: //loops
069: data.addMetric(new MetricData("Do", new Integer(doLoop)));
070: data.addMetric(new MetricData("For", new Integer(forLoop)));
071: data.addMetric(new MetricData("While", new Integer(whileLoop)));
072: data.addMetric(new MetricData("UnConditional", new Integer(
073: whileTrue)));
074: data.addMetric(new MetricData("Total Loops", new Integer(
075: whileTrue + whileLoop + forLoop + doLoop)));
076: }
077:
078: public NodeVisitor enter(Node parent, Node n) {
079:
080: /*
081: * Num if and ifelse
082: */
083: if (n instanceof If) {
084: //check if there is the "optional" else branch present
085: If ifNode = (If) n;
086: Stmt temp = ifNode.alternative();
087: if (temp == null) {
088: //else branch is empty
089: //System.out.println("This was an if stmt"+n);
090: numIf++;
091: } else {
092: //else branch has something
093: //System.out.println("This was an ifElse stmt"+n);
094: numIfElse++;
095: }
096: }
097:
098: /*
099: * Num Labeled Blocks
100: */
101: if (n instanceof Labeled) {
102: Stmt s = ((Labeled) n).statement();
103: //System.out.println("labeled"+((Labeled)n).label());
104: if (s instanceof Block) {
105: //System.out.println("labeled block with label"+((Labeled)n).label());
106: numLabeledBlocks++;
107: }
108: }
109:
110: /*
111: * Do
112: */
113: if (n instanceof Do) {
114: //System.out.println((Do)n);
115: doLoop++;
116: }
117: /*
118: * For
119: */
120: if (n instanceof For) {
121: //System.out.println((For)n);
122: forLoop++;
123: }
124:
125: /*
126: * While and While True loop
127: */
128: if (n instanceof While) {
129: //System.out.println((While)n);
130: if (((While) n).condIsConstantTrue())
131: whileTrue++;
132: else
133: whileLoop++;
134: }
135:
136: return enter(n);
137: }
138:
139: }
|