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.Scope; 30 import com.pavelvlasov.jsel.TypeBody; 31 import com.pavelvlasov.jsel.statements.CompoundStatement; 32 import com.pavelvlasov.jsel.statements.ForStatement; 33 import com.pavelvlasov.jsel.statements.WhileStatement; 34 import com.pavelvlasov.review.SourceMarker; 35 36 42 public class LogicalNestingRule extends InspectorBase implements Parameterizable { 43 44 49 public void visit(CompoundStatement element) { 50 if (!isEmpty(element)) { 51 Scope prevScope=null; 53 int actNesting = -1; 54 for (Scope enclosingScope = element.getEnclosingScope(); !(enclosingScope==null || enclosingScope instanceof TypeBody); enclosingScope=enclosingScope.getEnclosingScope()) { 55 if (!((enclosingScope instanceof ForStatement || enclosingScope instanceof WhileStatement) && prevScope instanceof CompoundStatement)) { 56 actNesting++; 57 } 59 prevScope=enclosingScope; 60 } 61 62 if (actNesting>maxNesting.intValue()) { 63 context.reportViolation((SourceMarker) element, new Object [] {maxNesting, new Integer (actNesting)}); 64 } 70 } 71 } 72 73 77 private Integer maxNesting; 78 79 80 87 public boolean setParameter(String name, Object parameter) throws ConfigurationException { 88 if ("max-nesting".equals(name)) { 89 maxNesting = (Integer ) parameter; 90 } else { 91 throw new ConfigurationException("Parameter '"+name+"' is not supported by "+getClass().getName()); 92 } 93 return true; 94 } 95 96 99 public String getConfigInfo() { 100 if (maxNesting!=null) { 101 StringBuffer ret=new StringBuffer ("Allowed maximum logical nesting:\n"); 102 ret.append("max-nesting: " + maxNesting + "\n"); 103 return ret.toString(); 104 } else { 105 return ""; 106 } 107 } 108 } 109 | Popular Tags |