1 23 package org.hammurapi.inspectors; 24 25 import org.hammurapi.InspectorBase; 26 import org.hammurapi.HammurapiException; 27 28 import com.pavelvlasov.jsel.Method; 29 import com.pavelvlasov.jsel.expressions.MethodCall; 30 import com.pavelvlasov.util.AccumulatingVisitorExceptionSink; 31 import com.pavelvlasov.util.DispatchingVisitor; 32 33 39 public class SuperCloneRule extends InspectorBase { 40 41 42 45 public static class MethodCallSnooper { 46 47 50 java.util.List returns=new java.util.ArrayList (); 51 52 55 private static final String SUPER_CLONE_CALL = "super.clone"; 56 57 62 public void visit(MethodCall call) { 63 String callTxt = call.getName().toString(); 64 if (callTxt.indexOf(SUPER_CLONE_CALL)==0 && 65 call.getParameters().size()==0) { 66 67 returns.add(call); 68 } 69 } 70 } 71 72 75 private static final String CLONE_NAME = "clone"; 76 77 80 private static final String CHAINED_ERRS = 81 "There have been exceptions (see above)"; 82 83 90 public void visit(Method element) throws HammurapiException { 91 if (element.getName().compareTo(CLONE_NAME)==0 && 92 element.getParameters().size()==0) { 93 94 AccumulatingVisitorExceptionSink es = new AccumulatingVisitorExceptionSink(); 95 MethodCallSnooper rs = new MethodCallSnooper(); 96 element.accept(new DispatchingVisitor(rs, es)); 97 98 if(rs.returns.size()!=1) { 99 context.reportViolation(element); 100 } 101 if (!es.getExceptions().isEmpty()) { 102 es.dump(); 103 throw new HammurapiException(CHAINED_ERRS); 104 } 105 } 106 } 107 108 } 109 | Popular Tags |