From 6cbf9cca10146bca581d4db989aebee0c82b558c Mon Sep 17 00:00:00 2001 From: wisberg Date: Wed, 31 Mar 2004 08:02:30 +0000 Subject: Convenience method to run ajc in a child classloader and get List/String results in the parent (e.g., when running as a JUnit test) --- .../src/org/aspectj/tools/ajc/Main.java | 67 +++++++++++++++++++--- .../testsrc/org/aspectj/tools/ajc/MainTest.java | 33 +++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/MainTest.java (limited to 'org.aspectj.ajdt.core') diff --git a/org.aspectj.ajdt.core/src/org/aspectj/tools/ajc/Main.java b/org.aspectj.ajdt.core/src/org/aspectj/tools/ajc/Main.java index 0fa806314..600962c20 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/tools/ajc/Main.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/tools/ajc/Main.java @@ -17,10 +17,8 @@ package org.aspectj.tools.ajc; import java.io.File; import java.io.IOException; import java.io.PrintStream; -//import java.util.Arrays; -//import java.util.List; -// -//import org.aspectj.ajdt.ajc.BuildArgParser; +import java.util.List; + import org.aspectj.bridge.AbortException; import org.aspectj.bridge.ICommand; import org.aspectj.bridge.IMessage; @@ -81,6 +79,59 @@ public class Main { new Main().runMain(args, true); } + /** + * 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. + * @param args the String[] args to pass to the compiler + * @param useSystemExit if true and errors, return System.exit(errs) + * @param fails the List sink, if any, for String failure (or worse) messages + * @param errors the List sink, if any, for String error messages + * @param warnings the List sink, if any, for String warning messages + * @param info the List sink, if any, for String info messages + * @return number of messages reported with level ERROR or above + * @throws any unchecked exceptions the compiler does + */ + public static int bareMain( + String[] args, + boolean useSystemExit, + List fails, + List errors, + List warnings, + List infos) { + Main main = new Main(); + MessageHandler holder = new MessageHandler(); + main.setHolder(holder); + try { + main.runMain(args, useSystemExit); + } finally { + readMessages(holder, IMessage.FAIL, true, fails); + readMessages(holder, IMessage.ERROR, false, errors); + readMessages(holder, IMessage.WARNING, false, warnings); + readMessages(holder, IMessage.INFO, false, infos); + } + return holder.numMessages(IMessage.ERROR, true); + } + + /** Read messages of a given kind into a List as String */ + private static void readMessages( + IMessageHolder holder, + IMessage.Kind kind, + boolean orGreater, + List sink) { + if ((null == sink) || (null == holder)) { + return; + } + IMessage[] messages = holder.getMessages(kind, orGreater); + if (!LangUtil.isEmpty(messages)) { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < messages.length; i++) { + sink.add(MessagePrinter.render(messages[i])); + } + } + } + /** * @return String rendering throwable as compiler error for user/console, * including information on how to report as a bug. @@ -92,9 +143,7 @@ public class Main { + (null != m ? m + "\n": "") + LangUtil.renderException(thrown, true); } - - - + /** append nothing if numItems is 0, * numItems + label + (numItems > 1? "s" : "") otherwise, * prefixing with " " if sink has content @@ -129,7 +178,7 @@ public class Main { private int lastFails; private int lastErrors; - + /** if not null, run this synchronously after each compile completes */ private Runnable completionRunner; @@ -405,7 +454,7 @@ public class Main { * @param message the IMessage to render * @return String rendering IMessage (never null) */ - protected String render(IMessage message) { + public static String render(IMessage message) { // IMessage.Kind kind = message.getKind(); StringBuffer sb = new StringBuffer(); diff --git a/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/MainTest.java b/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/MainTest.java new file mode 100644 index 000000000..29d64c27c --- /dev/null +++ b/org.aspectj.ajdt.core/testsrc/org/aspectj/tools/ajc/MainTest.java @@ -0,0 +1,33 @@ +/* ******************************************************************* + * Copyright (c) 2004 Contributors. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Common Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * Wes Isberg initial implementation + * ******************************************************************/ + +package org.aspectj.tools.ajc; + +import java.util.ArrayList; + +import junit.framework.TestCase; + +/** + * + */ +public class MainTest extends TestCase { + + public void testMainbare() { + ArrayList list = new ArrayList(); + Main.bareMain(new String[] {"-help"}, false, list, null, null, null); + assertTrue(1 == list.size()); + Object o = list.get(0); + assertTrue(o instanceof String); + assertTrue(-1 != ((String)o).indexOf("-aspectpath")); + assertTrue(-1 != ((String)o).indexOf("-incremental")); + } +} -- cgit v1.2.3