Example usage for org.aspectj.apache.bcel.util ClassPath getBytes

List of usage examples for org.aspectj.apache.bcel.util ClassPath getBytes

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.util ClassPath getBytes.

Prototype

public byte[] getBytes(String name, String suffix) throws IOException 

Source Link

Usage

From source file:br.jabuti.device.ProberInstrum.java

License:Open Source License

public static void main(String args[]) throws Throwable {
    String workDir = null;//  w  w w  .j ava 2 s  .  co  m
    String projectName = null;
    JabutiProject project = null;
    String baseClass = null; // The class file to be executed
    Program program = null; // The program to be tested
    HashSet toInstrumenter = null;
    String outName = null; // Name of the output file
    String server = null; // Address of the server
    String tempFile = null; // Name of temporary file
    String midletName = null;
    int memTreshold = 0;
    boolean keepAlive = true;

    if (args.length > 0) {

        int i = 0;

        while (args[i].startsWith("-")) {
            // -CP: Class path
            if (("-d".equals(args[i])) && (i < args.length - 1)) {
                i++;
                workDir = args[i];
            } // -P: project name
            else if (("-p".equals(args[i])) && (i < args.length - 1)) {
                i++;
                projectName = args[i];
            } else if (("-o".equals(args[i])) && (i < args.length - 1)) {
                i++;
                outName = args[i];
            } else if (("-name".equals(args[i])) && (i < args.length - 1)) {
                i++;
                midletName = args[i];
            } else if (("-h".equals(args[i])) && (i < args.length - 1)) {
                i++;
                server = args[i];
            } else if (("-temp".equals(args[i])) && (i < args.length - 1)) {
                i++;
                tempFile = args[i];
            } else if (("-nokeepalive".equals(args[i])) && (i < args.length - 1)) {
                keepAlive = false;
            } else if (("-mem".equals(args[i])) && (i < args.length - 1)) {
                i++;
                int f = 1;

                StringBuffer s = new StringBuffer(args[i]);
                if (args[i].endsWith("M") || args[i].endsWith("m")) {
                    f = 1024 * 1024;
                    s = s.delete(s.length() - 1, s.length());
                } else if (args[i].endsWith("K") || args[i].endsWith("k")) {
                    f = 1024;
                    s = s.delete(s.length() - 1, s.length());
                }
                memTreshold = f * Integer.parseInt(s.toString());
            } else {
                System.out.println("Error: Unrecognized option: " + args[i]);
                System.exit(0);
            }
            i++;
        }

        if (i >= args.length) {
            System.out.println("Error: Missing base class!!!");
            System.exit(0);
        }
        // Ultimo parametro... file seguido dos parametros
        baseClass = args[i++];

        // Checking if all essential parameters are not null
        if (projectName == null || outName == null || midletName == null) {
            System.out.println("Error: Missing parameter!!!");
            usage();
            System.exit(0);
        }

        // Creating the absolute path to a given project
        String absoluteName = null;

        if (workDir != null) {
            absoluteName = workDir + File.separator + projectName;
        } else {
            absoluteName = projectName;
        }

        try {
            File theFile = new File(absoluteName);

            if (!theFile.isFile()) // verifica se existe
            {
                System.out.println("File " + theFile.getName() + " not found");
                System.exit(0);
            }

            project = JabutiProject.reloadProj(theFile.toString(), false);
            program = project.getProgram();

            toInstrumenter = project.getInstr();
        } catch (Exception e) {
            ToolConstants.reportException(e, ToolConstants.STDERR);
            System.exit(0);
        }

        DeviceProbeInsert dpi = new DeviceProbeInsert(program, toInstrumenter, baseClass, tempFile, server,
                memTreshold, keepAlive, midletName);
        Map mp = null;

        try {
            mp = dpi.instrument();
            // substitui os objetos JavaClass por byte[]
            JavaClass jv;

            Iterator it0 = mp.keySet().iterator();
            Hashtable ht = new Hashtable();

            while (it0.hasNext()) {
                String clName = (String) it0.next();
                jv = (JavaClass) mp.get(clName);
                ht.put(clName, jv.getBytes());
            }

            mp = ht;

            File outFile = new File(outName);
            FileOutputStream fos = new FileOutputStream(outFile);
            JarOutputStream outJar = new JarOutputStream(fos);
            Iterator it = mp.keySet().iterator();
            System.out.println();
            System.out.println("----------------------------------------");
            System.out.println("Files to insert:");
            while (it.hasNext()) {
                String clName = (String) it.next();
                System.out.println("\t" + clName);
                byte[] b = (byte[]) mp.get(clName);
                clName = clName.replace('.', '/');
                JarEntry jarEntry = new JarEntry(clName + ".class");
                outJar.putNextEntry(jarEntry);
                outJar.write(b);
            }
            ClassPath cp = new ClassPath(System.getProperty("java.class.path"));
            for (int z1 = 0; z1 < ProberClasses.length; z1++) {
                String clName = ProberClasses[z1];
                System.out.println("\t" + clName);
                byte[] b = cp.getBytes(clName, ".class");
                clName = clName.replace('.', '/');
                JarEntry jarEntry = new JarEntry(clName + ".class");
                outJar.putNextEntry(jarEntry);
                outJar.write(b);
            }
            System.out.println("----------------------------------------");
            outJar.close();
        } catch (Exception eu) {
            System.err.println(eu);
            eu.printStackTrace();
        }

    } else {
        usage();
    }
}

From source file:br.jabuti.probe.desktop.ProberInstrum.java

License:Open Source License

/**
 * //from   w  w  w  .ja  va  2  s . com
 * @param program
 *            - classes that are part of the program
 * @param toInstrumenter
 *            - list of classes to instrument. null == all
 * @param CFGOption
 *            - type of CFG used
 * @param outName
 *            - name of the output file
 * @param traceFileName
 *            - name of the trace file, in case there is a main
 * @return OK?
 */
public static boolean instrumentProgram(Program program, HashSet toInstrumenter, String outName,
        String traceFileName) {

    DefaultProbeInsert dpi = new DefaultProbeInsert(program, toInstrumenter);
    Map mp = null;

    // esta com pau aqui.... Precisa ignorar as classes com
    // ClassFormatException geradas pelo BCEL.
    // Essas classes devem ser excluidas da instrumentao mas incluidas no
    // jar sem serem instrumentadas.

    try {
        mp = dpi.instrument();
        // substitui os objetos JavaClass por byte[]

        Iterator it0 = mp.keySet().iterator();
        Hashtable ht = new Hashtable();

        while (it0.hasNext()) {
            String clName = (String) it0.next();
            JavaClass jv = (JavaClass) mp.get(clName);
            ht.put(clName, jv.getBytes());
        }

        mp = ht;

        File outFile = new File(outName);
        FileOutputStream fos = new FileOutputStream(outFile);
        JarOutputStream outJar = new JarOutputStream(fos);
        Iterator it = mp.keySet().iterator();
        while (it.hasNext()) {
            String clName = (String) it.next();
            byte[] b = (byte[]) mp.get(clName);
            clName = clName.replace('.', '/');
            JarEntry jarEntry = new JarEntry(clName + ".class");
            outJar.putNextEntry(jarEntry);
            outJar.write(b);
        }
        ClassPath cp = new ClassPath(System.getProperty("java.class.path"));
        for (int z1 = 0; z1 < ProberClasses.length; z1++) {
            String clName = ProberClasses[z1];
            byte[] b = cp.getBytes(clName, ".class");
            clName = clName.replace('.', '/');
            JarEntry jarEntry = new JarEntry(clName + ".class");
            outJar.putNextEntry(jarEntry);
            outJar.write(b);
        }
        outJar.close();
    } catch (Exception eu) {
        eu.printStackTrace();
        return false;
    }
    return true;
}

From source file:br.jabuti.probe.desktop.ProberLoader.java

License:Open Source License

public static void main(String args[]) throws Throwable {
    String classPath = null;/*from ww  w. j  a v a2  s. c  om*/
    String workDir = null;
    String projectName = null;
    String baseClass = null; // The class file to be executed
    String[] parameters = null; // The paramenters used to execute the main
    // file

    JabutiProject project = null;
    Program program = null; // The program to be tested
    HashSet toInstrumenter = null;
    String traceFileName = null; // A given class or compressed file name

    if (args.length > 0) {

        int i = 0;

        while (args[i].startsWith("-")) {
            // -cp: Class path
            if (("-cp".equals(args[i])) && (i < args.length - 1)) {
                i++;
                classPath = args[i];
            } // -d: work directory
            else if (("-d".equals(args[i])) && (i < args.length - 1)) {
                i++;
                workDir = args[i];
            } // -p: project name
            else if (("-p".equals(args[i])) && (i < args.length - 1)) {
                i++;
                projectName = args[i];
            } else {
                System.out.println("Error: Unrecognized option: " + args[i]);
                System.exit(0);
            }
            i++;
        }

        if (i >= args.length) {
            System.out.println("Error: Missing base class!!!");
            System.exit(0);
        }
        // Ultimo parmetro... file seguido dos parmetros
        baseClass = args[i++];
        // System.out.println("Loaded: " + clo.loadClass(baseClass));

        parameters = new String[args.length - i];
        for (int j = 0; i < args.length; j++, i++) {
            parameters[j] = args[i];
        }

        // Checking if all essential parameters are not null
        if ((projectName == null) || (baseClass == null)) {
            System.out.println("Error: Missing parameter!!!");
            usage();
            System.exit(0);
        }

        // Creating the absolute path to a given project
        String absoluteName = null;

        if (workDir != null) {
            absoluteName = workDir + File.separator + projectName;
        } else {
            absoluteName = projectName;
        }

        traceFileName = absoluteName.substring(0, absoluteName.length() - ToolConstants.traceExtension.length())
                + ToolConstants.traceExtension;
        // System.setProperty("DEFAULT_PROBER", traceFileName);

        System.out.println("Project Name: " + absoluteName);
        System.out.println("Trace File Name: " + traceFileName);

        try {
            File theFile = new File(absoluteName);

            if (!theFile.isFile()) // verifica se existe
            {
                System.out.println("File " + theFile.getName() + " not found");
                System.exit(0);
            }

            project = JabutiProject.reloadProj(theFile.toString(), false);
            program = project.getProgram();

            toInstrumenter = project.getInstr();
        } catch (Exception e) {
            ToolConstants.reportException(e, ToolConstants.STDERR);
            System.exit(0);
        }

        DefaultProbeInsert dpi = new DefaultProbeInsert(program, toInstrumenter);
        Map mp = null;

        try {
            mp = dpi.instrument();
            // substitui os objetos JavaClass por byte[]
            Iterator it0 = mp.keySet().iterator();
            Hashtable ht = new Hashtable();

            while (it0.hasNext()) {
                String clName = (String) it0.next();
                JavaClass jv = (JavaClass) mp.get(clName);
                ht.put(clName, jv.getBytes());
            }

            ClassPath cp = new ClassPath(System.getProperty("java.class.path"));
            for (int z1 = 0; z1 < ProberClasses.length; z1++) {
                String clName = ProberClasses[z1];
                byte[] b = cp.getBytes(clName, ".class");
                clName = clName.replace('.', '/');
                ht.put(clName, b);
            }

            mp = ht;

            // Alterado: agora o programa soh procura as classes no caminho
            // especificado
            // no argumento -cp
            /*
             * if (classPath != null) { classPath = classPath +
             * System.getProperty("path.separator") +
             * System.getProperty("java.class.path"); } else { classPath =
             * System.getProperty("java.class.path"); }
             */

            if (classPath == null) {
                classPath = ".";
            }

            InstrumentLoader myLoader = new InstrumentLoader(mp, classPath);

            Class xcl = myLoader.loadClass("br.jabuti.probe.DefaultProber");
            Class str = myLoader.loadClass("java.lang.String");

            java.lang.reflect.Method init = xcl.getMethod("init", new Class[] { str });
            init.invoke((Object) null, new Object[] { traceFileName });

            //Included to start the trace collection
            java.lang.reflect.Method start = xcl.getMethod("startTrace", new Class[] {});
            start.invoke((Object) null, new Object[] {});

            myLoader.runClass(baseClass, parameters);
        } catch (Throwable eu) {
            System.err.println(eu);
            eu.printStackTrace();
        }
    } else {
        usage();
    }
}