1 23 package org.hammurapi.inspectors; 24 25 import org.hammurapi.InspectorBase; 26 27 import com.pavelvlasov.antlr.AST; 28 import com.pavelvlasov.antlr.Token; 29 import com.pavelvlasov.config.ConfigurationException; 30 import com.pavelvlasov.config.Parameterizable; 31 import com.pavelvlasov.jsel.CompilationUnit; 32 33 34 40 public class MaxLinesInFileRule 41 extends InspectorBase implements Parameterizable { 42 43 49 public void visit(CompilationUnit element) { 50 AST ast = element.getAst(); 51 if (ast==null) { 52 context.addMetric(element, "File length", 0); 53 context.reportViolation(element, "Empty file"); 54 } else { 55 Token token = ast.getToken(); 56 while (token!=null && token.getNextToken()!=null) { 57 token = token.getNextToken(); 58 } 59 60 int lastLineNbr = token==null ? 0 : token.getLine()-1; 61 62 context.addMetric(element, "File length", lastLineNbr); 63 64 if (maxLine!=null && lastLineNbr>maxLine.intValue()) { 65 context.reportViolation(element); 66 } 67 } 68 } 69 70 74 private Integer maxLine; 75 76 83 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 84 if ("max-lines".equals(name)) { 85 maxLine = (Integer ) parameter; 86 } else { 87 throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName()); 88 } 89 return true; 90 } 91 92 95 public String getConfigInfo() { 96 if (maxLine==null) { 97 return super.getConfigInfo(); 98 } else { 99 StringBuffer ret=new StringBuffer ("Allowed maximum file length:\n"); 100 ret.append("max-lines: " + maxLine + "\n"); 101 return ret.toString(); 102 } 103 } 104 } 105 | Popular Tags |