/*
* Created on May 12, 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.signatureAnalyser;
import java.util.Hashtable;
//import java.util.Iterator;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.bcel.classfile.Field;
//import org.apache.bcel.classfile.Field;
//import org.apache.bcel.generic.ConstantPoolGen;
import fr.lifl.stc.stan.link.TableLink;
import fr.lifl.stc.stan.policy.ClassPolicy;
import fr.lifl.stc.stan.policy.Policy;
import fr.lifl.stc.stan.samples.flow.FlowPartialSignature;
import fr.lifl.stc.stan.samples.flow.FlowPartialSignatureData;
import fr.lifl.stc.stan.signature.Signature;
import fr.lifl.stc.stan.signature.SignatureDictionary;
/**
* @author dorina
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClassSignatureAnalyser {
// Contains signatures
private SignatureDictionary dictionary;
private Policy policy;
private JavaClass _class;
private JavaClass[] classes;
/**
* Creates a new ClassAnalyser
instance.
*
*/
public ClassSignatureAnalyser(SignatureDictionary dict, Policy pol, JavaClass c, JavaClass[] cl)
{
dictionary = dict;
policy = pol;
_class = c;
classes = cl;
}
private JavaClass findJavaClass(String name) {
for(int i = 0 ; i < classes.length; i++) {
if( classes[i].getClassName().equals(name))
return classes[i];
}
return null;
}
public void analyse() {
//test secret attributes if they are private!
analyseAttributes();
//test methods
analyseMethods();
}
public void analyseAttributes(){
boolean error = false;
Field[] attr = _class.getFields();
String className = _class.getClassName();
for(int i = 0; i < attr.length; i++) {
if(!attr[i].isPrivate() && (policy.findAttribute(className, attr[i].getName()) == Policy.SECRET_ATTRIBUTE)){
if(attr[i].isProtected()){
System.out.println("[WARNING] the attribute '"+attr[i].getName()+"' has 'protected' acces flag");
}
else
if(attr[i].isPublic()) {
error = true;
System.err.println("[ERROR] the attribute '"+attr[i].getName()+"' has 'public' acces flag");
}
else
System.err.println("[WARNING] the attribute '"+attr[i].getName()+"' has 'package' acces flag");
}
}
ClassPolicy cp = policy.getClassPolicy(className);
for(int j = 0 ; j < classes.length; j++){
if(cp.hasSuperClass(classes[j].getClassName())){
attr = classes[j].getFields();
className = classes[j].getClassName();
for(int i = 0; i < attr.length; i++) {
error = testAttribute(className, attr[i]) | error;
}
}
}
if(!error){
System.out.println("No secret accesible directly (all secrets are declared private)");
}
}
private boolean testAttribute(String className, Field attr) {
boolean error = false;
if(!attr.isPrivate() && (policy.findAttribute(className, attr.getName()) == Policy.SECRET_ATTRIBUTE)){
if(attr.isProtected()){
System.out.println("[WARNING] the attribute '"+attr.getName()+"' has 'protected' acces flag in class "+className);
}
else
if(attr.isPublic()) {
error = true;
System.err.println("[ERROR] the attribute '"+attr.getName()+"' has 'public' acces flag in class "+className);
}
else
System.err.println("[WARNING] the attribute '"+attr.getName()+"' has 'package' acces flag in class "+className);
}
return error;
}
public void analyseMethods(){
//test methods
Method[] m;
JavaClass jc = null;
String className = _class.getClassName();
String key;
MethodSignatureAnalyser ma;
Hashtable analyzedMethods = new Hashtable();
Hashtable analyzedClasses = new Hashtable();
while(className != null && !analyzedClasses.containsKey(className)) {
jc = findJavaClass(className);
if(jc == null) {
System.out.println("JavaClass for class "+className+" not found");
className = null;
continue;
}
analyzedClasses.put(className, "");
System.out.println("-->Analyzing methods of class "+className);
m = jc.getMethods();
//FIXME get methods from superclass
for (int i = 0; i < m.length; i++)
{
if(!analyzedMethods.containsKey(m[i].getName()+"."+m[i].getSignature())){
analyzedMethods.put(m[i].getName()+"."+m[i].getSignature(), "");
key = SignatureDictionary.key(className, m[i].getName(), m[i].getSignature());
if(m[i].isStatic())
{
//System.out.println("static method "+key);
continue;
}
if (dictionary.contains(key)) {
Signature sign = dictionary.findSignature(key, false);
//PartialSignature
FlowPartialSignature ps = (FlowPartialSignature)sign.getPartialSignature("fr.lifl.rd2p.jits.tools.ea.samples.flow.FlowAnnotation");
TableLink data = ((FlowPartialSignatureData)(ps.getComponent())).getData();
//System.out.println("-----> Analyzing "+key);
ma = new MethodSignatureAnalyser(m[i], className, data);
ma.analyse();
//compare(m[j].getSignature(), data.getTableLinks(), className+"."+m[j].getMethodName());
}
else {
System.err.println("Signature "+key+" not found in the dictionary");
}
}
}
className = _class.getSuperclassName();
}
}
}