1 24 package org.hammurapi.inspectors; 25 26 import org.hammurapi.InspectorBase; 27 28 import com.pavelvlasov.config.ConfigurationException; 29 import com.pavelvlasov.config.Parameterizable; 30 import com.pavelvlasov.jsel.Operation; 31 32 37 public class CyclomaticComplexityRule extends InspectorBase implements Parameterizable { 38 42 private Integer operationMaxComplexity; 43 47 private Integer classMaxComplexity; 48 49 55 56 private Integer operationMaxRedComplexity; 57 62 private Integer classMaxRedComplexity; 63 64 70 public void visit(Operation operation) { 71 int complexity=operation.getComplexity(); 72 context.addMetric(operation, "Operation complexity", complexity); 73 if(operationMaxRedComplexity!=null ){ 75 if (operationMaxComplexity!=null && complexity>operationMaxComplexity.intValue() && 76 complexity < operationMaxRedComplexity.intValue() ) { 77 context.reportViolation(operation, new Object [] {operationMaxComplexity, new Integer (complexity)}); 78 } 79 } 80 else { 81 if (operationMaxComplexity!=null && complexity>operationMaxComplexity.intValue()) { 82 context.reportViolation(operation, new Object [] {operationMaxComplexity, new Integer (complexity)}); 83 } 84 } 85 } 86 87 92 public void visit(com.pavelvlasov.jsel.Class clazz) { 93 int complexity=clazz.getComplexity(); 94 context.addMetric(clazz, "Class complexity", complexity); 95 if(classMaxRedComplexity!=null ){ 97 if (classMaxComplexity!=null && complexity>classMaxComplexity.intValue() && 98 complexity < classMaxRedComplexity.intValue() ) { 99 context.reportViolation(clazz, new Object [] {classMaxComplexity, new Integer (complexity)}); 100 } 101 } 102 else { 103 if (classMaxComplexity!=null && complexity>classMaxComplexity.intValue()) { 104 context.reportViolation(clazz, new Object [] {classMaxComplexity, new Integer (complexity)}); 105 } 106 } 107 } 108 109 117 public boolean setParameter(String name, Object value) throws ConfigurationException { 118 if ("operation-max-complexity".equals(name)) { 119 operationMaxComplexity= (Integer ) value; 120 } else if ("class-max-complexity".equals(name)) { 121 classMaxComplexity= (Integer ) value; 122 } else if ("class-max-redcomplexity".equals(name)) { 124 classMaxRedComplexity= (Integer ) value; 125 } else if ("operation-max-redcomplexity".equals(name)) { 126 operationMaxRedComplexity= (Integer ) value; 127 } else { 128 throw new ConfigurationException("Parameter '"+name+"' is not supported"); 129 } 130 if(operationMaxRedComplexity!=null && operationMaxComplexity!=null) 133 { 134 if (operationMaxRedComplexity.intValue()< operationMaxComplexity.intValue() ){ 135 throw new ConfigurationException( 136 "operationMaxRedComplexity should be higher than operationMaxComplexity" ); 137 } 138 } 139 if(classMaxRedComplexity!=null && classMaxComplexity!=null) 140 { 141 if (classMaxRedComplexity.intValue()< classMaxComplexity.intValue() ){ 142 throw new ConfigurationException( 143 "classMaxRedComplexity should be higher than classMaxComplexity" ); 144 } 145 } 146 return true; 147 } 148 149 152 public String getConfigInfo() { 153 StringBuffer ret=new StringBuffer ("Allowed cyclematic complexity:\n"); 154 ret.append("operation: " + operationMaxComplexity + "\t"); 155 ret.append("class: " + classMaxComplexity + "\n"); 156 return ret.toString(); 157 } 158 } 159 | Popular Tags |