private List<ActionContributionItem> getActions() { List<ActionContributionItem> actions = new ArrayList<ActionContributionItem>(); List<IRSEDiagramEngine> diagEngines = new ArrayList<IRSEDiagramEngine>(PluggableDiagramsSupport.getRegisteredDiagramEngines()); for (final IRSEDiagramEngine diagramEngine : diagEngines) { IAction action = new Action(diagramEngine.diagramType(), diagramEngine.getImageDescriptor()) { @Override public void run() { BuildStatus.addUsage(diagramEngine.diagramUsageName()); try { generateDiagram(diagramEngine); } catch (Exception e) { System.out.println( "Exception while generating diagram based on navigation history. " + e.getMessage()); e.printStackTrace(); } } }; ActionContributionItem newActionItem = new ActionContributionItem(action); actions.add(newActionItem); } return actions; }
private static void generate(IRSEDiagramEngine diagramEngine) { if (diagramEngine == null) return; LinkedHashMap<IType, List<Invocation>> history = SeqUtil.getNavigationHistory(numNavigationsToInclude); List<CodeUnit> classUnits = new ArrayList<CodeUnit>(); List<CodeUnit> methodUnits = new ArrayList<CodeUnit>(); List<ArtifactFragment> toAddToDiagram = new ArrayList<ArtifactFragment>(); for (IType type : history.keySet()) { if (!openTabs.contains(type)) continue; Resource classOfNavigationRes = RJCore.jdtElementToResource(StoreUtil.getDefaultStoreRepository(), type); CodeUnit classOfNavigationCU = GenerateUtil.getCodeUnitForRes(classOfNavigationRes, null, classUnits, null); // Add method calls for (Invocation invocation : history.get(type)) { IType container = null; if (invocation.getMethodElement() != null) { IMethod method = invocation.getMethodElement(); container = method.getDeclaringType(); } if (container == null) continue; boolean containerIsAnOpenTab = false; for (IType navigatedType : openTabs) { if (navigatedType.equals(container)) { containerIsAnOpenTab = true; break; } } if (!containerIsAnOpenTab) continue; // Find the method declaration in which the invocation is made CompilationUnit cu = ASTUtil.getAst(IJEUtils.ije_getTypeRoot(type)); MethodDeclarationFinder finder = new MethodDeclarationFinder(cu); MethodDeclaration declaration = finder.findDeclaration(invocation.getStartPosition()); if (declaration == null || declaration.resolveBinding() == null || declaration.resolveBinding().getJavaElement() == null) continue; Resource declarationRes = RJCore.jdtElementToResource( StoreUtil.getDefaultStoreRepository(), declaration.resolveBinding().getJavaElement()); CodeUnit declarationThatMakesInvocation = GenerateUtil.getCodeUnitForRes(declarationRes, null, methodUnits, classOfNavigationCU); String instanceName = (invocation.getInvocation() instanceof SuperMethodInvocation) ? null : MethodUtil.getInstanceCalledOn(invocation); CodeUnit containerOfDeclOfInvokedMethod = GenerateUtil.getCodeUnitForRes( RJCore.jdtElementToResource(StoreUtil.getDefaultStoreRepository(), container), instanceName, classUnits, null); Resource declOfInvokedMethodRes = RJCore.jdtElementToResource( StoreUtil.getDefaultStoreRepository(), invocation.getMethodElement()); CodeUnit declOfInvokedMethodCU = GenerateUtil.getCodeUnitForRes( declOfInvokedMethodRes, null, methodUnits, containerOfDeclOfInvokedMethod); ArtifactRel rel = new ArtifactRel(declarationThatMakesInvocation, declOfInvokedMethodCU, RJCore.calls); declarationThatMakesInvocation.addSourceConnection(rel); declOfInvokedMethodCU.addTargetConnection(rel); } // Add inheritance relationships among classes for (Resource superClassRes : InstanceUtil.getSuperClasses(classOfNavigationRes)) { CodeUnit superClassCU = GenerateUtil.getCodeUnitForRes(superClassRes, null, classUnits, null); IJavaElement superClassElt = RJCore.resourceToJDTElement(StoreUtil.getDefaultStoreRepository(), superClassRes); if (!history.keySet().contains(superClassElt)) continue; // superclass has not been recently navigated if (!openTabs.contains(superClassElt)) continue; // superclass is not an open tab ArtifactRel inheritanceRel = new ArtifactRel(classOfNavigationCU, superClassCU, RJCore.inherits); classOfNavigationCU.addSourceConnection(inheritanceRel); superClassCU.addTargetConnection(inheritanceRel); } } for (CodeUnit classUnit : classUnits) { if (classUnit.getShownChildren().size() > 0) toAddToDiagram.add(classUnit); } int numBeingAdded = toAddToDiagram.size(); if (numBeingAdded < numNavigationsToInclude) { // Diagram going to be small, so add a reasonable number // of the open tabs that were most recently activated // (openTabs list is ordered with most recently activated first) for (int i = 0; i < openTabs.size() && numBeingAdded < numNavigationsToInclude; i++) { IType type = openTabs.get(i); // only adding top level tabs here, not any nested classes if (!(type instanceof SourceType) || ((SourceType) type).getParent().getElementType() == IJavaElement.TYPE) continue; Resource classOfNavigationRes = RJCore.jdtElementToResource(StoreUtil.getDefaultStoreRepository(), type); CodeUnit classOfNavigationCU = GenerateUtil.getCodeUnitForRes(classOfNavigationRes, null, classUnits, null); if (!toAddToDiagram.contains(classOfNavigationCU)) { toAddToDiagram.add(classOfNavigationCU); numBeingAdded++; } } } diagramEngine.openDiagramFromNavigatedTabs( toAddToDiagram, new ArrayList<ArtifactFragment>(classUnits)); }