package fr.lifl.stc.stan.tools.signatureAnalyser;
import java.io.IOException;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import fr.lifl.stc.stan.link.LinkFactory;
import fr.lifl.stc.stan.link.defaultLink.DefaultLinkFactory;
import fr.lifl.stc.stan.policy.Policy;
import fr.lifl.stc.stan.policy.defaultPolicy.DefaultPolicy;
import fr.lifl.stc.stan.signature.SignatureDictionary;
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
/**
* @author Dorina Ghindici
*/
public class SignatureAnalyser
{
//private static SignatureDictionary dict = null;
private SignatureDictionary dictionary;
private Policy policy;
private JavaClass classes[];
public static LinkFactory linkFactory = new DefaultLinkFactory();
/**
* Creates a new GlobalAnalyser
instance.
*
* @param sign_filename
* name of the file which will contains the methods signature
* @param classes
* set of classfiles to analyse
* @throws ClassNotFoundException
* @throws IOException
* Attention cree un nouveau dictionnaire a chaque execution
*/
public SignatureAnalyser(JavaClass[] classes, Policy pol, SignatureDictionary dict)
throws IOException, ClassNotFoundException
{
//dictionary = new SignatureDictionary("/tmp/dico/dicoJITS.txt"); // TODO: fix this dictionary name
//dictionary = new SignatureDictionary("/tmp/dicoflow.txt"); // TODO: fix this dictionary name
this.dictionary = dict;
if(pol != null)
this.policy = pol;
else
this.policy = new DefaultPolicy();
//System.out.println(policy);
for(int i = 0; i < classes.length; i++) {
policy.registerClass(classes[i]);
}
this.classes = classes;
}
/**
* @see fr.lifl.stc.stan.analyser.Analyser#analyse()
* Analyse le groupe de classes par passes successives tant qu'un changement apparait dans le dictionnaire
**/
public void analyse() throws IOException
{
ClassSignatureAnalyser canalyser;
for (int i = 0; i < classes.length; i++)
{
System.out.println("\n--------+++++++++++++ANALYSING CLASS " + classes[i].getFileName()+"+++++++++++--------");
//HTMLOutput.openHTMLOutput(classes[i].getClassName());
canalyser = new ClassSignatureAnalyser(dictionary, policy, classes[i], classes);
canalyser.analyse();
}
}
private static void printHelp() {
System.out.println("Usage : if_inspectDico ");
System.out.println("where possible options include: ");
System.out.println(" -h/--help : print this help");
System.out.println(" -d/--dictionary : use as dictionary");
System.out.println(" -p/--policy : use file as policy");
System.out.println(" -x/--directory : analyze all .class files in and its subdirectories");
}
/* Charge les classes de la ligne d'arguments depuis bcel et lance l'analyser */
public static void main(String args[])
{
int i = 0, error = 0;
int c;
LongOpt[] longopts = new LongOpt[3];
longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
longopts[1] = new LongOpt("dictionary", LongOpt.REQUIRED_ARGUMENT, null, 'd');
longopts[2] = new LongOpt("policy", LongOpt.REQUIRED_ARGUMENT, null, 'p');
Getopt g = new Getopt("SignatureAnalyser", args, "hd:p:W;", longopts);
//Getopt g = new Getopt("GlobalAnalyzer", args, "hvp:d:i:P:ar:so:tb:c:wvW;", longopts);
Policy policy = null;
SignatureDictionary dict = null;
try {
while ((c = g.getopt()) != -1)
switch (c) {
case 'h':
printHelp();
System.exit(0);
break;
case 'd':
dict = new SignatureDictionary(g.getOptarg());
break;
case 'p':
policy = new DefaultPolicy(g.getOptarg());
break;
case 'W':
System.out.println("You tried a -W with an incorrect long option name");
break;
case ':':
System.out.println("You need an argument for option " + (char) g.getOptopt());
break;
case '?':
System.out.println("The option '" + (char) g.getOptopt() + "' is not valid");
break;
default:
System.out.println("getopt(): unknown option " + c);
break;
}
if (dict == null) {
System.err.println("You must provide a dictionary");
printHelp();
System.exit(1);
}
SignatureAnalyser ganalyser;
JavaClass classes[];
int from = g.getOptind();
classes = new JavaClass[args.length - from];
for (i = 0; i < args.length - from; i++)
classes[i] = (new ClassParser(args[i + from])).parse();
ganalyser = new SignatureAnalyser(classes, policy, dict);
ganalyser.analyse();
}
catch (IOException e)
{
e.printStackTrace();
System.err.println("E: File " + args[i]
+ " does not exist or you don't have permission on it.");
error = 1;
} catch (ClassNotFoundException e)
{
System.err.println("E: Internal error");
e.printStackTrace();
error = 1;
}
catch (OutOfMemoryError er)
{
System.err.println("EXCEPTION:: "+er);
er.printStackTrace();
error = 1;
}
System.exit(error);
}
}