Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Demo.java

00001 package edu.ksu.cis.bandera.bofa;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1999, 2000, 2001                                    *
00006  * Venkatesh Prasad Ranganath (rvprasad@cis.ksu.edu)                 *
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 
00037 import ca.mcgill.sable.soot.*;
00038 import ca.mcgill.sable.soot.jimple.*;
00039 import ca.mcgill.sable.util.*;
00040 import edu.ksu.cis.bandera.jjjc.CompilationManager;
00041 import java.io.PrintWriter;
00042 import org.apache.log4j.Category;
00043 
00044 /*
00045  * Demo.java
00046  * $Id: Demo.java,v 1.1.1.1 2002/01/24 03:42:07 pserver Exp $
00047  */
00048 
00049 /**
00050  * This class tests the implementation of BOFA and provides an example of how to
00051  * use BOFA.
00052  *
00053  * @author 
00054  * <a href="http://www.cis.ksu.edu/~rvprasadVenkatesh Prasad Ranganath</a>
00055 
00056  * @version $Name:  $($Revision: 1.1.1.1 $)
00057  */
00058 class Demo {
00059 
00060     /**
00061      * Provides the logging capability through log4j.
00062      *
00063      */
00064     private static Category cat;
00065 
00066     static {
00067         cat = Category.getInstance("edu.ksu.cis.bandera.bofa.Demo");
00068     }
00069 
00070     /**
00071      * Analyzes the given collection of classes.
00072      *
00073      * @param storedClasses a collection of <code>SootClass</code>es
00074      * representing the classes to be analyzed.
00075      */
00076     static void analyze(Collection storedClasses)
00077     {
00078         SootClass sc;
00079         SootMethod sm;
00080         TempExpr expr = new TempExpr();
00081         TempStmt stmt = new TempStmt(expr);
00082 
00083         if (storedClasses == null || storedClasses.size() == 0) {
00084             System.out.println("You need to compile before you do " +
00085                                "Flow Analysis");
00086             return;
00087         }
00088 
00089         for (Iterator e = storedClasses.iterator(); e.hasNext();) {
00090             Object o = e.next();
00091             if (o instanceof java.lang.String) {
00092                 cat.warn("SootClass not available.  -- " + o.getClass());
00093                 continue;
00094             } // end of if (o instanceof java.lang.String)
00095             sc = (SootClass) o;
00096             System.out.println("###########################################" + 
00097                                "##########################"); 
00098             System.out.println("PROCESSING CLASS " + sc);
00099             for (Iterator methods =
00100                      sc.getMethods().iterator();methods.hasNext();) {
00101                 sm = (SootMethod)methods.next();
00102                 StmtList stmts;
00103                 System.out.println("=======================================" + 
00104                                    "=======================================");
00105                 System.out.println("PROCESSING METHOD " + sc + "." + sm);
00106                 try {
00107                     stmts = ((JimpleBody)sm.getBody(Jimple.v())).getStmtList(); 
00108                 } catch (RuntimeException ee) {
00109                     cat.warn(ee.getMessage());
00110                     continue;
00111                 } // end of try-catch
00112                 
00113                 if (!FA.isReachable(sm)){
00114                     cat.warn("Unreachable method: " + sm);
00115                     continue;
00116                 }
00117                 expr.init(sm);
00118                 stmt.init(sm, stmts);
00119                 for (Iterator i = stmts.iterator(); i.hasNext();) {
00120                     Stmt s = (Stmt)i.next();
00121                     System.out.println("*********************************" + 
00122                                        "*********************************");
00123                     System.out.println("PROCESSING STMT " + s);
00124                     stmt.build(s);
00125                 } // end of for ()
00126             } // end of for ()
00127         }
00128     }
00129     /**
00130      * Uses JJJC to generate the jimple representation of the given source files.
00131      *
00132      * @param args names of java source files.
00133      */
00134     static void jjjc (String[] args) {
00135         LinkedList classes = new LinkedList();
00136         for (int i = 1; i < args.length; i++) {
00137             classes.addLast(args[i].trim());
00138         }
00139         BOFA.compile(".", classes, new LinkedList());
00140         BOFA.analyze();
00141         BOFA.dumpFile(".");
00142         analyze(Util.convert("ca.mcgill.sable.util.VectorList", 
00143                              CompilationManager.getCompiledClasses().values())); 
00144     } // end of main ()
00145     /**
00146      * Entry point to the demo.
00147      *
00148      * @param args mode and arguments.  In case the mode is <i>jjjc</i> the
00149      * arguments need to be all the java source files to be used in compilation.
00150      * In case the mode is <i>soot</i> the arguments need to be the names of the
00151      * classes that need to be analyzed.
00152      */
00153     public static void main (String[] args) {
00154         if (args[0].equals("jjjc")) {
00155             jjjc(args);
00156         } else if (args[0].equals("soot")){
00157             soot(args);
00158         } else {
00159             System.out.println("Usage: java edu.ksu.cis.bandera.bofa.Demo" + 
00160                                "[jjjc|soot] classlist");
00161             System.exit(0);
00162         } // end of else
00163     } // end of main ()
00164     /**
00165      * Uses Soot to generate the jimple representation of the given classes.
00166      *
00167      * @param args names of the classes.
00168      */
00169     static void soot(String[] args) {
00170         SootClassManager scm = new SootClassManager();
00171         LinkedList storedclasses = new LinkedList();
00172         java.io.PrintWriter p = new java.io.PrintWriter(System.out, true);
00173         BuildAndStoreBody basb = 
00174             new BuildAndStoreBody(Jimple.v(), 
00175                                   new StoredBody(ClassFile.v())); 
00176         for (int i = 1; i < args.length; i++) {
00177             SootClass sc = scm.getClass(args[i]);
00178             sc.resolve();
00179             sc.printTo(basb, p);
00180             storedclasses.add(sc);
00181         }
00182         BOFA.analyze(scm, storedclasses);//scm.getClasses()
00183         analyze(storedclasses);
00184     }
00185 }

Generated at Thu Feb 7 06:43:38 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001