1 26 package org.hammurapi.inspectors.metrics; 27 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.util.Collection ; 31 import java.util.Enumeration ; 32 import java.util.Iterator ; 33 import java.util.Properties ; 34 35 import org.hammurapi.HammurapiException; 36 import org.hammurapi.InspectorBase; 37 import org.hammurapi.inspectors.metrics.callertrace.CallerTraceService; 38 import org.hammurapi.inspectors.metrics.callertrace.MethodMap; 39 import org.hammurapi.inspectors.metrics.callertrace.MethodWrapper; 40 import org.hammurapi.inspectors.metrics.callertrace.MethodWrapperDeclaration; 41 import org.hammurapi.inspectors.metrics.callertrace.MethodWrapperInvoked; 42 import org.hammurapi.results.AnnotationContext; 43 import org.hammurapi.results.LinkedAnnotation; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 47 import com.pavelvlasov.config.ConfigurationException; 48 import com.pavelvlasov.config.Parameterizable; 49 import com.pavelvlasov.jsel.Code; 50 import com.pavelvlasov.jsel.Constructor; 51 import com.pavelvlasov.jsel.JselException; 52 import com.pavelvlasov.jsel.LanguageElement; 53 import com.pavelvlasov.jsel.Method; 54 import com.pavelvlasov.jsel.Operation; 55 import com.pavelvlasov.jsel.OperationInfo; 56 import com.pavelvlasov.jsel.Repository; 57 import com.pavelvlasov.jsel.TypeBody; 58 import com.pavelvlasov.jsel.TypeDefinition; 59 import com.pavelvlasov.jsel.expressions.MethodCall; 60 import com.pavelvlasov.jsel.impl.AbstractRepositoryImpl; 61 import com.pavelvlasov.jsel.impl.ClassImpl; 62 import com.pavelvlasov.render.RenderRequest; 63 import com.pavelvlasov.render.dom.AbstractRenderer; 64 import com.pavelvlasov.review.SourceMarker; 65 import com.pavelvlasov.util.Acceptor; 66 67 71 72 public class CallerTrace extends InspectorBase implements Parameterizable { 74 75 private CallerTraceService callerTraceService = new CallerTraceService(); 76 78 public void visit(Operation methodCall) { 79 } 81 82 public void visit(MethodCall methodCall) { 83 84 86 try { 87 AbstractRepositoryImpl repository = null; 88 OperationInfo opi = methodCall.getProvider(); 89 MethodWrapperInvoked mwi = new MethodWrapperInvoked( opi, (SourceMarker)methodCall ); 90 96 Code code = ((LanguageElement) methodCall).getEnclosingCode(); 97 if( code != null && code instanceof Operation ){ 99 Operation op = (Operation) code; 100 TypeBody tb = code.getEnclosingType(); 102 String key = tb.getFcn() + ">>" +op.getOperationSignature(); 104 105 107 MethodMap mp = callerTraceService.getAllMethodsImplemented(); 108 MethodWrapperDeclaration caller = (MethodWrapperDeclaration)mp.selectMethodsWithoutHashkey( key ) ; 109 110 112 if ( caller != null ){ 113 caller.getInvokedMethods().add(mwi); 114 115 } else { 116 } 118 119 mwi.setCallerMethod( caller ); 120 MethodWrapper invokeeDeclarationResolution = mwi; 121 MethodWrapperDeclaration invokeeDecla = (MethodWrapperDeclaration)mp.selectMethodsWithoutHashkey( mwi.getMethodKey() ) ; 122 123 if ( invokeeDecla != null ){ 124 invokeeDecla.setCalled(); 126 invokeeDecla.afferentMethodCoupling++; 127 invokeeDeclarationResolution = invokeeDecla; 129 130 } else { 131 } 133 callerTraceService.getAdjacencyMatrix().put( invokeeDeclarationResolution.getMethodKey(), caller.getMethodKey() ); 137 callerTraceService.getAllMethods().put(mwi.getMethodKey(), mwi); 138 callerTraceService.getAllInvokedMethodTable().put( mwi.getMethodKey(), mwi); 139 } 140 } catch (Exception e) { 142 e.printStackTrace(); 145 } 146 } 147 148 public void visit(Constructor aConstructor) { 149 try { 150 Repository repository = aConstructor.getCompilationUnit().getPackage().getRepository(); 151 MethodWrapperDeclaration mwd = new MethodWrapperDeclaration( aConstructor.getInfo(), (SourceMarker)aConstructor); 152 153 if( this.callerTraceService.getAllInterfacesAndImplementors().containsKey( mwd.getDeclaringType() )){ 154 callerTraceService.getAllInterfaceOperations().add( mwd ); 155 } 157 158 callerTraceService.getAllMethods().put(mwd.getMethodKey(),mwd); 159 callerTraceService.getAllMethodsImplemented().put(mwd.getMethodKey(),mwd); 160 } catch (JselException e) { 161 e.printStackTrace(); 163 } 164 } 165 166 167 public void visit(Method methodDecl) { 168 try { 169 Repository repository = methodDecl.getCompilationUnit().getPackage().getRepository(); 170 MethodWrapperDeclaration mwd = new MethodWrapperDeclaration(methodDecl.getInfo(), (SourceMarker)methodDecl); 171 172 if( this.callerTraceService.getAllInterfacesAndImplementors().containsKey( mwd.getDeclaringType() )){ 173 callerTraceService.getAllInterfaceOperations().add( mwd ); 174 } 176 177 callerTraceService.getAllMethods().put(mwd.getMethodKey(),mwd); 178 callerTraceService.getAllMethodsImplemented().put(mwd.getMethodKey(),mwd); 179 } catch (JselException e) { 180 e.printStackTrace(); 182 } 183 } 184 185 public void visit(com.pavelvlasov.jsel.Interface ifc) { 186 188 final String searchTerm = ifc.getFcn(); 189 190 Repository repository = ifc.getCompilationUnit().getPackage().getRepository(); 191 Collection classesOfInterest=repository.findAll(new Acceptor() { 192 public boolean accept(Object element) { 193 try { 194 return (element instanceof TypeDefinition && ((TypeDefinition) element).isKindOf( searchTerm)); 195 } catch (JselException e) { 196 e.printStackTrace(); 198 return false; 199 } 200 } 201 }); 202 if (classesOfInterest != null && classesOfInterest.size()>0 ){ 203 this.callerTraceService.getAllInterfacesAndImplementors().put( ifc.getName(), classesOfInterest ); 205 } 206 207 } 208 209 210 214 215 private void resolveInvokeesImplementors(){ 216 callerTraceService.resolveInvokeesImplementors(); 217 } 218 219 223 224 private void handleInterfaces() { 225 226 for( int i=0; i<this.callerTraceService.getAllInterfaceOperations().size(); i++){ 227 MethodWrapperDeclaration interFaceOp = (MethodWrapperDeclaration)this.callerTraceService.getAllInterfaceOperations().elementAt(i); 228 String keyIf = interFaceOp.getDeclaringType()+ ">>" +interFaceOp.getSignature(); 229 Enumeration enumIF = this.callerTraceService.getAllInterfacesAndImplementors().keys(); 231 while ( enumIF.hasMoreElements() ){ 232 233 String key = (String )enumIF.nextElement(); 234 236 Collection classes = (Collection )this.callerTraceService.getAllInterfacesAndImplementors().get(key); 237 Iterator itcls = classes.iterator(); 238 while ( itcls.hasNext() ){ 239 Object obj = itcls.next(); 241 if( obj instanceof ClassImpl){ 242 String keyImpl = ((ClassImpl) obj).getFcn()+ ">>" + interFaceOp.getSignature(); 244 MethodWrapperDeclaration mwif = (MethodWrapperDeclaration)this.callerTraceService.getAllMethodsImplemented().selectMethodsWithoutHashkey( keyIf ) ; 249 MethodWrapperDeclaration mwd = (MethodWrapperDeclaration)this.callerTraceService.getAllMethodsImplemented().selectMethodsWithoutHashkey( keyImpl ) ; 250 callerTraceService.getAdjacencyMatrix().put(mwd.getMethodKey(), mwif.getMethodKey() ); 252 } 253 } 254 } 255 256 } 257 } 258 259 private void aggregate(){ 260 resolveInvokeesImplementors(); 262 handleInterfaces(); 263 264 callerTraceService.init(); 265 callerTraceService.traceCaller(); 266 } 267 268 public void leave(Repository repository) { 269 aggregate(); 270 271 context.annotate( new LinkedAnnotation() { 272 private String path; 273 274 public String getName() { 275 return getContext().getDescriptor().getName(); 276 } 277 278 public String getPath() { 279 return path; 280 } 281 282 public void render(AnnotationContext context) throws HammurapiException { 283 String errorCausingFile = ""; 284 285 287 class CallerTraceRenderer extends AbstractRenderer { 288 CallerTraceRenderer() { 289 super(new RenderRequest(CallerTrace.this)); 290 } 291 292 public Element render(Document document) { 293 CallerTrace ctInspector=(CallerTrace) request.getRenderee(); 294 295 Element ctInspectorElement=document.createElement("CallerTrace"); 296 callerTraceService.toDom( document , ctInspectorElement) ; 297 298 299 return ctInspectorElement; 300 } 301 } 303 304 AnnotationContext.FileEntry fileEntry=context.getNextFile(context.getExtension()); 305 path=fileEntry.getPath(); 306 308 AnnotationContext.FileEntry fileEntryXml=context.getNextFile(".xml"); 309 try { 310 CallerTraceRenderer renderer=new CallerTraceRenderer(); 311 FileOutputStream out=new FileOutputStream (fileEntry.getFile()); 312 313 renderer.setEmbeddedStyle(context.isEmbeddedStyle()); 314 try { 315 errorCausingFile = fileEntry.getFile().getAbsolutePath(); 316 renderer.render(context.getParameter("style")==null ? null : new FileInputStream (context.getParameter("style").toString()), out); 317 } finally { 318 out.close(); 319 } 320 FileOutputStream outXml=new FileOutputStream (fileEntryXml.getFile()); 322 323 try { 324 errorCausingFile = fileEntryXml.getFile().getAbsolutePath(); 325 renderer.setEmbeddedStyle(false); 327 renderer.render(outXml); 328 332 } finally { 333 outXml.close(); 334 } 335 } catch (Exception e) { 336 throw new HammurapiException("Can't save "+ errorCausingFile +". " +e.getMessage() ); 337 } 338 } 339 public Properties getProperties() { 340 Properties ret=new Properties (); 341 ret.setProperty("left-panel", "yes"); 342 ret.setProperty("target", "Caller Trace"); 343 return ret; 344 } 345 }); 346 } 347 348 349 357 public boolean setParameter(String name, Object value) throws ConfigurationException { 358 if (true) { 359 return true; 360 } else { 361 throw new ConfigurationException("Parameter '"+name+"' is not supported"); 362 } 363 } 364 } 365 366 | Popular Tags |