org.aspectj.tools.ajc
Class Main

java.lang.Object
  extended by org.aspectj.tools.ajc.Main
Direct Known Subclasses:
CompilerWrapper

public class Main
extends java.lang.Object

Programmatic and command-line interface to AspectJ compiler. The compiler is an ICommand obtained by reflection. Not thread-safe. By default, messages are printed as they are emitted; info messages go to the output stream, and warnings and errors go to the error stream.

Clients can handle all messages by registering a holder:

 Main main = new Main();
 IMessageHolder holder = new MessageHandler();
 main.setHolder(holder);
 
Clients can get control after each command completes by installing a Runnable:
 main.setCompletionRunner(new Runnable() {..});
 


Nested Class Summary
static class Main.CommandController
          controller for repeatable command delays until input or file changed or removed
static class Main.LogModeMessagePrinter
           
static class Main.MessagePrinter
          interceptor IMessageHandler to print as we go.
 
Field Summary
protected  ICommand command
           
protected  java.lang.String commandName
          ReflectionFactory identifier for command (compiler)
protected  Main.CommandController controller
          control iteration/continuation for command (compiler)
protected  MessageHandler ourHandler
          internally-set message sink
static java.lang.String THROWN_PREFIX
          Header used when rendering exceptions for users
 
Constructor Summary
Main()
           
 
Method Summary
static int bareMain(java.lang.String[] args, boolean useSystemExit, java.util.List fails, java.util.List errors, java.util.List warnings, java.util.List infos)
          Convenience method to run ajc and collect String lists of messages.
protected static void fail(IMessageHandler handler, java.lang.String message, java.lang.Throwable thrown)
          convenience API to make fail messages (without MessageUtils's fail prefix)
 IMessageHolder getHolder()
           
 MessageHandler getMessageHandler()
           
static void main(java.lang.String[] args)
           
protected  void outMessage(java.lang.String message)
          Messages to the user
 void quit()
          call this to stop after the next iteration of incremental compile
static java.lang.String renderExceptionForUser(java.lang.Throwable thrown)
           
protected  boolean report(boolean pass, IMessageHolder holder)
          Report results from a (possibly-incremental) compile run.
 void run(java.lang.String[] args, IMessageHolder holder)
          Run without using System.exit(..), putting all messages in holder: ERROR: compiler error WARNING: compiler warning FAIL: command error (bad arguments, exception thrown) This handles incremental behavior: If args include "-incremental", repeat for every input char until 'q' is entered.
 void runMain(java.lang.String[] args, boolean useSystemExit)
          Run without throwing exceptions but optionally using System.exit(..).
 void setCommand(ICommand command)
           
 void setCompletionRunner(java.lang.Runnable runner)
          Install a Runnable to be invoked synchronously after each compile completes.
 void setHolder(IMessageHolder holder)
          Set holder to be passed all messages.
protected  void systemExit(IMessageHolder messages)
          Call System.exit(int) with values derived from the number of failures/aborts or errors in messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

THROWN_PREFIX

public static final java.lang.String THROWN_PREFIX
Header used when rendering exceptions for users


controller

protected Main.CommandController controller
control iteration/continuation for command (compiler)


commandName

protected java.lang.String commandName
ReflectionFactory identifier for command (compiler)


command

protected ICommand command

ourHandler

protected final MessageHandler ourHandler
internally-set message sink

Constructor Detail

Main

public Main()
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Parameters:
args - the String[] of command-line arguments
Throws:
java.io.IOException

bareMain

public static int bareMain(java.lang.String[] args,
                           boolean useSystemExit,
                           java.util.List fails,
                           java.util.List errors,
                           java.util.List warnings,
                           java.util.List infos)
Convenience method to run ajc and collect String lists of messages. This can be reflectively invoked with the List collecting parameters supplied by a parent class loader. The String messages take the same form as command-line messages.

Parameters:
args - the String[] args to pass to the compiler
useSystemExit - if true and errors, return System.exit(errs)
fails - the List sink, if any, for String failure (or worse) messages
errors - the List sink, if any, for String error messages
warnings - the List sink, if any, for String warning messages
info - the List sink, if any, for String info messages
Returns:
number of messages reported with level ERROR or above
Throws:
any - unchecked exceptions the compiler does

renderExceptionForUser

public static java.lang.String renderExceptionForUser(java.lang.Throwable thrown)
Returns:
String rendering throwable as compiler error for user/console, including information on how to report as a bug.
Throws:
java.lang.NullPointerException - if thrown is null

getMessageHandler

public MessageHandler getMessageHandler()

setCommand

public void setCommand(ICommand command)

runMain

public void runMain(java.lang.String[] args,
                    boolean useSystemExit)
Run without throwing exceptions but optionally using System.exit(..). This sets up a message handler which emits messages immediately, so report(boolean, IMessageHandler) only reports total number of errors or warnings.

Parameters:
args - the String[] command line for the compiler
useSystemExit - if true, use System.exit(int) to complete unless one of the args is -noExit. and signal result (0 no exceptions/error, <0 exceptions, >0 compiler errors).

run

public void run(java.lang.String[] args,
                IMessageHolder holder)
Run without using System.exit(..), putting all messages in holder: This handles incremental behavior: When complete, this contains all the messages of the final run of the command and/or any FAIL messages produced in running the command, including any Throwable thrown by the command itself.

Parameters:
args - the String[] command line for the compiler
holder - the MessageHandler sink for messages.

quit

public void quit()
call this to stop after the next iteration of incremental compile


setHolder

public void setHolder(IMessageHolder holder)
Set holder to be passed all messages. When holder is set, messages will not be printed by default.

Parameters:
holder - the IMessageHolder sink for all messages (use null to restore default behavior)

getHolder

public IMessageHolder getHolder()

setCompletionRunner

public void setCompletionRunner(java.lang.Runnable runner)
Install a Runnable to be invoked synchronously after each compile completes.

Parameters:
runner - the Runnable to invoke - null to disable

systemExit

protected void systemExit(IMessageHolder messages)
Call System.exit(int) with values derived from the number of failures/aborts or errors in messages.

Parameters:
messages - the IMessageHolder to interrogate.
messages -

outMessage

protected void outMessage(java.lang.String message)
Messages to the user


report

protected boolean report(boolean pass,
                         IMessageHolder holder)
Report results from a (possibly-incremental) compile run. This delegates to any reportHandler or otherwise prints summary counts of errors/warnings to System.err (if any errors) or System.out (if only warnings). WARNING: this silently ignores other messages like FAIL, but clears the handler of all messages when returning true. XXX false This implementation ignores the pass parameter but clears the holder after reporting on the assumption messages were handled/printed already. (ignoring UnsupportedOperationException from holder.clearMessages()).

Parameters:
pass - true result of the command
holder - IMessageHolder with messages from the command
Returns:
false if the process should abort
See Also:
reportCommandResults(IMessageHolder)

fail

protected static void fail(IMessageHandler handler,
                           java.lang.String message,
                           java.lang.Throwable thrown)
convenience API to make fail messages (without MessageUtils's fail prefix)