00001 package edu.ksu.cis.bandera.specification.assertion;
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 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
00059
00060 private AssertionExtractor(Vector expressions) {
00061 this.expressions = expressions.elements();
00062 }
00063
00064
00065
00066
00067 public void caseALocationAssertion(ALocationAssertion node) {
00068 node.setExp((PExp) expressions.nextElement());
00069 }
00070
00071
00072
00073
00074 public void caseAPostAssertion(APostAssertion node) {
00075 node.setExp((PExp) expressions.nextElement());
00076 }
00077
00078
00079
00080
00081 public void caseAPreAssertion(APreAssertion node) {
00082 node.setExp((PExp) expressions.nextElement());
00083 }
00084
00085
00086
00087
00088
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
00100 System.out.println("Error extracting assertion from: " + tag);
00101 }
00102 }
00103 }
00104 }
00105
00106
00107
00108
00109 public static void main(String[] args) throws Exception {
00110 removeComment("sdsjdsk // dksj \n dfkofe // ded \n");
00111 }
00112
00113
00114
00115
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
00156
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
00164
00165
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
00181
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 }