/* * Copyright 2009 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2ext.Instrumenter; import java.util.*; import java.io.* ; /** * Just a class to hold a bunch of CFGs, so that we can save them together. * * @author Wishnu Prasetya * */ public class CFGBunchFile implements Serializable { private static final long serialVersionUID = 1L; public List cfgs = new LinkedList() ; public void save(String fname){ try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fname)); oos.writeObject(this); oos.close(); } catch (Exception e) { throw new Error(e); } } static public CFGBunchFile load(String fname){ //System.out.println(">>>" + fname) ; CFGBunchFile result ; try { File file = new File(fname) ; //System.out.println(">>>" + file.exists()) ; ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fname)); result = (CFGBunchFile) ois.readObject(); ois.close(); } catch (Exception e) { //e.printStackTrace(); throw new Error(e) ; } return result ; } static private boolean isMethodNamePrefix(String pref, String mname) { int k = mname.lastIndexOf("#") ; return mname.startsWith(pref,k+1) ; } /** * Use this to remove all CFGs of the methods whose unique names match the * given prefix. */ public void removeCFG(String methodprefix) { List tobeRemoved = new LinkedList() ; for (CFG cfg : cfgs) { if (isMethodNamePrefix(methodprefix,cfg.methodName)) tobeRemoved.add(cfg) ; } cfgs.removeAll(tobeRemoved) ; } static private boolean pathMatch(LinkedList path, List p) { if (path.size() != p.size()) return false ; int i = 0 ; for (Node N : path) { if (N.id != p.get(i)) return false ; i++ ; } return true ; } /** * Use this to remove a target path from ALL CFGs of the methods whose * unique names match the given prefix. */ public void removeTargetPath(String mnamePrefix, String path) { List p = Sequenic.Graph.PathUtil.readlist(path) ; for (CFG cfg : cfgs) { if (isMethodNamePrefix(mnamePrefix,cfg.methodName)) { for (LinkedList path_ : cfg.targetPaths) { if (pathMatch(path_, p )) { cfg.targetPaths.remove(path_) ; break ; } } } } } /** * Use this to mark a target path from ALL CFGs of the methods whose * unique names match the given prefix. The target path will be marked * as unfeasible to be directly covered. */ public void markTargetPathDirectlyUnfeasible(String mnamePrefix, String path) { List p = Sequenic.Graph.PathUtil.readlist(path) ; for (CFG cfg : cfgs) { if (isMethodNamePrefix(mnamePrefix,cfg.methodName)) { for (LinkedList path_ : cfg.targetPaths) { if (pathMatch(path_, p )) { if (! cfg.directlyUnfeasibleTargetPaths.contains(path)) cfg.directlyUnfeasibleTargetPaths.add(path_) ; break ; } } } } } static private LinkedList constructPath(CFG cfg, List p) { LinkedList pp = new LinkedList() ; for (Integer i : p) { boolean found = false ; for (Node N : cfg.getNodes()) { if (N.id == i) { pp.add(N) ; found = true ; break ; } } if (!found) return null ; } return pp ; } /** * This will add the given target path to all CFGs of the methods whose * unique names match the given prefix. */ public void addTargetPath(String mnamePrefix, String path) { List p = Sequenic.Graph.PathUtil.readlist(path) ; LinkedList pp ; for (CFG cfg : cfgs) { if (isMethodNamePrefix(mnamePrefix,cfg.methodName)) { // check first if p is already in the cfg: for (LinkedList path_ : cfg.targetPaths) { if (pathMatch(path_, p )) return ; } pp = constructPath(cfg,p) ; if (pp != null) cfg.targetPaths.add(pp) ; } } } static public void main(String[] args) { System.out.println(isMethodNamePrefix("woo","aap.bies.moes.woo_int_int")) ; } }