/* * Created on Oct 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; import java.io.IOException; import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.ConstantPool; import org.apache.bcel.classfile.Constant; import org.apache.bcel.classfile.ConstantMethodref; import org.apache.bcel.classfile.ConstantInterfaceMethodref; import org.apache.bcel.classfile.ConstantCP; import org.apache.bcel.classfile.ConstantNameAndType; /** * @author dorina * * Analyzes the constant pool of the classes passed as command line arguments, and outputs statistics * on the number of methods */ public class ConstantPoolAnalyser { JavaClass[] classes; ConstantPoolAnalyser(JavaClass classes[]){ this.classes = classes; } void analyse() { JavaClass currentClass; int n =0; int total = 0; int noMethods = 0; for(int i = 0; i < classes.length; i++) { currentClass = classes[i]; System.out.println("\n Analysing class "+currentClass.getClassName()); n = analyseConstantPool(currentClass.getConstantPool()); total += n; noMethods += currentClass.getMethods().length; System.out.println("--class methods = "+currentClass.getMethods().length+" \n--external methods = "+ n); } System.out.println("\n no classes = "+classes.length+" no methods = "+noMethods+" external methods = "+total); } int analyseConstantPool(ConstantPool cp) { Constant[] cst = cp.getConstantPool(); int noMeth = 0; for(int i = 0; i < cst.length; i++) { if(cst[i] instanceof ConstantMethodref || cst[i] instanceof ConstantInterfaceMethodref){ ConstantCP c = (ConstantCP)cst[i]; System.out.println(c.toString()); ConstantNameAndType n = (ConstantNameAndType)cp.getConstant(c.getNameAndTypeIndex()); System.out.println(n.getName(cp)); noMeth++; } /* else if (cst[i] instanceof ConstantInterfaceMethodref) { ConstantCP c = (ConstantCP)cst[i]; System.out.println("ConstantInterfaceMethodref "+c.toString()); } */ /* if(cst[i] instanceof ConstantNameAndType) { ConstantNameAndType c = (ConstantNameAndType)cst[i]; System.out.println(c+" "+c.getName(cp)); } */ } return noMeth; } public static void main(String[] args) { JavaClass classes[]; System.out.println("PROGRAM ARGUMENTS: "+args.length); int i=0; ConstantPoolAnalyser cpanalyser; try { classes = new JavaClass[args.length]; for (i = 0; i < args.length; i++) classes[i] = (new ClassParser(args[i])).parse(); cpanalyser = new ConstantPoolAnalyser(classes); cpanalyser.analyse(); } catch (IOException e) { e.printStackTrace(); System.err.println("E: File " + args[i] + " does not exist or you don't have permission on it."); } catch (OutOfMemoryError er) { System.err.println("EXCEPTION:: "+er); er.printStackTrace(); } } }