package fr.lifl.stc.stan.tools; import java.io.IOException; import org.apache.bcel.classfile.Attribute; import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.EscapeAttribute; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; import org.apache.bcel.generic.ConstantPoolGen; import fr.lifl.stc.stan.samples.escape.EscapeAnnotation; import fr.lifl.stc.stan.samples.escape.EscapePartialSignature; import fr.lifl.stc.stan.samples.escape.EscapePartialSignatureData; import fr.lifl.stc.stan.signature.Signature; import fr.lifl.stc.stan.signature.SignatureDictionary; import gnu.getopt.Getopt; import gnu.getopt.LongOpt; /** * @author yann */ public class EA_Annoter { private static String classfile = null; private static JavaClass cl = null; private static SignatureDictionary dict = null; private static ConstantPoolGen cpg = null; private static int escape_index; // private static int fresh_index; private static String output = null; private static boolean fresh = true; private static void prepareConstantPool() { cpg = new ConstantPoolGen(cl.getConstantPool()); escape_index = cpg.addUtf8("EscapeMap"); if(fresh) //fresh_index = cpg.addUtf8("FreshMap"); cl.setConstantPool(cpg.getFinalConstantPool()); } private static void annotateEscape(Method meth, Signature sign) { EscapePartialSignature esc = (EscapePartialSignature)sign.getPartialSignature(EscapeAnnotation.class.getName()); if (esc == null) { System.err.println("Unsigned escape method in " + cl.getClassName() + ": " + meth); return; } short[] offsets = ((EscapePartialSignatureData)esc.getComponent()).getData(); if (offsets == null) { System.err.println("No escape offsets in " + meth); return; } Attribute[] orig = meth.getCode().getAttributes(); if (offsets.length > 0) { EscapeAttribute ea = new EscapeAttribute(escape_index,2*offsets.length,offsets,cl.getConstantPool()); int j = 0; while(j < orig.length) { if (orig[j] instanceof EscapeAttribute) break; j++; } if(j < orig.length) { orig[j] = ea; meth.getCode().setAttributes(orig); } else { Attribute[] new_attr = new Attribute[orig.length + 1]; new_attr[0] = ea; System.arraycopy(orig,0,new_attr,1,orig.length); meth.getCode().setAttributes(new_attr); } } else { // maybe suppress the attribute int j = 0; while(j < orig.length) { if (orig[j] instanceof EscapeAttribute) { break; } j++; } if(j < orig.length) { Attribute[] new_attr = new Attribute[orig.length - 1]; if(j>0) System.arraycopy(orig,0,new_attr,0,j); if(j 0) { int l = 0; for(int i = 0; i< offsets.length; i++) { for(int j = 0; j < offsets[i].length; j++) l++; } FreshAttribute fr = new FreshAttribute(fresh_index,FreshAttribute.cellSize*l,offsets,cl.getConstantPool()); int j = 0; while(j < orig.length) { if (orig[j] instanceof FreshAttribute) break; j++; } if(j < orig.length) { orig[j] = fr; meth.getCode().setAttributes(orig); } else { Attribute[] new_attr = new Attribute[orig.length + 1]; new_attr[0] = fr; System.arraycopy(orig,0,new_attr,1,orig.length); meth.getCode().setAttributes(new_attr); } } else { // maybe suppress the attribute int j = 0; while(j < orig.length) { if (orig[j] instanceof FreshAttribute) { break; } j++; } if(j < orig.length) { Attribute[] new_attr = new Attribute[orig.length - 1]; if(j>0) System.arraycopy(orig,0,new_attr,0,j); if(j