FileDocCategorySizeDatePackage
Jikes.javaAPI DocApache Axis 1.48286Sat Apr 22 18:57:28 BST 2006org.apache.axis.components.compiler

Jikes

public class Jikes extends AbstractCompiler
This class wraps IBM's Jikes Java compiler NOTE: inspired by the Apache Jasper implementation.
author
Davanum Srinivas
author
Stefano Mazzocchi
since
2.0

Fields Summary
protected static Log
log
static final int
OUTPUT_BUFFER_SIZE
static final int
BUFFER_SIZE
Constructors Summary
Methods Summary
public booleancompile()
Execute the compiler


        List args = new ArrayList();
        // command line name
        args.add("jikes");
        // indicate Emacs output mode must be used
        args.add("+E");
        // avoid warnings
        // Option nowarn with one hyphen only
        args.add("-nowarn");

        int exitValue;
        ByteArrayOutputStream tmpErr = new ByteArrayOutputStream(OUTPUT_BUFFER_SIZE);

        try {
            Process p = Runtime.getRuntime().exec(toStringArray(fillArguments(args)));

            BufferedInputStream compilerErr = new BufferedInputStream(p.getErrorStream());

            StreamPumper errPumper = new StreamPumper(compilerErr, tmpErr);

            errPumper.start();

            p.waitFor();
            exitValue = p.exitValue();

            // Wait until the complete error stream has been read
            errPumper.join();
            compilerErr.close();

            p.destroy();

            tmpErr.close();
            this.errors = new ByteArrayInputStream(tmpErr.toByteArray());

        } catch (InterruptedException somethingHappened) {
            log.debug("Jikes.compile():SomethingHappened", somethingHappened);
            return false;
        }

        // Jikes returns 0 even when there are some types of errors.
        // Check if any error output as well
        // Return should be OK when both exitValue and
        // tmpErr.size() are 0 ?!
        return ((exitValue == 0) && (tmpErr.size() == 0));
    
private CompilerErrorparseError(java.lang.String error)
Parse an individual compiler error message

param
error The error text
return
A mssaged CompilerError

        StringTokenizer tokens = new StringTokenizer(error, ":");
        String file = tokens.nextToken();
        if (file.length() == 1) file = new StringBuffer(file).append(":").append(tokens.nextToken()).toString();
        StringBuffer message = new StringBuffer();
        String type = "";
        int startline = 0;
        int startcolumn = 0;
        int endline = 0;
        int endcolumn = 0;

        try {
            startline = Integer.parseInt(tokens.nextToken());
            startcolumn = Integer.parseInt(tokens.nextToken());
            endline = Integer.parseInt(tokens.nextToken());
            endcolumn = Integer.parseInt(tokens.nextToken());
        } catch (Exception e) {
            // FIXME: VG: This is not needed anymore?
            message.append(Messages.getMessage("compilerFail00"));
            type="error";
            log.error(Messages.getMessage("compilerFail00"), e);
        }

        if ("".equals(message)) {
            type = tokens.nextToken().trim().toLowerCase();
            message.append(tokens.nextToken("\n").substring(1).trim());

            while (tokens.hasMoreTokens())
                message.append("\n").append(tokens.nextToken());
        }

        return new CompilerError(file, type.equals("error"), startline, startcolumn, endline, endcolumn, message.toString());
    
protected java.util.ListparseStream(java.io.BufferedReader input)
Parse the compiler error stream to produce a list of CompilerErrors

param
input The error stream
return
The list of compiler error messages
exception
IOException If an error occurs during message collection

        List errors = null;
        String line = null;
        StringBuffer buffer = null;

        while (true) {
            // cleanup the buffer
            buffer = new StringBuffer(); // this is faster than clearing it

            // first line is not space-starting
            if (line == null) line = input.readLine();
            if (line == null) return errors;
            log.debug(line);
            buffer.append(line);

            // all other space-starting lines are one error
            while (true) {
                line = input.readLine();
                // EOF
                if (line == null)
                    break;
                // Continuation of previous error starts with ' '
                if (line.length() > 0 && line.charAt(0) != ' ")
                    break;
                log.debug(line);
                buffer.append('\n");
                buffer.append(line);
            }

            // if error is found create the vector
            if (errors == null) errors = new ArrayList();

            // add the error bean
            errors.add(parseError(buffer.toString()));
        }
    
public java.lang.StringtoString()

        return Messages.getMessage("ibmJikes");
    
protected java.lang.String[]toStringArray(java.util.List arguments)
Copy arguments to a string array

param
arguments The compiler arguments
return
A string array containing compilation arguments

        int i;

        for (i = 0; i < arguments.size(); i++) {
            String arg = (String) arguments.get(i);
            if (arg.equals("-sourcepath")) {
                // Remove -sourcepath option. Jikes does not understand that.
                arguments.remove(i);
                arguments.remove(i);
                break;
            }
        }

        String[] args = new String[arguments.size() + fileList.size()];
        for (i = 0; i < arguments.size(); i++) {
            args[i] = (String) arguments.get(i);
        }

        for (int j=0; j < fileList.size(); i++,j++) {
            args[i] = (String)fileList.get(j);
        }

        return args;