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

AssertionExtractor.java

00001 package edu.ksu.cis.bandera.specification.assertion;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 2000   Robby (robby@cis.ksu.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 edu.ksu.cis.bandera.specification.assertion.datastructure.*;
00036 import edu.ksu.cis.bandera.specification.assertion.ast.*;
00037 import edu.ksu.cis.bandera.jjjc.node.AAssertionCompilationUnit;
00038 import edu.ksu.cis.bandera.jjjc.node.PExp;
00039 import ca.mcgill.sable.laleh.java.astfix.*;
00040 import edu.ksu.cis.bandera.jjjc.*;
00041 import edu.ksu.cis.bandera.jjjc.symboltable.*;
00042 import edu.ksu.cis.bandera.jjjc.symboltable.Package;
00043 import java.io.*;
00044 import java.util.*;
00045 import edu.ksu.cis.bandera.jjjc.doc.*;
00046 import edu.ksu.cis.bandera.specification.assertion.parser.*;
00047 import edu.ksu.cis.bandera.specification.assertion.lexer.*;
00048 import edu.ksu.cis.bandera.specification.assertion.node.*;
00049 import edu.ksu.cis.bandera.specification.assertion.analysis.*;
00050 import edu.ksu.cis.bandera.annotation.*;
00051 import ca.mcgill.sable.soot.*;
00052 public class AssertionExtractor extends DepthFirstAdapter {
00053     private static AnnotationManager am;
00054     private static SootClassManager scm;
00055     private Enumeration expressions;
00056 /**
00057  * 
00058  * @param expressions java.util.Vector
00059  */
00060 private AssertionExtractor(Vector expressions) {
00061     this.expressions = expressions.elements();
00062 }
00063 /**
00064  * 
00065  * @param node edu.ksu.cis.bandera.specification.assertion.node.ALocationAssertion
00066  */
00067 public void caseALocationAssertion(ALocationAssertion node) {
00068     node.setExp((PExp) expressions.nextElement());
00069 }
00070 /**
00071  * 
00072  * @param node edu.ksu.cis.bandera.specification.assertion.node.APostAssertion
00073  */
00074 public void caseAPostAssertion(APostAssertion node) {
00075     node.setExp((PExp) expressions.nextElement());
00076 }
00077 /**
00078  * 
00079  * @param node edu.ksu.cis.bandera.specification.assertion.node.APreAssertion
00080  */
00081 public void caseAPreAssertion(APreAssertion node) {
00082     node.setExp((PExp) expressions.nextElement());
00083 }
00084 /**
00085  * 
00086  * @param sc ca.mcgill.sable.soot.SootClass
00087  * @param sm ca.mcgill.sable.soot.SootMethod
00088  * @param tags java.util.Vector
00089  */
00090 public static void extract(SootClass sc, SootMethod sm, Vector tags) {
00091     scm = CompilationManager.getSootClassManager();
00092     am = CompilationManager.getAnnotationManager();
00093     for (Iterator i = tags.iterator(); i.hasNext();) {
00094         String tag = (String) i.next();
00095         if ("@assert".equals(new StringTokenizer(tag).nextToken())) {
00096             try {
00097                 process(parse(tag.substring(7)), sc, sm);
00098             } catch (Exception e) {
00099                 //e.printStackTrace();
00100                 System.out.println("Error extracting assertion from: " + tag);
00101             }
00102         }
00103     }
00104 }
00105 /**
00106  * 
00107  * @param args java.lang.String[]
00108  */
00109 public static void main(String[] args) throws Exception {
00110     removeComment("sdsjdsk // dksj \n dfkofe // ded \n");
00111 }
00112 /**
00113  * 
00114  * @return edu.ksu.cis.bandera.specification.assertion.node.Node
00115  * @param text java.lang.String
00116  */
00117 private static Node parse(String text) throws Exception {
00118     Vector expressions = new Vector();
00119     StringBuffer buffer = new StringBuffer();
00120 
00121     String textNoComment = removeComment(text);
00122 
00123     Vector v = new Vector();
00124     for (StringTokenizer t = new StringTokenizer(textNoComment, ":;", true); t.hasMoreTokens();) {
00125         String token = t.nextToken();
00126         if (":".equals(token)) {
00127             token = t.nextToken();
00128             String token2 = t.nextToken();
00129             while (!";".equals(token2)) {
00130                 token += token2;
00131                 token2 = t.nextToken();
00132             }
00133             v.add(token);
00134         }   
00135     }
00136 
00137     int lastIndex = 0;
00138     for (Enumeration e = v.elements(); e.hasMoreElements();) {
00139         String expText = (String) e.nextElement();
00140         expressions.add(parseExp(expText));
00141         int index = textNoComment.indexOf(expText, lastIndex);
00142         buffer.append(text.substring(lastIndex, index));
00143         lastIndex = index + expText.length();
00144     }
00145     buffer.append(text.substring(lastIndex));
00146 
00147     Node node = new Parser(new Lexer(new PushbackReader(new StringReader(buffer.toString())))).parse();
00148 
00149     node.apply(new AssertionExtractor(expressions));
00150     
00151     return node;
00152 }
00153 /**
00154  * 
00155  * @return edu.ksu.cis.bandera.bpdl.node.Node
00156  * @param text java.lang.String
00157  */
00158 private static edu.ksu.cis.bandera.jjjc.node.Node parseExp(String text) throws Exception {
00159     return ((AAssertionCompilationUnit) new JJCParser(new edu.ksu.cis.bandera.jjjc.lexer.Lexer(new PushbackReader(new StringReader(text)))).parse().getPCompilationUnit()).getExp();
00160 }
00161 /**
00162  *
00163  * @param node edu.ksu.cis.bandera.bpdl.node.Node
00164  * @param sc ca.mcgill.sable.soot.SootClass
00165  * @param sm ca.mcgill.sable.soot.SootMethod
00166  */
00167 private static void process(Node node, SootClass sc, SootMethod sm) {
00168     ClassOrInterfaceType classOrInterfaceType = null;
00169     SymbolTable symbolTable = null;
00170     try {
00171         classOrInterfaceType = Package.getClassOrInterfaceType(new Name(sc.getName()));
00172         symbolTable = classOrInterfaceType.getSymbolTable();
00173     } catch (Exception e) {
00174     }
00175     AssertionBuilder assertionBuilder = new AssertionBuilder(node, symbolTable, classOrInterfaceType, am.getAnnotation(sc, sm));
00176     assertionBuilder.build();
00177 }
00178 /**
00179  * 
00180  * @return java.lang.String
00181  * @param text java.lang.String
00182  */
00183 private static String removeComment(String text) {
00184     StringBuffer buffer = new StringBuffer();
00185     int lastIndex = 0;
00186     do {
00187         int index = text.indexOf("//", lastIndex);
00188         index = index < 0 ? text.length() : index;
00189         buffer.append(text.substring(lastIndex, index));
00190         lastIndex = text.indexOf("\n", index);
00191         for (int i = index; i < lastIndex; i++) {
00192             buffer.append(" ");
00193         }
00194     } while (lastIndex >= 0);
00195     return buffer.toString();
00196 }
00197 }

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