Example usage for org.aspectj.apache.bcel.generic MethodGen getName

List of usage examples for org.aspectj.apache.bcel.generic MethodGen getName

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.generic MethodGen getName.

Prototype

public String getName() 

Source Link

Usage

From source file:br.jabuti.instrumenter.bytecode.bcel.ASMInstrumenter.java

License:Open Source License

/**
 * This is a test driver. It takes a class name on args[0], inserts
 * some instructions in several points of each method in the class
 * and then dump the instrumented class to "new_"<original_name> 
 * file// ww  w.ja  v a 2  s.co m
 */

public static void main(String args[]) throws Exception {
    // o melhor eh chamar com java ... ASMInstrumenter samples\arquivo.class
    // assim ele vai criar um novo arquivo new_samples\arquivo.class que
    // se pode testar

    JavaClass java_class;

    if ((java_class = Repository.lookupClass(args[0])) == null) {
        java_class = new ClassParser(args[0]).parse();
    } // May throw IOException

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

    for (int i = 0; i < methods.length; i++) {
        try {
            System.out.println("\n\n--------------------------");
            System.out.println(methods[i].getName());
            System.out.println("--------------------------");
            MethodGen mg = new MethodGen(methods[i], cg.getClassName(), cp);
            ASMInstrumenter gi = new ASMInstrumenter(mg, cg, cp);
            int nvars = mg.getMaxLocals() + 10;

            String s = "GETSTATIC java.lang.System out \"Ljava/io/PrintStream;\"  " + "astore " + nvars + " ";
            String s2 = "aload " + nvars + " ";
            String s3 = "LDC \"Entrando no metodo " + mg.getName() + "\" ";
            String s4 = "LDC \"Saindo do metodo " + mg.getName() + "\\n \" ";
            String s5 = "invokevirtual java.io.PrintStream println " + "\"(Ljava/lang/Object;)V\" ";

            gi.insertBefore(mg.getInstructionList().getStart(), s + s2 + s3 + s5);
            gi.insertBefore(mg.getInstructionList().getEnd(), s2 + s4 + s5);
            methods[i] = mg.getMethod();
        } catch (ParseException e) {
            System.err.println("Parser error " + e.getMessage());
        }
    }
    cg.setMethods(methods);
    java_class = cg.getJavaClass();
    java_class.dump("new_" + args[0]);
}

From source file:br.jabuti.metrics.AbstractMetric.java

License:Open Source License

protected final CFG getCFG(MethodGen mg, ClassGen cg) throws InvalidInstructionException, InvalidStackArgument {
    String s = mg.getClassName() + "." + mg.getName() + mg.getSignature();
    Object osg = graphTable.get(s);
    if (osg == null) {
        CFG g = new CFG(mg, cg);
        String sg = Persistency.add(g);
        if (sg != null) {
            graphTable.put(s, sg);//www  . j  a  v  a  2  s .c o  m
        } else {
            graphTable.put(s, g);
        }
        return g;
    } else if (osg instanceof String) {
        String sg = (String) osg;
        CFG g = null;
        try {
            g = (CFG) Persistency.get(sg);
        } catch (Exception e) {
        }
        return g;
    }
    return (CFG) osg;
}