00001 package edu.ksu.cis.bandera.pdgslicer.dependency;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import javax.swing.*;
00036 import javax.swing.tree.*;
00037 import java.util.*;
00038 import java.awt.*;
00039 import ca.mcgill.sable.util.Map;
00040 import ca.mcgill.sable.util.Iterator;
00041 import ca.mcgill.sable.util.List;
00042 import ca.mcgill.sable.util.ArrayList;
00043 import ca.mcgill.sable.soot.*;
00044 import edu.ksu.cis.bandera.pdgslicer.*;
00045 import edu.ksu.cis.bandera.jjjc.CompilationManager;
00046
00047
00048
00049
00050
00051
00052 public class CallGraphDialog extends JDialog {
00053 private SootClassManager scm = CompilationManager.getSootClassManager();
00054 private Hashtable newOldSmTable = new Hashtable();
00055 private Map sootMethodInfoMap = Slicer.sootMethodInfoMap;
00056 private DefaultMutableTreeNode callGraphTreeRoot = null;
00057 private DefaultMutableTreeNode succCallGraphTreeRoot = null;
00058 private DefaultMutableTreeNode predCallGraphTreeRoot = null;
00059 private boolean succCallGraph = true;
00060 private boolean predCallGraph = false;
00061 private JPanel ivjCallGraphDialogContentPane = null;
00062 private JToolBar ivjCallGraphToolBar = null;
00063 private JTree ivjCallGraphTree = null;
00064 private JScrollPane ivjCallGraphTreeScrollPane = null;
00065 private JButton ivjOkButton = null;
00066 private JButton ivjPredToolBarButton = null;
00067 private JButton ivjSuccToolBarButton = null;
00068 IvjEventHandler ivjEventHandler = new IvjEventHandler();
00069 class IvjEventHandler implements java.awt.event.ActionListener {
00070 public void actionPerformed(java.awt.event.ActionEvent e) {
00071 if (e.getSource() == CallGraphDialog.this.getSuccToolBarButton())
00072 connEtoC1();
00073 if (e.getSource() == CallGraphDialog.this.getPredToolBarButton())
00074 connEtoC2();
00075 if (e.getSource() == CallGraphDialog.this.getOkButton())
00076 connEtoM1(e);
00077 };
00078 };
00079
00080
00081
00082 public CallGraphDialog() {
00083 super();
00084 initialize();
00085 }
00086
00087
00088
00089
00090 public CallGraphDialog(java.awt.Dialog owner) {
00091 super(owner);
00092 }
00093
00094
00095
00096
00097
00098 public CallGraphDialog(java.awt.Dialog owner, String title) {
00099 super(owner, title);
00100 }
00101
00102
00103
00104
00105
00106
00107 public CallGraphDialog(java.awt.Dialog owner, String title, boolean modal) {
00108 super(owner, title, modal);
00109 }
00110
00111
00112
00113
00114
00115 public CallGraphDialog(java.awt.Dialog owner, boolean modal) {
00116 super(owner, modal);
00117 }
00118
00119
00120
00121
00122 public CallGraphDialog(java.awt.Frame owner) {
00123 super(owner);
00124 }
00125
00126
00127
00128
00129
00130 public CallGraphDialog(java.awt.Frame owner, String title) {
00131 super(owner, title);
00132 }
00133
00134
00135
00136
00137
00138
00139 public CallGraphDialog(java.awt.Frame owner, String title, boolean modal) {
00140 super(owner, title, modal);
00141 }
00142
00143
00144
00145
00146
00147 public CallGraphDialog(java.awt.Frame owner, boolean modal) {
00148 super(owner, modal);
00149 }
00150
00151
00152
00153
00154 public void buildCallGraphTree() {
00155 if (succCallGraph) {
00156 buildSuccCallGraphTree();
00157 } else
00158 if (predCallGraph)
00159 buildPredCallGraphTree();
00160 getCallGraphTree().setModel(new DefaultTreeModel(callGraphTreeRoot));
00161 getCallGraphTreeScrollPane().validate();
00162 getCallGraphTreeScrollPane().repaint();
00163 }
00164 private void buildPredCallGraphTree() {
00165 if (predCallGraphTreeRoot != null) {
00166 callGraphTreeRoot = predCallGraphTreeRoot;
00167 return;
00168 }
00169 predCallGraphTreeRoot = new DefaultMutableTreeNode("Pred Call Graph");
00170 callGraphTreeRoot = predCallGraphTreeRoot;
00171 Hashtable callersForCurrentLevel = new Hashtable();
00172 Hashtable callersForNextLevel;
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 Set currentCallers = new HashSet();
00183 for (Iterator mdIt = sootMethodInfoMap.keySet().iterator(); mdIt.hasNext();) {
00184
00185 SootMethod sm = (SootMethod) mdIt.next();
00186 MethodInfo mdInfo = (MethodInfo) sootMethodInfoMap.get(sm);
00187 Map callSiteMap = mdInfo.indexMaps.getCallSiteMap();
00188 if (callSiteMap == null || callSiteMap.isEmpty()) {
00189 SootMethod newSm = getNewSootMethod(mdInfo.sootClass, sm);
00190 currentCallers.add(newSm);
00191 newOldSmTable.put(newSm, sm);
00192 }
00193 }
00194 callersForCurrentLevel.put(callGraphTreeRoot, currentCallers);
00195 do {
00196 callersForNextLevel = new Hashtable();
00197 for (java.util.Iterator calleeIt = callersForCurrentLevel.keySet().iterator(); calleeIt.hasNext();) {
00198 DefaultMutableTreeNode currentCallee = (DefaultMutableTreeNode) calleeIt.next();
00199 currentCallers = (HashSet) callersForCurrentLevel.get(currentCallee);
00200 for (java.util.Iterator callerIt = currentCallers.iterator(); callerIt.hasNext();) {
00201 SootMethod sm = (SootMethod) callerIt.next();
00202 DefaultMutableTreeNode smTreeNode = new DefaultMutableTreeNode(sm);
00203 currentCallee.add(smTreeNode);
00204 Set callersForSm = getCallersFor(sm);
00205 if (!callersForSm.isEmpty())
00206 callersForNextLevel.put(smTreeNode, callersForSm);
00207 }
00208 }
00209 callersForCurrentLevel = callersForNextLevel;
00210 } while (!callersForNextLevel.isEmpty());
00211 }
00212 private void buildSuccCallGraphTree() {
00213 if (succCallGraphTreeRoot != null) {
00214 callGraphTreeRoot = succCallGraphTreeRoot;
00215 return;
00216 }
00217 succCallGraphTreeRoot = new DefaultMutableTreeNode("Succ Call Graph");
00218 callGraphTreeRoot = succCallGraphTreeRoot;
00219 Hashtable callSitesForCurrentLevel = new Hashtable();
00220 Hashtable callSitesForNextLevel;
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 Set currentCallSites = new HashSet();
00232 for (Iterator mdIt = sootMethodInfoMap.keySet().iterator(); mdIt.hasNext();) {
00233
00234 SootMethod sm = (SootMethod) mdIt.next();
00235 MethodInfo mdInfo = (MethodInfo) sootMethodInfoMap.get(sm);
00236 if (mdInfo.whoCallMe == null || mdInfo.whoCallMe.isEmpty()) {
00237 SootMethod newSm = getNewSootMethod(mdInfo.sootClass, sm);
00238 currentCallSites.add(newSm);
00239 newOldSmTable.put(newSm, sm);
00240 }
00241 }
00242 callSitesForCurrentLevel.put(callGraphTreeRoot, currentCallSites);
00243 do {
00244 callSitesForNextLevel = new Hashtable();
00245 for (java.util.Iterator callerIt = callSitesForCurrentLevel.keySet().iterator(); callerIt.hasNext();) {
00246 DefaultMutableTreeNode currentCaller = (DefaultMutableTreeNode) callerIt.next();
00247 currentCallSites = (HashSet) callSitesForCurrentLevel.get(currentCaller);
00248 for (java.util.Iterator siteIt = currentCallSites.iterator(); siteIt.hasNext();) {
00249 SootMethod sm = (SootMethod) siteIt.next();
00250
00251 DefaultMutableTreeNode smTreeNode = new DefaultMutableTreeNode(sm);
00252 currentCaller.add(smTreeNode);
00253 Set callSitesForSm = getCallSitesFor(sm);
00254 if (!callSitesForSm.isEmpty())
00255 callSitesForNextLevel.put(smTreeNode, callSitesForSm);
00256
00257 }
00258 }
00259 callSitesForCurrentLevel = callSitesForNextLevel;
00260 } while (!callSitesForNextLevel.isEmpty());
00261 }
00262
00263
00264
00265
00266 private void connEtoC1() {
00267 try {
00268
00269
00270 this.succToolBarButton_ActionEvents();
00271
00272
00273 } catch (java.lang.Throwable ivjExc) {
00274
00275
00276 handleException(ivjExc);
00277 }
00278 }
00279
00280
00281
00282
00283 private void connEtoC2() {
00284 try {
00285
00286
00287 this.predToolBarButton_ActionEvents();
00288
00289
00290 } catch (java.lang.Throwable ivjExc) {
00291
00292
00293 handleException(ivjExc);
00294 }
00295 }
00296
00297
00298
00299
00300
00301 private void connEtoM1(java.awt.event.ActionEvent arg1) {
00302 try {
00303
00304
00305 this.dispose();
00306
00307
00308 } catch (java.lang.Throwable ivjExc) {
00309
00310
00311 handleException(ivjExc);
00312 }
00313 }
00314
00315
00316
00317
00318 private static void getBuilderData() {
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 }
00355 private Set getCallersFor(SootMethod newSm) {
00356 SootMethod sm = (SootMethod) newOldSmTable.get(newSm);
00357 Set callers = new HashSet();
00358 MethodInfo methodInfo = (MethodInfo) sootMethodInfoMap.get(sm);
00359 ca.mcgill.sable.util.Set whoCallme = methodInfo.whoCallMe;
00360 if (whoCallme == null)
00361 return callers;
00362 for (Iterator callerIt = whoCallme.iterator(); callerIt.hasNext();) {
00363 CallSite callSite = (CallSite) callerIt.next();
00364 SootMethod callSm = callSite.callerSootMethod;
00365 MethodInfo callSmInfo = (MethodInfo) sootMethodInfoMap.get(callSm);
00366 SootMethod newCallSm = getNewSootMethod(callSmInfo.sootClass, callSm);
00367 callers.add(newCallSm);
00368 newOldSmTable.put(newCallSm, callSm);
00369 }
00370 return callers;
00371 }
00372
00373
00374
00375
00376
00377 private javax.swing.JPanel getCallGraphDialogContentPane() {
00378 if (ivjCallGraphDialogContentPane == null) {
00379 try {
00380 ivjCallGraphDialogContentPane = new javax.swing.JPanel();
00381 ivjCallGraphDialogContentPane.setName("CallGraphDialogContentPane");
00382 ivjCallGraphDialogContentPane.setLayout(new java.awt.GridBagLayout());
00383
00384 java.awt.GridBagConstraints constraintsCallGraphToolBar = new java.awt.GridBagConstraints();
00385 constraintsCallGraphToolBar.gridx = 0; constraintsCallGraphToolBar.gridy = 0;
00386 constraintsCallGraphToolBar.fill = java.awt.GridBagConstraints.HORIZONTAL;
00387 constraintsCallGraphToolBar.weightx = 1.0;
00388 constraintsCallGraphToolBar.insets = new java.awt.Insets(4, 4, 4, 4);
00389 getCallGraphDialogContentPane().add(getCallGraphToolBar(), constraintsCallGraphToolBar);
00390
00391 java.awt.GridBagConstraints constraintsCallGraphTreeScrollPane = new java.awt.GridBagConstraints();
00392 constraintsCallGraphTreeScrollPane.gridx = 0; constraintsCallGraphTreeScrollPane.gridy = 1;
00393 constraintsCallGraphTreeScrollPane.fill = java.awt.GridBagConstraints.BOTH;
00394 constraintsCallGraphTreeScrollPane.weightx = 1.0;
00395 constraintsCallGraphTreeScrollPane.weighty = 1.0;
00396 constraintsCallGraphTreeScrollPane.insets = new java.awt.Insets(4, 4, 4, 4);
00397 getCallGraphDialogContentPane().add(getCallGraphTreeScrollPane(), constraintsCallGraphTreeScrollPane);
00398
00399 java.awt.GridBagConstraints constraintsOkButton = new java.awt.GridBagConstraints();
00400 constraintsOkButton.gridx = 0; constraintsOkButton.gridy = 2;
00401 constraintsOkButton.fill = java.awt.GridBagConstraints.HORIZONTAL;
00402 constraintsOkButton.insets = new java.awt.Insets(4, 4, 4, 4);
00403 getCallGraphDialogContentPane().add(getOkButton(), constraintsOkButton);
00404
00405
00406 } catch (java.lang.Throwable ivjExc) {
00407
00408
00409 handleException(ivjExc);
00410 }
00411 }
00412 return ivjCallGraphDialogContentPane;
00413 }
00414
00415
00416
00417
00418
00419 private javax.swing.JToolBar getCallGraphToolBar() {
00420 if (ivjCallGraphToolBar == null) {
00421 try {
00422 ivjCallGraphToolBar = new javax.swing.JToolBar();
00423 ivjCallGraphToolBar.setName("CallGraphToolBar");
00424 ivjCallGraphToolBar.setBackground(new java.awt.Color(204,204,255));
00425 ivjCallGraphToolBar.add(getSuccToolBarButton());
00426 getCallGraphToolBar().add(getPredToolBarButton(), getPredToolBarButton().getName());
00427
00428
00429 } catch (java.lang.Throwable ivjExc) {
00430
00431
00432 handleException(ivjExc);
00433 }
00434 }
00435 return ivjCallGraphToolBar;
00436 }
00437
00438
00439
00440
00441
00442 private javax.swing.JTree getCallGraphTree() {
00443 if (ivjCallGraphTree == null) {
00444 try {
00445 ivjCallGraphTree = new javax.swing.JTree();
00446 ivjCallGraphTree.setName("CallGraphTree");
00447 ivjCallGraphTree.setBounds(0, 0, 78, 72);
00448
00449 getCallGraphTree().setModel(new DefaultTreeModel(new DefaultMutableTreeNode("")));
00450 getCallGraphTreeScrollPane().validate();
00451 getCallGraphTreeScrollPane().repaint();
00452 ivjCallGraphTree.setUI(new javax.swing.plaf.metal.MetalTreeUI() {
00453 public javax.swing.plaf.metal.MetalTreeUI setAngledColor() {
00454 setHashColor(Color.black);
00455 return this;
00456 }
00457 }
00458 .setAngledColor());
00459 ivjCallGraphTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
00460 ivjCallGraphTree.setCellRenderer(new DefaultTreeCellRenderer() {
00461 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
00462 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
00463 Object o = ((DefaultMutableTreeNode) value).getUserObject();
00464 setForeground(Color.black);
00465 setIcon(null);
00466 return this;
00467 }
00468 });
00469
00470 ivjCallGraphTree.putClientProperty("JTree.lineStyle", "Angled");
00471
00472
00473 } catch (java.lang.Throwable ivjExc) {
00474
00475
00476 handleException(ivjExc);
00477 }
00478 }
00479 return ivjCallGraphTree;
00480 }
00481
00482
00483
00484
00485
00486 private javax.swing.JScrollPane getCallGraphTreeScrollPane() {
00487 if (ivjCallGraphTreeScrollPane == null) {
00488 try {
00489 ivjCallGraphTreeScrollPane = new javax.swing.JScrollPane();
00490 ivjCallGraphTreeScrollPane.setName("CallGraphTreeScrollPane");
00491 getCallGraphTreeScrollPane().setViewportView(getCallGraphTree());
00492
00493
00494 } catch (java.lang.Throwable ivjExc) {
00495
00496
00497 handleException(ivjExc);
00498 }
00499 }
00500 return ivjCallGraphTreeScrollPane;
00501 }
00502 private Set getCallSitesFor(SootMethod newSm) {
00503 SootMethod sm = (SootMethod) newOldSmTable.get(newSm);
00504 Set callSites = new HashSet();
00505 MethodInfo mdInfo = (MethodInfo) sootMethodInfoMap.get(sm);
00506 Map callSiteMap = mdInfo.indexMaps.getCallSiteMap();
00507 if (callSiteMap.isEmpty())
00508 return callSites;
00509 for (Iterator siteIt = callSiteMap.keySet().iterator(); siteIt.hasNext();) {
00510 CallSite callSite = (CallSite) siteIt.next();
00511 SootMethod sootMethod = (SootMethod) callSiteMap.get(callSite);
00512 MethodInfo smdInfo = (MethodInfo) sootMethodInfoMap.get(sootMethod);
00513 SootMethod newSootMethod = getNewSootMethod(smdInfo.sootClass, sootMethod);
00514 newOldSmTable.put(newSootMethod, sootMethod);
00515 callSites.add(newSootMethod);
00516 }
00517 return callSites;
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 private SootMethod getNewSootMethod(SootClass sc, SootMethod sm) {
00527 SootClass newSc = scm.getClass(sc.getName());
00528 SootMethod newSm = newSc.getMethod(sm.getName());
00529 return newSm;
00530 }
00531
00532
00533
00534
00535
00536 private javax.swing.JButton getOkButton() {
00537 if (ivjOkButton == null) {
00538 try {
00539 ivjOkButton = new javax.swing.JButton();
00540 ivjOkButton.setName("OkButton");
00541 ivjOkButton.setText("OK");
00542 ivjOkButton.setBackground(new java.awt.Color(204,204,255));
00543
00544
00545 } catch (java.lang.Throwable ivjExc) {
00546
00547
00548 handleException(ivjExc);
00549 }
00550 }
00551 return ivjOkButton;
00552 }
00553
00554
00555
00556
00557
00558 private javax.swing.JButton getPredToolBarButton() {
00559 if (ivjPredToolBarButton == null) {
00560 try {
00561 ivjPredToolBarButton = new javax.swing.JButton();
00562 ivjPredToolBarButton.setName("PredToolBarButton");
00563 ivjPredToolBarButton.setText("pred");
00564 ivjPredToolBarButton.setBackground(new java.awt.Color(204,204,255));
00565 ivjPredToolBarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
00566 ivjPredToolBarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
00567 ivjPredToolBarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/ksu/cis/bandera/pdgslicer/dependency/images/pred_arrow_iii.gif")));
00568 ivjPredToolBarButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
00569 ivjPredToolBarButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/ksu/cis/bandera/pdgslicer/dependency/images/pred_arrow_iii_pressed.gif")));
00570
00571
00572 } catch (java.lang.Throwable ivjExc) {
00573
00574
00575 handleException(ivjExc);
00576 }
00577 }
00578 return ivjPredToolBarButton;
00579 }
00580
00581
00582
00583
00584
00585 private javax.swing.JButton getSuccToolBarButton() {
00586 if (ivjSuccToolBarButton == null) {
00587 try {
00588 ivjSuccToolBarButton = new javax.swing.JButton();
00589 ivjSuccToolBarButton.setName("SuccToolBarButton");
00590 ivjSuccToolBarButton.setText("succ");
00591 ivjSuccToolBarButton.setBackground(new java.awt.Color(204,204,255));
00592 ivjSuccToolBarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
00593 ivjSuccToolBarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
00594 ivjSuccToolBarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/ksu/cis/bandera/pdgslicer/dependency/images/succ_arrow_iii_pressed.gif")));
00595 ivjSuccToolBarButton.setSelected(false);
00596 ivjSuccToolBarButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
00597 ivjSuccToolBarButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/ksu/cis/bandera/pdgslicer/dependency/images/succ_arrow_iii.gif")));
00598
00599
00600 } catch (java.lang.Throwable ivjExc) {
00601
00602
00603 handleException(ivjExc);
00604 }
00605 }
00606 return ivjSuccToolBarButton;
00607 }
00608
00609
00610
00611
00612 private void handleException(java.lang.Throwable exception) {
00613
00614
00615
00616
00617 }
00618
00619
00620
00621
00622
00623 private void initConnections() throws java.lang.Exception {
00624
00625
00626 getSuccToolBarButton().addActionListener(ivjEventHandler);
00627 getPredToolBarButton().addActionListener(ivjEventHandler);
00628 getOkButton().addActionListener(ivjEventHandler);
00629 }
00630
00631
00632
00633
00634 private void initialize() {
00635 try {
00636
00637
00638 setName("CallGraphDialog");
00639 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
00640 setSize(475, 312);
00641 setTitle("Call Graph");
00642 setContentPane(getCallGraphDialogContentPane());
00643 initConnections();
00644 } catch (java.lang.Throwable ivjExc) {
00645 handleException(ivjExc);
00646 }
00647
00648
00649 }
00650
00651
00652
00653
00654 public static void main(java.lang.String[] args) {
00655 try {
00656 CallGraphDialog aCallGraphDialog;
00657 aCallGraphDialog = new CallGraphDialog();
00658 aCallGraphDialog.setModal(true);
00659 aCallGraphDialog.addWindowListener(new java.awt.event.WindowAdapter() {
00660 public void windowClosing(java.awt.event.WindowEvent e) {
00661 System.exit(0);
00662 };
00663 });
00664 aCallGraphDialog.setVisible(true);
00665 } catch (Throwable exception) {
00666 System.err.println("Exception occurred in main() of javax.swing.JDialog");
00667 exception.printStackTrace(System.out);
00668 }
00669 }
00670
00671
00672
00673 public void predToolBarButton_ActionEvents() {
00674 Icon temp = getPredToolBarButton().getPressedIcon();
00675 getPredToolBarButton().setPressedIcon(getPredToolBarButton().getIcon());
00676 getPredToolBarButton().setIcon(temp);
00677 predCallGraph = !predCallGraph;
00678 if (predCallGraph) {
00679
00680 if (succCallGraph)
00681 succToolBarButton_ActionEvents();
00682 buildCallGraphTree();
00683 } else
00684
00685
00686 if (!succCallGraph)
00687 succToolBarButton_ActionEvents();
00688 return;
00689 }
00690
00691
00692
00693 public void succToolBarButton_ActionEvents() {
00694 Icon temp = getSuccToolBarButton().getPressedIcon();
00695 getSuccToolBarButton().setPressedIcon(getSuccToolBarButton().getIcon());
00696 getSuccToolBarButton().setIcon(temp);
00697 succCallGraph = !succCallGraph;
00698 if (succCallGraph) {
00699
00700 if (predCallGraph)
00701 predToolBarButton_ActionEvents();
00702 buildCallGraphTree();
00703 } else
00704
00705
00706 if (!predCallGraph)
00707 predToolBarButton_ActionEvents();
00708 return;
00709 }
00710 }