1 23 package org.hammurapi.inspectors; 24 import org.hammurapi.InspectorBase; 25 import org.hammurapi.HammurapiException; 26 27 import com.pavelvlasov.jsel.Constructor; 28 import com.pavelvlasov.jsel.TypeDefinition; 29 import com.pavelvlasov.jsel.statements.Statement; 30 import com.pavelvlasov.jsel.statements.SuperConstructorCall; 31 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink; 32 import com.pavelvlasov.util.DispatchingVisitor; 33 34 47 public class UnnecessaryConstructorRule extends InspectorBase { 48 49 52 public static class ConstructorSnooper { 53 54 57 java.util.List constList = new java.util.ArrayList (); 58 59 64 public void visit(Constructor constructor) { 65 constList.add(constructor); 66 } 67 } 68 69 72 private static final String CHAINED_ERRS = 73 "There have been exceptions (see above)"; 74 75 76 82 public void visit(TypeDefinition element) throws HammurapiException { 83 AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink(); 84 ConstructorSnooper rs= new ConstructorSnooper(); 85 element.accept(new DispatchingVisitor(rs, es)); 86 87 if (rs.constList.size()==1) { 88 Constructor constructor = 89 (Constructor) rs.constList.get(0); 90 91 checkConstructor(constructor); 92 } 93 94 if (!es.getExceptions().isEmpty()) { 95 es.dump(); 96 throw new HammurapiException(CHAINED_ERRS); 97 } 98 } 99 100 103 private static final String PUBLIC = "public"; 104 105 108 private void checkConstructor(Constructor constructor) { 109 if (constructor.getModifiers().contains(PUBLIC) && 110 constructor.getParameters().size()==0) { 111 112 if (isEmpty(constructor.getCompoundStatement())) { 113 context.reportViolation(constructor); 114 } 115 else { 116 java.util.Iterator statements = 117 constructor.getCompoundStatement(). 118 getStatements().iterator(); 119 int cntr = 0; 120 while (statements.hasNext()) { 121 Statement statement = (Statement) statements.next(); 122 if (!(statement instanceof SuperConstructorCall)) { 123 cntr++; 124 } 125 } 126 if (cntr==0) { 127 context.reportViolation(constructor); 128 } 129 } 130 } 131 } 132 } 133 | Popular Tags |