1 23 package org.hammurapi.inspectors; 24 25 import org.hammurapi.InspectorBase; 26 import org.hammurapi.HammurapiException; 27 28 import com.pavelvlasov.jsel.JselException; 29 import com.pavelvlasov.jsel.Method; 30 import com.pavelvlasov.jsel.TypeSpecification; 31 import com.pavelvlasov.jsel.VariableDefinition; 32 import com.pavelvlasov.jsel.expressions.NewObject; 33 import com.pavelvlasov.jsel.statements.CompoundStatement; 34 import com.pavelvlasov.jsel.statements.Statement; 35 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink; 36 import com.pavelvlasov.util.DispatchingVisitor; 37 38 44 public class ConstructorsInCloneRule extends InspectorBase { 45 46 50 public static class NewSnooper { 51 52 56 private String classNameToReview; 57 58 62 NewSnooper(String theClassName) { 63 classNameToReview = theClassName; 64 } 65 66 69 java.util.List returns=new java.util.ArrayList (); 70 71 77 public void visit(NewObject newCall) throws JselException { 78 TypeSpecification ts = newCall.getTypeSpecification(); 79 if (classNameToReview.equals(ts.getType().getName())) { 80 returns.add(newCall); 81 } 82 } 83 } 84 85 88 private static final String CLONE_NAME = "clone"; 89 90 93 private static final String CHAINED_ERRS = 94 "There have been exceptions (see above)"; 95 96 103 public void visit(Method element) throws HammurapiException { 104 if (CLONE_NAME.equals(element.getName()) && element.getParameters().isEmpty()) { 105 CompoundStatement compoundStatement = element.getCompoundStatement(); 106 if (compoundStatement!=null) { 107 java.util.Iterator statements = compoundStatement.getStatements().iterator(); 108 109 while (statements.hasNext()) { 110 Statement statement = (Statement) statements.next(); 111 if (statement instanceof VariableDefinition) { 112 AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink(); 113 NewSnooper rs = new NewSnooper(element.getEnclosingType().getFcn()); 114 element.accept(new DispatchingVisitor(rs, es)); 115 116 if( !rs.returns.isEmpty()) { 117 context.reportViolation(element); 118 } 119 if (!es.getExceptions().isEmpty()) { 120 es.dump(); 121 throw new HammurapiException(CHAINED_ERRS); 122 } 123 } 124 } 125 } 126 } 127 } 128 } 129 | Popular Tags |