1 23 package org.hammurapi.inspectors; 24 25 import org.hammurapi.InspectorBase; 26 27 import com.pavelvlasov.config.ConfigurationException; 28 import com.pavelvlasov.config.Parameterizable; 29 import com.pavelvlasov.jsel.Code; 30 31 37 public class CodeTooLongRule extends InspectorBase implements Parameterizable { 38 39 45 public void visit(Code element) { 46 int firstLine = element.getAst().getFirstToken().getLine(); 47 int lastLine = element.getAst().getLastToken().getLine(); 48 49 int length = lastLine-firstLine; 50 context.addMetric(element, "Code length", length); 51 52 if (maxLine!=null && length>maxLine.intValue()) { 53 context.reportViolation(element); 54 } 55 } 56 57 61 private Integer maxLine; 62 63 70 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 71 if ("max-lines".equals(name)) { 72 maxLine = (Integer ) parameter; 73 return true; 74 } else { 75 throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName()); 76 } 77 } 78 79 82 public String getConfigInfo() { 83 if (maxLine==null) { 84 return super.getConfigInfo(); 85 } else { 86 StringBuffer ret=new StringBuffer ("Allowed maximum line length of the methods:\n"); 87 ret.append("max-lines: " + maxLine + "\n"); 88 return ret.toString(); 89 } 90 } 91 } 92 | Popular Tags |