/* * Created on May 23, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package fr.lifl.stc.stan.tools; //import fr.lifl.rd2p.jits.tools.ea.link.defaultLink.DefaultLink; import fr.lifl.stc.stan.link.TableLink; import fr.lifl.stc.stan.samples.flow.FlowAnnotation; import fr.lifl.stc.stan.samples.flow.FlowPartialSignature; import fr.lifl.stc.stan.signature.PartialSignature; import fr.lifl.stc.stan.signature.Signature; import fr.lifl.stc.stan.signature.SignatureDictionary; import fr.lifl.stc.stan.util.Printer; import gnu.getopt.Getopt; import gnu.getopt.LongOpt; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.FileNotFoundException; import java.io.Serializable; import java.util.Collection; import java.util.Hashtable; import java.util.Iterator; /** * @author dorina * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class NativesAnnoter { SignatureDictionary dict; SignatureDictionary target; String fileName; /** * String => Hashtable (String (name e.g flowAnnotation)=> String (value)) */ Hashtable annotations; JavaClass[] classes; public NativesAnnoter(SignatureDictionary dict, SignatureDictionary target, String fileName) { this.dict = dict; this.target = target; this.fileName = fileName; this.annotations = new Hashtable(); } public NativesAnnoter( SignatureDictionary target, String fileName, JavaClass[] classes) { //this.dict = dict; this.target = target; this.fileName = fileName; this.annotations = new Hashtable(); this.classes = classes; } public void generateDico() { readAnnotations(); extractNatives(); annote(); try { target.dump(); } catch(Exception e) { System.err.println(e); } } public void addToDico() { readAnnotations(); annote(); } /** * reads the hand annotation from the specified as argument * */ private void readAnnotations(){ try { BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); String methodName; methodName = bf.readLine(); while(methodName!=null){ Hashtable h = new Hashtable(); String annot; while( (annot = bf.readLine())!=null && annot.charAt(0)=='['){ String annotName = annot.substring(1, annot.indexOf(']')); String annotValue = annot.substring(annot.indexOf(']')+1); h.put(annotName, annotValue); } this.annotations.put(methodName, h); methodName = annot; } bf.close(); } catch(FileNotFoundException e) { System.err.println(e); System.exit(-1); } catch(IOException e) { System.err.println(e); //System.exit(-1); } } // private void readAnnotationsAsMatrix(){ // try { // BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); // String methodName; // String line; // while((methodName = bf.readLine())!=null){ // //methodSignature = bf.readLine(); // line = bf.readLine(); // int dim = Integer.parseInt(line); // //System.out.println("dim = "+dim); // TableLink deps = new TableLink(dim, dim); // for(int i = 0 ; i < deps.getSizeL(); i++) { // line = bf.readLine(); // StringTokenizer tokenizer = new StringTokenizer (line, " "); // int j = 0; // while (tokenizer.hasMoreTokens() && j < deps.getSizeC()) { // String token = tokenizer.nextToken(); // try { // byte b = (byte)Integer.parseInt (token); // if(b!=0) // deps.set(i, j, GlobalAnalyser.linkFactory.createLink(b)); // //deps[i][j] = new DefaultLink(); // //GlobalAnalyser.linkFactory.createLink((byte)Integer.parseInt (token)); // // // j++; // } // catch (Exception exception) { // System.out.println(exception); // } // } // } // //MethodSign ms = new MethodSign(methodName, methodSignature, deps); // this.annotations.put(methodName, deps); // //ms.print(); // } // bf.close(); // } // catch(FileNotFoundException e) { // System.err.println(e); // System.exit(-1); // } // catch(IOException e) { // System.err.println(e); // //System.exit(-1); // } // // } // private void printAnnotationsFromMatrix() { // Iterator i = this.annotations.keySet().iterator(); // while(i.hasNext()) { // String meth = i.next(); // TableLink value = (TableLink)this.annotations.get(meth); // System.out.println(meth); // System.out.print("[FlowAnnotation] "); // for(int j = 0; j < value.getSizeL(); j++) // for(int k = 0; k < value.getSizeC(); k++) // { // Link l = value.get(j,k); // if(l!=null && !l.isEmpty()) { // System.out.print(j+">>"+k+":"+l+","); // // } // } // System.out.println(""); // // } // } private void extractNatives() { Iterator i = this.annotations.keySet().iterator(); while(i.hasNext()) { String meth = i.next(); Signature sign; sign = dict.findSignature(meth,true); if (sign == null) { System.err.println("Error: no such method \"" + meth + "\"in dictionary"); System.exit(1); } target.putSignatureForcively(meth,sign, true); } } private void annote() { Iterator i = this.annotations.keySet().iterator(); while(i.hasNext()) { String meth = i.next(); Hashtable values = (Hashtable)this.annotations.get(meth); Signature sign = target.findSignature(meth,true); if (sign == null) { //String className = meth.substring(0, meth.lastIndexOf('.')); //String methodName = meth.substring(meth.lastIndexOf('.')+1); // Printer.println(meth); Method m = null; for(int j = 0 ; j < classes.length; j++) { for(int k = 0 ; k < classes[j].getMethods().length; k++) { //Printer.println("compare to:"+SignatureDictionary.key(classes[j], classes[j].getMethods()[k])); if(meth.equals(SignatureDictionary.key(classes[j], classes[j].getMethods()[k]))) { m = classes[j].getMethods()[k]; break; } } } if(m==null){ Printer.println("[warning] No such method \"" + meth + "\"in dictionary"); } else{ int n = m.getArgumentTypes().length; int dec; if (m.isStatic()) dec = 4; else dec = 5; TableLink b = new TableLink(n + dec, n + dec); FlowPartialSignature lps = new FlowPartialSignature( FlowAnnotation.class.getName(), b); sign = new Signature(); sign.add(lps); Collection s = sign.getPartialSignatures(); Iterator it = s.iterator(); while (it.hasNext()) { PartialSignature ps = it.next(); Iterator newSign = values.keySet().iterator(); while(newSign.hasNext()) { String annot = newSign.next(); if(ps.getClassId().indexOf(annot) != -1) { ps.getComponent().setData(values.get(annot)); break; } } } target.putSignature(meth, sign, true); } } } } // private void old_annote() { // Iterator i = this.annotations.keySet().iterator(); // while(i.hasNext()) { // String meth = i.next(); // Hashtable values = (Hashtable)this.annotations.get(meth); // // // Signature sign = target.findSignature(meth,true); // if (sign == null) { // Printer.println("[warning] No such method \"" + meth + "\"in dictionary"); // //System.exit(1); // } // else // if(sign.isLocked()) { // Printer.println("[warning] method \"" + meth + "\" locked in dictionary"); // } // else { // // Collection s = sign.getPartialSignatures(); // Iterator it = s.iterator(); // while (it.hasNext()) { // PartialSignature ps = (PartialSignature)it.next(); // Iterator newSign = values.keySet().iterator(); // while(newSign.hasNext()) { // String annot = (String)newSign.next(); // // if(ps.getClassId().indexOf(annot) != -1) { // //System.out.println("put "+meth); // ps.getComponent().setData((String)values.get(annot)); // break; // } // } // } // } // } // // } private static void printHelp() { System.out.println("-h/--help : print this help"); System.out.println("-d/--dictionary : use as input dictionary"); System.out.println("-t/--target : use as target dictionary"); System.out.println("-f/--file : use as input file for annotations"); } public static void main(String[] args) { int c; SignatureDictionary dict = null; SignatureDictionary targetDict = null; String fileName = null; LongOpt[] longopts = new LongOpt[4]; longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); longopts[1] = new LongOpt("dictionary", LongOpt.REQUIRED_ARGUMENT, null, 'd'); longopts[2] = new LongOpt("target", LongOpt.REQUIRED_ARGUMENT, null, 't'); longopts[3] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'); Getopt g = new Getopt("Displayer", args, "-:hd:t:f:W;", longopts); try { while ((c = g.getopt()) != -1) switch (c) { case 'h': printHelp(); System.exit(0); break; case 'd': String dictName = g.getOptarg(); //System.out.println("dict name = "+dictName); dict = new SignatureDictionary(dictName); break; case 't': String dictName1= g.getOptarg(); //System.out.println("target name = "+dictName1); targetDict = new SignatureDictionary(dictName1); break; case 'f': fileName = g.getOptarg(); //System.out.println("file name = "+fileName); break; } if (dict == null) { System.err.println("You must provide a dictionary"); printHelp(); System.exit(1); } if (targetDict == null) { System.err.println("You must provide a target dictionary"); printHelp(); System.exit(1); } if (fileName == null) { System.err.println("You must provide a file name"); printHelp(); System.exit(1); } NativesAnnoter na = new NativesAnnoter(dict, targetDict, fileName); na.generateDico(); } catch (Exception e) { System.err.println("Error: Dictionary contains unknown classes"); System.exit(1); } /*catch (FileNotFoundException e) { System.err.println("Error: Dictionary file not found"); System.exit(1); } catch (IOException e) { System.err.println("Error: IO error"); System.exit(1); }*/ } }