/* ******************************************************************* * Copyright (c) 1999-2001 Xerox Corporation, * 2002 Palo Alto Research Center, Incorporated (PARC). * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v 2.0 * which accompanies this distribution and is available at * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt * * Contributors: * Xerox/PARC initial implementation * ******************************************************************/ package org.aspectj.bridge; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.aspectj.bridge.IMessage.Kind; import org.aspectj.util.LangUtil; /** * Convenience API's for constructing, printing, and sending messages. */ public class MessageUtil { // ------ some constant, content-less messages // no variants for "info" or "debug", which should always have content public static final IMessage ABORT_NOTHING_TO_RUN = new Message("aborting - nothing to run", IMessage.ABORT, null, null); public static final IMessage FAIL_INCOMPLETE = new Message("run not completed", IMessage.FAIL, null, null); public static final IMessage ABORT_NOMESSAGE = new Message("", IMessage.ABORT, null, null); public static final IMessage FAIL_NOMESSAGE = new Message("", IMessage.FAIL, null, null); public static final IMessage ERROR_NOMESSAGE = new Message("", IMessage.ERROR, null, null); public static final IMessage WARNING_NOMESSAGE = new Message("", IMessage.WARNING, null, null); /** handle abort message (ignored if handler is null) */ public static boolean abort(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(abort(message))); } /** create and handle exception message (ignored if handler is null) */ public static boolean abort(IMessageHandler handler, String message, Throwable t) { if (handler != null) { return handler.handleMessage(abort(message, t)); } return false; } /** create and handle fail message (ignored if handler is null) */ public static boolean fail(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(fail(message))); } // /** create and handle fail message from reader (ignored if handler is null) */ // public static boolean fail(IMessageHandler handler, String message, LineReader reader) { // return ((null != handler) // && handler.handleMessage(fail(message, reader))); // } /** create and handle fail message (ignored if handler is null) */ public static boolean fail(IMessageHandler handler, String message, Throwable thrown) { return ((null != handler) && handler.handleMessage(fail(message, thrown))); } /** create and handle error message (ignored if handler is null) */ public static boolean error(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(error(message))); } /** create and handle warn message (ignored if handler is null) */ public static boolean warn(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(warn(message))); } /** create and handle debug message (ignored if handler is null) */ public static boolean debug(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(debug(message))); } /** create and handle info message (ignored if handler is null) */ public static boolean info(IMessageHandler handler, String message) { return ((null != handler) && handler.handleMessage(info(message))); } /** @return ABORT_NOMESSAGE if message is empty or IMessage otherwise */ // public static IMessage abort(String message) { if (LangUtil.isEmpty(message)) { return ABORT_NOMESSAGE; } else { return new Message(message, IMessage.ABORT, null, null); } } /** * @return abort IMessage with thrown and message o ABORT_NOMESSAGE if both are empty/null */ // public static IMessage abort(String message, Throwable thrown) { if (!LangUtil.isEmpty(message)) { return new Message(message, IMessage.ABORT, thrown, null); } else if (null == thrown) { return ABORT_NOMESSAGE; } else { return new Message(thrown.getMessage(), IMessage.ABORT, thrown, null); } } /** @return FAIL_NOMESSAGE if message is empty or IMessage otherwise */ public static IMessage fail(String message) { if (LangUtil.isEmpty(message)) { return FAIL_NOMESSAGE; } else { return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY); // return fail(message, (LineReader) null); } } /** * Create fail message. If message is empty but thrown is not, use thrown.getMessage() as the message. If message is empty and * thrown is null, return FAIL_NOMESSAGE. * * @return FAIL_NOMESSAGE if thrown is null and message is empty or IMessage FAIL with message and thrown otherwise */ public static IMessage fail(String message, Throwable thrown) { if (LangUtil.isEmpty(message)) { if (null == thrown) { return FAIL_NOMESSAGE; } else { return new Message(thrown.getMessage(), IMessage.FAIL, thrown, null); } } else { return new Message(message, IMessage.FAIL, thrown, null); } } /** * @return IMessage with IMessage.Kind FAIL and message as text and soure location from reader */ // public static IMessage fail(String message) {//, LineReader reader) { // ISourceLocation loc = null; // if (null == reader) { // loc = ISourceLocation.EMPTY; // } else { // int line = reader.getLineNumber(); // if (0 < line) { // line = 0; // } // loc = new SourceLocation(reader.getFile(), line, line, 0); // } // return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY); // } /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */ // public static IMessage error(String message, ISourceLocation location) { if (LangUtil.isEmpty(message)) { return ERROR_NOMESSAGE; } else { return new Message(message, IMessage.ERROR, null, location); } } /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */ // public static IMessage warn(String message, ISourceLocation location) { if (LangUtil.isEmpty(message)) { return WARNING_NOMESSAGE; } else { return new Message(message, IMessage.WARNING, null, location); } } /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */ // public static IMessage error(String message) { if (LangUtil.isEmpty(message)) { return ERROR_NOMESSAGE; } else { return new Message(message, IMessage.ERROR, null, null); } } /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */ // public static IMessage warn(String message) { if (LangUtil.isEmpty(message)) { return WARNING_NOMESSAGE; } else { return new Message(message, IMessage.WARNING, null, null); } } /** @return IMessage.DEBUG message with message content */ public static IMessage debug(String message) { return new Message(message, IMessage.DEBUG, null, null); } /** @return IMessage.INFO message with message content */ public static IMessage info(String message) { return new Message(message, IMessage.INFO, null, null); } // /** @return ISourceLocation with the current File/line of the reader */ // public static ISourceLocation makeSourceLocation(LineReader reader) { // LangUtil.throwIaxIfNull(reader, "reader"); // // int line = reader.getLineNumber(); // if (0 < line) { // line = 0; // } // return new SourceLocation(reader.getFile(), line, line, 0); // } // ------------------------ printing messages /** * Print total counts message to the print stream, starting each on a new line * * @param messageHolder * @param out */ public static void printMessageCounts(PrintStream out, IMessageHolder messageHolder) { if ((null == out) || (null == messageHolder)) { return; } printMessageCounts(out, messageHolder, ""); } public static void printMessageCounts(PrintStream out, IMessageHolder holder, String prefix) { out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder)); } /** * Print all message to the print stream, starting each on a new line * * @param messageHolder * @param out * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler) */ public static void print(PrintStream out, IMessageHolder messageHolder) { print(out, messageHolder, (String) null, (IMessageRenderer) null, (IMessageHandler) null); } /** * Print all message to the print stream, starting each on a new line, with a prefix. * * @param holder * @param out * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler) */ public static void print(PrintStream out, IMessageHolder holder, String prefix) { print(out, holder, prefix, (IMessageRenderer) null, (IMessageHandler) null); } /** * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer. * * @param holder * @param out * @param renderer IMessageRender to render result - use MESSAGE_LINE if null * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler) */ public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer) { print(out, holder, prefix, renderer, (IMessageHandler) null); } /** * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer. The first line * renders a summary: {prefix}MessageHolder: {summary} Each message line has the following form: * *
* {prefix}[{kind} {index}]: {rendering} ** * (where "{index}" (length 3) is the position within the set of like-kinded messages, ignoring selector omissions. Renderers * are free to render multi-line output. * * @param out the PrintStream sink - return silently if null * @param holder the IMessageHolder with the messages to print * @param prefix the prefix for each line * @param renderer IMessageRender to render result - use MESSAGE_ALL if null * @param selector IMessageHandler to select messages to render - if null, do all non-null */ public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer, IMessageHandler selector) { print(out, holder, prefix, renderer, selector, true); } public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer, IMessageHandler selector, boolean printSummary) { if ((null == out) || (null == holder)) { return; } if (null == renderer) { renderer = MESSAGE_ALL; } if (null == selector) { selector = PICK_ALL; } if (printSummary) { out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder)); } for (IMessage.Kind kind : IMessage.KINDS) { if (!selector.isIgnoring(kind)) { IMessage[] messages = holder.getMessages(kind, IMessageHolder.EQUAL); for (int i = 0; i < messages.length; i++) { if (selector.handleMessage(messages[i])) { String label = (null == prefix ? "" : prefix + "[" + kind + " " + LangUtil.toSizedString(i, 3) + "]: "); out.println(label + renderer.renderToString(messages[i])); } } } } } public static String toShortString(IMessage message) { if (null == message) { return "null"; } String m = message.getMessage(); Throwable t = message.getThrown(); return (message.getKind() + (null == m ? "" : ": " + m) + (null == t ? "" : ": " + LangUtil.unqualifiedClassName(t))); } /** @return int number of message of this kind (optionally or greater) in list */ public static int numMessages(List
* {file:}line{:column} ** */ public static String renderSourceLocation(ISourceLocation loc) { if (null == loc) { return "((ISourceLocation) null)"; } StringBuilder sb = new StringBuilder(); File sourceFile = loc.getSourceFile(); if (sourceFile != ISourceLocation.NO_FILE) { sb.append(sourceFile.getPath()); sb.append(":"); } int line = loc.getLine(); sb.append("" + line); int column = loc.getColumn(); if (column != ISourceLocation.NO_COLUMN) { sb.append(":" + column); } return sb.toString(); } /** * Render message in a line. IMessage.Kind is always printed, then any unqualified exception class, then the remainder of text * and location according to their relative scale, all to fit in max characters or less. This does not render thrown except for * the unqualified class name * * @param max the number of characters - forced to 32..10000 * @param textScale relative proportion to spend on message and/or exception message, relative to source location - if 0, * message is suppressed * @param locScale relative proportion to spend on source location suppressed if 0 * @return "((IMessage) null)" or message per spec */ public static String renderMessageLine(IMessage message, int textScale, int locScale, int max) { if (null == message) { return "((IMessage) null)"; } if (max < 32) { max = 32; } else if (max > 10000) { max = 10000; } if (0 > textScale) { textScale = -textScale; } if (0 > locScale) { locScale = -locScale; } String text = message.getMessage(); Throwable thrown = message.getThrown(); ISourceLocation sl = message.getSourceLocation(); IMessage.Kind kind = message.getKind(); StringBuilder result = new StringBuilder(); result.append(kind.toString()); result.append(": "); if (null != thrown) { result.append(LangUtil.unqualifiedClassName(thrown) + " "); if ((null == text) || ("".equals(text))) { text = thrown.getMessage(); } } if (0 == textScale) { text = ""; } else if ((null != text) && (null != thrown)) { // decide between message and exception text? String s = thrown.getMessage(); if ((null != s) && (0 < s.length())) { text += " - " + s; } } String loc = ""; if ((0 != locScale) && (null != sl)) { File f = sl.getSourceFile(); if (f == ISourceLocation.NO_FILE) { f = null; } if (null != f) { loc = f.getName(); } int line = sl.getLine(); int col = sl.getColumn(); int end = sl.getEndLine(); if ((0 == line) && (0 == col) && (0 == end)) { // ignore numbers if default } else { loc += ":" + line + (col == 0 ? "" : ":" + col); if (line != end) { // XXX consider suppressing nonstandard... loc += ":" + end; } } if (!LangUtil.isEmpty(loc)) { loc = "@[" + loc; // matching "]" added below after clipping } } // now budget between text and loc float totalScale = locScale + textScale; float remainder = max - result.length() - 4; if ((remainder > 0) && (0 < totalScale)) { int textSize = (int) (remainder * textScale / totalScale); int locSize = (int) (remainder * locScale / totalScale); // adjust for underutilization int extra = locSize - loc.length(); if (0 < extra) { locSize = loc.length(); textSize += extra; } extra = textSize - text.length(); if (0 < extra) { textSize = text.length(); if (locSize < loc.length()) { locSize += extra; } } if (locSize > loc.length()) { locSize = loc.length(); } if (textSize > text.length()) { textSize = text.length(); } if (0 < textSize) { result.append(text.substring(0, textSize)); } if (0 < locSize) { if (0 < textSize) { result.append(" "); } result.append(loc.substring(0, locSize) + "]"); } } return result.toString(); } /** @return String of the form "{(# {type}) }.." for message kinds, skipping 0 */ public static String renderCounts(IMessageHolder holder) { if (0 == holder.numMessages(null, false)) { return "(0 messages)"; } StringBuilder sb = new StringBuilder(); for (IMessage.Kind kind : IMessage.KINDS) { int num = holder.numMessages(kind, false); if (0 < num) { sb.append(" (" + num + " " + kind + ") "); } } return sb.toString(); } /** * Factory for handler adapted to PrintStream XXX weak - only handles println(String) * * @param handler the IMessageHandler sink for the messages generated * @param kind the IMessage.Kind of message to create * @param overage the OuputStream for text not captured by the handler (if null, System.out used) * @throws IllegalArgumentException if kind or handler is null */ public static PrintStream handlerPrintStream(final IMessageHandler handler, final IMessage.Kind kind, final OutputStream overage, final String prefix) { LangUtil.throwIaxIfNull(handler, "handler"); LangUtil.throwIaxIfNull(kind, "kind"); class HandlerPrintStream extends PrintStream { HandlerPrintStream() { super(null == overage ? System.out : overage); } public void println() { println(""); } public void println(Object o) { println(null == o ? "null" : o.toString()); } public void println(String input) { String textMessage = (null == prefix ? input : prefix + input); IMessage m = new Message(textMessage, kind, null, null); handler.handleMessage(m); } } return new HandlerPrintStream(); } /** utility class */ private MessageUtil() { } /** * Handle all messages in the second handler using the first * * @param sink the IMessageHandler sink for all messages in source * @param source the IMessageHolder source for all messages to handle * @param fastFail if true, stop on first failure * @return false if any sink.handleMessage(..) failed */ public static boolean handleAll(IMessageHandler sink, IMessageHolder source, boolean fastFail) { return handleAll(sink, source, null, true, fastFail); } /** * Handle messages in the second handler using the first * * @param sink the IMessageHandler sink for all messages in source * @param source the IMessageHolder source for all messages to handle * @param kind the IMessage.Kind to select, if not null * @param orGreater if true, also accept greater kinds * @param fastFail if true, stop on first failure * @return false if any sink.handleMessage(..) failed */ public static boolean handleAll(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater, boolean fastFail) { LangUtil.throwIaxIfNull(sink, "sink"); LangUtil.throwIaxIfNull(source, "source"); return handleAll(sink, source.getMessages(kind, orGreater), fastFail); } /** * Handle messages in the second handler using the first if they are NOT of this kind (optionally, or greater). If you pass null * as the kind, then all messages are ignored and this returns true. * * @param sink the IMessageHandler sink for all messages in source * @param source the IMessageHolder source for all messages to handle * @param kind the IMessage.Kind to reject, if not null * @param orGreater if true, also reject greater kinds * @param fastFail if true, stop on first failure * @return false if any sink.handleMessage(..) failed */ public static boolean handleAllExcept(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater, boolean fastFail) { LangUtil.throwIaxIfNull(sink, "sink"); LangUtil.throwIaxIfNull(source, "source"); if (null == kind) { return true; } IMessage[] messages = getMessagesExcept(source, kind, orGreater); return handleAll(sink, messages, fastFail); } /** * Handle messages in the sink. * * @param sink the IMessageHandler sink for all messages in source * @param sources the IMessage[] messages to handle * @param fastFail if true, stop on first failure * @return false if any sink.handleMessage(..) failed * @throws IllegalArgumentException if sink is null */ public static boolean handleAll(IMessageHandler sink, IMessage[] sources, boolean fastFail) { LangUtil.throwIaxIfNull(sink, "sink"); if (LangUtil.isEmpty(sources)) { return true; } boolean result = true; for (IMessage source : sources) { if (!sink.handleMessage(source)) { if (fastFail) { return false; } if (result) { result = false; } } } return result; } }