Example usage for org.aspectj.apache.bcel.classfile JavaClass getConstantPool

List of usage examples for org.aspectj.apache.bcel.classfile JavaClass getConstantPool

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.classfile JavaClass getConstantPool.

Prototype

public ConstantPool getConstantPool() 

Source Link

Usage

From source file:br.jabuti.criteria.AllNodes.java

License:Open Source License

public static void main(String args[]) throws Exception {
    JavaClass java_class;
    java_class = new ClassParser(args[0]).parse(); // May throw IOException
    ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());

    ClassGen cg = new ClassGen(java_class);

    Method[] methods = java_class.getMethods();
    for (int i = 0; i < methods.length; i++) {
        System.out.println("\n\n--------------------------");
        System.out.println(methods[i].getName());
        System.out.println("--------------------------");
        MethodGen mg = new MethodGen(methods[i], java_class.getClassName(), cp);

        CFG g = new CFG(mg, cg);

        AllNodes an = new AllNodes(g);

        System.out.println("Number of Requirements: " + an.getNumberOfRequirements());

        System.out.println("Number of Possible Requirements: " + an.getNumberOfPossibleRequirements());

        Object[] reqs = an.getRequirements();
        System.out.println("Requirements: ");

        for (int j = 0; j < reqs.length; j++) {
            Requirement req = (Requirement) reqs[j];
            System.out.print(req);
            System.out.print(" active: ");
            if (an.isActive(req)) {
                System.out.print("true");
            } else {
                System.out.print("false");
            }/*w w w.  j  a v a2s  . c  o m*/

            System.out.print(" covered: ");
            if (an.isCovered(req)) {
                System.out.print("true");
            } else {
                System.out.print("false");
            }

            System.out.print(" feasible: ");
            if (an.isFeasible(req)) {
                System.out.print("true");
            } else {
                System.out.print("false");
            }
            System.out.println();
        }
        System.out.println();

        an.addPath(new String[] { "0", "4" }, "path 1");
        an.addPath(new String[] { "0", "48", "207" }, "path 3");
        an.addPath(new String[] { "0", "76", "207" }, "path 2");

        int[] cv = an.getCoverage();
        System.out.println();

        System.out.println("Number of Possible Covered Requirements: " + an.getNumberOfPossibleCovered());

        HashSet hs = an.getCoveredRequirements();
        if (hs.isEmpty())
            System.out.println("No covered requirement.");
        else
            System.out.println("There are covered requirements.");

        hs = an.getPossibleCoveredRequirements();
        if (hs.isEmpty())
            System.out.println("No possible covered requirement.");
        else
            System.out.println("There are possible covered requirements.");

        System.out.println("Covered: ");
        for (int j = 0; j < cv.length; j++) {
            System.out.print(cv[j] + "  ");
        }
        System.out.println();

        hs = an.getCoveredRequirements("path 1");
        System.out.println("Covered requirements by Path 1");
        Iterator it = hs.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }

        // Marcando todos os requisitos como inativos e reativando-os
        for (int j = 0; j < reqs.length; j++) {
            an.setInactive((Requirement) reqs[j]);
        }
        hs = an.getInactiveRequirements();
        for (int j = 0; j < reqs.length; j++) {
            an.setActive((Requirement) reqs[j]);
        }

        // Marcando todos os requisitos como infeasible e reativando-os
        for (int j = 0; j < reqs.length; j++) {
            an.setInfeasible((Requirement) reqs[j]);
        }
        hs = an.getInfeasibleRequirements();
        for (int j = 0; j < reqs.length; j++) {
            an.setFeasible((Requirement) reqs[j]);
        }

        // Obtendo os requisitos por meio de seus rtulos
        /* for (int j = 0; j < req.length; j++) {
            Object o = an.getRequirementByLabel( req[j].toString() );
         }
         */

        // Removendo um caso de teste
        an.removePath("path 2");

        // Removendo todos os casos de teste
        an.removeAllPaths();
    }
}

From source file:br.jabuti.graph.datastructure.ig.InstructionGraph.java

License:Open Source License

/** A driver for testing the class. Creates the graph and calls
 * {@link InstructionGraph#calcStack}. The arguments determine
 * to which methods to apply.//w w  w  .ja  v  a 2s .  com
 *
 * @param args[0] A file name. Can be a classfile, a jar file or a
 * zip file. If jar or zip, the second and third arguments does not apply.
 * In this case, the output is only the name of the classes and of the
 * methods in the class. The test is applyied in all the listed
 * methods. If this is a single class file name, the output will be
 * the complete graph (as presented by {InstructionNode#print}) for
 * each selected method.
 * @param args[1] The name of a method. The test is applyed to all
 * methods in the class that match this name.
 * @param args[2] The signature of a method. Used to select one
 * between several homonymous methods.
 */

public static void main(String args[]) throws Exception {
    boolean all = true;
    String filename = args[0];
    ZipFile jf = null;

    if (filename.endsWith(".jar")) {
        jf = new JarFile(filename);
    } else if (filename.endsWith(".zip")) {
        jf = new ZipFile(filename);
    }

    if (jf == null) {
        JavaClass java_class;

        java_class = new ClassParser(filename).parse(); // May throw IOException

        ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
        Method[] methods = java_class.getMethods();

        for (int i = 0; i < methods.length; i++) {
            if (args.length >= 2 && (!args[1].equals(methods[i].getName()))) {
                continue;
            }
            if (args.length >= 3 && (!args[2].equals(methods[i].getSignature()))) {
                continue;
            }
            System.out.println("--------------------------");
            System.out.println(methods[i].getName());
            System.out.println(methods[i].getSignature());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], java_class.getClassName(), cp);

            if (mg.getInstructionList() == null) {
                continue;
            }
            InstructionGraph g = new InstructionGraph(mg);

            // g.calcReqLocal();
            g.calcStack(all);
            g.print(System.out);
        }
        return;
    }

    Enumeration en = jf.entries();
    ZipEntry ze = null;

    while (en.hasMoreElements()) {
        ze = (ZipEntry) en.nextElement();
        if (!ze.getName().endsWith(".class")) {
            System.out.println("Not a class file: " + ze.getName());
            continue;
        }

        System.out.println("\n\n**************************");
        System.out.println(ze.getName());
        System.out.println("**************************");

        JavaClass java_class;

        java_class = new ClassParser(jf.getInputStream(ze), ze.getName()).parse(); // May throw IOException

        ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
        Method[] methods = java_class.getMethods();

        for (int i = 0; i < methods.length; i++) {
            System.out.println("Memory : " + Runtime.getRuntime().freeMemory());
            System.out.println("--------------------------");
            System.out.println(methods[i].getName());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], java_class.getClassName(), cp);

            if (mg.getInstructionList() == null) {
                continue;
            }
            InstructionGraph g = new InstructionGraph(mg);

            // g.calcReqLocal();
            g.calcStack(all);
            System.out.println("Memory : " + Runtime.getRuntime().freeMemory());
            g = null;
            System.out.println("Collecting garbage...");
            System.out.println();

        }
    }
}

From source file:br.jabuti.lookup.java.bytecode.ClassClosure.java

License:Open Source License

public String[] accessedClasses(JavaClass javaClass) {
    Vector interestedClasses = new Vector();
    ConstantPool cp = javaClass.getConstantPool();
    Constant[] ct = cp.getConstantPool();

    for (int i = 0; i < ct.length; i++) {
        if (ct[i] instanceof ConstantClass) {
            ConstantClass cc = (ConstantClass) ct[i];
            // System.out.println("accessed: " + cc);
            ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(cc.getNameIndex());
            String t = cutf.getBytes();

            if (t.charAt(0) != '[') {
                interestedClasses.add(toPoint(t));
            }/*ww w .j a va  2s.  c  o m*/
        }
    }
    return (String[]) interestedClasses.toArray(new String[0]);
}

From source file:br.jabuti.lookup.java.bytecode.RClassCode.java

License:Open Source License

/** Retorna uma lista de metodos chamados por um dado metodo
 * desta classe./* www  . jav a  2 s .co m*/
 * @param assinatura - a assinatura do metodo que se deseja analisar
 * @return a lista de metodos chamados pelo metodo passado como argumento.
 * Se o metodo solicitado nao for encontrado na classe, retorna null.
 */
public String[] getCalledMethods(String assinatura) {
    JavaClass jc = this.getTheClass();
    Method[] mv = jc.getMethods();
    Method m = null;
    String met = new String();
    int i;
    for (i = 0; i < mv.length; i++) {
        m = mv[i];

        met = jc.getClassName() + "." + mv[i].getName() + mv[i].getSignature();
        System.out.println("Metodo Aplicao = " + met);
        System.out.println("Metodo Parametro = " + assinatura);

        if (met.equals(assinatura))
            break;
    }
    if (i == mv.length)
        return null;
    ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool());
    MethodGen mg = new MethodGen(m, jc.getClassName(), cp);

    InstructionList il = mg.getInstructionList();
    InstructionHandle[] ih = il.getInstructionHandles();
    Vector v = new Vector();

    for (int x = 0; x < ih.length; x++) {
        Instruction ins = ih[x].getInstruction();
        if (ins instanceof InvokeInstruction) {
            InvokeInstruction invoke = (InvokeInstruction) ins;
            String s = invoke.getClassName(cp) + "." + invoke.getMethodName(cp) + invoke.getSignature(cp);

            //System.out.println("gettype = " + invoke.getClassType(cp));
            System.out.println("metodo retornado = " + s);
            v.add(s);
        }
    }
    return (String[]) v.toArray(new String[0]);
}

From source file:br.jabuti.metrics.klass.MetricAMZLOCM.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    double theValue = 0.0;
    RClass rc = prog.get(className);/*  w  w  w . j a va  2  s .  co  m*/
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    int cont = 0;
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    Method[] methods = theClazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(methods[i], theClazz.getClassName(), cp);
        double d = getLinesOfCodeMethod(mg);
        theValue += d;
        cont++;
    }
    if (cont == 0) {
        return -1.0;
    }
    return theValue / cont;
}

From source file:br.jabuti.metrics.klass.MetricAMZNMS.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    double theValue = 0.0;
    RClass rc = prog.get(className);//from w w  w  .j  ava 2s. c  om
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    int cont = 0;
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    Method[] methods = theClazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(methods[i], theClazz.getClassName(), cp);
        InstructionList instructions = mg.getInstructionList();
        Iterator<Instruction> instructionsIterator = instructions.iterator();
        while (instructionsIterator.hasNext()) {
            Instruction instruction = instructionsIterator.next();
            if (instruction instanceof InvokeInstruction) {
                theValue++;
            }
        }
        cont++;
    }
    if (cont == 0) {
        return -1.0;
    }
    return theValue / cont;
}

From source file:br.jabuti.metrics.klass.MetricAMZSIZE.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    double theValue = 0.0;
    RClass rc = prog.get(className);/*ww w .  jav a 2s. c om*/
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    int cont = 0;
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    Method[] methods = theClazz.getMethods();
    for (Method method : methods) {
        if (method.isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(method, theClazz.getClassName(), cp);
        double d = getNumberOfBytecodeInstructions(mg);
        theValue += d;
        cont++;
    }
    if (cont == 0) {
        return -1.0;
    }
    return theValue / cont;
}

From source file:br.jabuti.metrics.klass.MetricANPM.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    RClass rc = prog.get(className);//from w ww .ja va 2  s. c  om
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }

    int countMethods = 0;
    int countParameters = 0;
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    Method[] methods = theClazz.getMethods();
    for (Method method : methods) {
        if (method.isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(method, theClazz.getClassName(), cp);
        countMethods++;
        countParameters += mg.getArgumentNames().length;
    }
    if (countMethods == 0) {
        return -1.0;
    }
    return (double) countParameters / countMethods;
}

From source file:br.jabuti.metrics.klass.MetricCBO.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    RClass rc = prog.get(className);//  w w  w  .  j  av  a  2s . c om
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPool cp = theClazz.getConstantPool();
    Constant[] cts = cp.getConstantPool();
    Set<String> hs = new HashSet<String>();
    for (Constant ct : cts) {
        if (ct instanceof ConstantCP) {
            ConstantCP cc = (ConstantCP) ct;
            if (!className.equals(cc.getClass(cp))) {
                hs.add(cc.getClass(cp));
            }
        }
    }
    return hs.size();
}

From source file:br.jabuti.metrics.klass.MetricCCAVG.java

License:Open Source License

@Override
public double getResult(Program prog, String className) {
    double theValue = 0.0;
    RClass rc = prog.get(className);/*from   ww  w .jav  a  2  s.c om*/
    if (!(rc instanceof RClassCode)) {
        return -1.0;
    }
    RClassCode rcc = (RClassCode) rc;
    JavaClass theClazz = rcc.getTheClass();
    ConstantPoolGen cp = new ConstantPoolGen(theClazz.getConstantPool());
    ClassGen cg = new ClassGen(theClazz);
    Method[] methods = theClazz.getMethods();
    int k = 0;
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isAbstract()) {
            continue;
        }
        MethodGen mg = new MethodGen(methods[i], theClazz.getClassName(), cp);
        double d = getCyclomaticComplexity(mg, cg);

        if (d < 0.0) {
            return -1;
        }
        theValue += d;
        k++;
    }
    return k > 0 ? theValue / k : 0.0;
}