1 24 25 package org.hammurapi.inspectors; 26 27 import java.util.Vector ; 28 29 import org.hammurapi.HammurapiException; 30 import org.hammurapi.InspectorBase; 31 32 import com.pavelvlasov.jsel.CompilationUnit; 33 import com.pavelvlasov.jsel.JselException; 34 import com.pavelvlasov.jsel.LanguageElement; 35 import com.pavelvlasov.jsel.Method; 36 import com.pavelvlasov.jsel.Operation; 37 import com.pavelvlasov.jsel.TypeDefinition; 38 import com.pavelvlasov.jsel.VariableDefinition; 39 40 51 public class StatelessSingleton extends InspectorBase { 52 53 private com.pavelvlasov.jsel.Class currentType = null ; 54 private boolean currentIsSingleton = false; 55 private Vector listOfNotFinalFields = null; 56 57 58 public void init (){; 59 currentIsSingleton = false; 60 listOfNotFinalFields = new Vector (); 61 } 62 63 public void visit(com.pavelvlasov.jsel.Class type) { 64 init(); 65 currentType = type; 66 } 67 68 public boolean isSingleton(VariableDefinition element) throws JselException { 69 LanguageElement parent=element.getParent(); 70 if (parent instanceof TypeDefinition) { 71 boolean ret = currentType.getFcn().equals( element.getTypeSpecification().getType().getName() ); 73 return ret ; 75 } else { 76 return false; 78 } 79 } 80 81 public void visit(VariableDefinition element) { 82 84 LanguageElement el = element.getParent(); 86 if ( el.getClass().equals( com.pavelvlasov.jsel.impl.ClassImpl.class) ){ 88 try { 89 if (el != null && isSingleton(element) && element.getModifiers().contains("static") 90 ) { 92 context.info(currentType, "Singleton detected" ); 93 context.getSession().getContext("ER-213").reportViolation( element, "Singleton detected" ); 94 this.currentIsSingleton = true; 95 } 96 if ( ! element.getModifiers().contains("final") ){ 97 listOfNotFinalFields.add (element); 99 } 100 } catch (JselException e) { 101 context.warn(element.getEnclosingType(), e); 102 } 103 } } 105 106 public void leave(TypeDefinition cu) throws HammurapiException { 107 this.checkStatelessSingleton(); 109 } 110 111 public void checkStatelessSingleton(){ 112 113 if ( currentIsSingleton && listOfNotFinalFields.size() > 1){ 115 context.info(currentType, "Singleton has " + listOfNotFinalFields.size() + " non final fields."); 116 context.getSession().getContext("ER-212").reportViolation( currentType, "Singleton contains " + listOfNotFinalFields.size() + " not final fields (together with the Singleton field)" ); 117 } 118 } 119 } 120 121 | Popular Tags |