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

JimpleLiteralPrinter.java

00001 package edu.ksu.cis.bandera.birc;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998, 1999   James Corbett (corbett@hawaii.edu)     *
00006  * All rights reserved.                                              *
00007  *                                                                   *
00008  * This work was done as a project in the SAnToS Laboratory,         *
00009  * Department of Computing and Information Sciences, Kansas State    *
00010  * University, USA (http://www.cis.ksu.edu/santos).                  *
00011  * It is understood that any modification not identified as such is  *
00012  * not covered by the preceding statement.                           *
00013  *                                                                   *
00014  * This work is free software; you can redistribute it and/or        *
00015  * modify it under the terms of the GNU Library General Public       *
00016  * License as published by the Free Software Foundation; either      *
00017  * version 2 of the License, or (at your option) any later version.  *
00018  *                                                                   *
00019  * This work is distributed in the hope that it will be useful,      *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00022  * Library General Public License for more details.                  *
00023  *                                                                   *
00024  * You should have received a copy of the GNU Library General Public *
00025  * License along with this toolkit; if not, write to the             *
00026  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00027  * Boston, MA  02111-1307, USA.                                      *
00028  *                                                                   *
00029  * Java is a trademark of Sun Microsystems, Inc.                     *
00030  *                                                                   *
00031  * To submit a bug report, send a comment, or get the latest news on *
00032  * this project and other SAnToS projects, please visit the web-site *
00033  *                http://www.cis.ksu.edu/santos                      *
00034  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00035 import ca.mcgill.sable.soot.SootMethod;
00036 import ca.mcgill.sable.soot.SootField;
00037 
00038 import java.util.*;
00039 
00040 /**
00041  * A visitor that prints JimpleLiterals neatly (using the notation #i
00042  * to print references to objects).
00043  * <p>
00044  * Demonstrates the use of JimpleLiteralSwitch to visit different
00045  * types of literals.
00046  */
00047 
00048 public class JimpleLiteralPrinter implements JimpleLiteralSwitch {
00049     
00050     Hashtable objectTable =   // Map (ClassLit or ArrayLit) --> Integer
00051     new Hashtable();  
00052     int objectCount = 0;
00053     Vector printQueue = new Vector();
00054     String prefix;
00055 
00056     public JimpleLiteralPrinter(String prefix) {
00057     this.prefix = prefix;
00058     }
00059     public void caseArrayLiteral(ArrayLiteral literal) { 
00060     System.out.print("[ ");
00061     for (int i = 0; i < literal.length; i++) {
00062         if (i > 0)
00063         System.out.print(", ");
00064         literal.contents[i].apply(this);
00065     }
00066     System.out.print(" ]");
00067     }
00068     public void caseBooleanLiteral(BooleanLiteral literal) { 
00069     System.out.print("" + literal.value);
00070     }
00071     public void caseClassLiteral(ClassLiteral literal) { 
00072     Vector fields = literal.getFields();
00073     String className = literal.getSource().getName();
00074     System.out.print(className + " { ");
00075     for (int i = 0; i < fields.size(); i++) {
00076         SootField field = (SootField) fields.elementAt(i);
00077         if (i > 0)
00078         System.out.print(", ");
00079         System.out.print(field.getName() + " = ");
00080         literal.getFieldValue(field).apply(this);
00081     }
00082     System.out.print(" } ");
00083     LockLiteral lock = literal.getLockValue();
00084     if (lock != null) 
00085         lock.apply(this);
00086     }
00087     public void caseIntegerLiteral(IntegerLiteral literal) { 
00088     System.out.print("" + literal.value);
00089     }
00090     public void caseLockLiteral(LockLiteral literal) { 
00091     SootMethod method = literal.getHoldingThread();
00092     String holding = "-";
00093     if (method != null)
00094         holding = method.getDeclaringClass().getName();
00095     String waiting = ";";
00096     for (int i = 0; i < literal.getNumWaiting(); i++) {
00097         if (i > 0)
00098         waiting += ",";
00099         waiting += 
00100         literal.getWaitingThread(i).getDeclaringClass().getName();
00101     }       
00102     System.out.print("<" + holding + waiting + ">");
00103     }
00104     public void caseReferenceLiteral(ReferenceLiteral literal) { 
00105     if (literal.value == null)
00106         System.out.print("null");
00107     else {
00108         Integer objectNum = (Integer) objectTable.get(literal.value);
00109         if (objectNum == null) {
00110         objectNum = new Integer(++objectCount);
00111         objectTable.put(literal.value,objectNum);
00112         printQueue.addElement(literal.value);
00113         }
00114         // System.out.print("#" + objectNum.intValue());
00115         int objectID = ((ObjectLiteral)literal.value).getId();
00116         System.out.print("#" + objectID);
00117     }
00118     }
00119     /**
00120      * Flush output
00121      * <p>
00122      * This method should be called after all static fields and locals
00123      * have been printed using the object---it will print any objects
00124      * that these variables were pointing to (as well as any objects
00125      * those objects point to, etc.).
00126      */
00127 
00128     public void flush() {
00129     while (printQueue.size() > 0) {
00130         ObjectLiteral lit = (ObjectLiteral) printQueue.elementAt(0);
00131         printQueue.removeElementAt(0);
00132         System.out.print(prefix + "#" + lit.getId() + " = ");
00133         lit.apply(this);
00134         System.out.println();
00135     }
00136     }
00137 }

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