Search in sources :

Example 1 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy by apache.

the class ErrorCollector method getSyntaxError.

/**
     * Convenience routine to return the specified error's
     * underlying SyntaxException, or null if it isn't one.
     */
public SyntaxException getSyntaxError(int index) {
    SyntaxException exception = null;
    Message message = getError(index);
    if (message != null && message instanceof SyntaxErrorMessage) {
        exception = ((SyntaxErrorMessage) message).getCause();
    }
    return exception;
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 2 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy by apache.

the class ErrorCollector method getException.

/**
     * Convenience routine to return the specified error's
     * underlying Exception, or null if it isn't one.
     */
public Exception getException(int index) {
    Exception exception = null;
    Message message = getError(index);
    if (message != null) {
        if (message instanceof ExceptionMessage) {
            exception = ((ExceptionMessage) message).getCause();
        } else if (message instanceof SyntaxErrorMessage) {
            exception = ((SyntaxErrorMessage) message).getCause();
        }
    }
    return exception;
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 3 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy by apache.

the class ErrorCollector method write.

//---------------------------------------------------------------------------
// OUTPUT
private void write(PrintWriter writer, Janitor janitor, List messages, String txt) {
    if (messages == null || messages.isEmpty())
        return;
    Iterator iterator = messages.iterator();
    while (iterator.hasNext()) {
        Message message = (Message) iterator.next();
        message.write(writer, janitor);
        if (configuration.getDebug() && (message instanceof SyntaxErrorMessage)) {
            SyntaxErrorMessage sem = (SyntaxErrorMessage) message;
            sem.getCause().printStackTrace(writer);
        }
        writer.println();
    }
    writer.print(messages.size());
    writer.print(" " + txt);
    if (messages.size() > 1)
        writer.print("s");
    writer.println();
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) Iterator(java.util.Iterator)

Example 4 with Message

use of org.codehaus.groovy.control.messages.Message in project ratpack by ratpack.

the class GroovySnippetExecuter method execute.

@Override
public void execute(TestCodeSnippet snippet) throws Exception {
    CompilerConfiguration config = new CompilerConfiguration();
    config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (compileStatic) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
        }
    });
    ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
    GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
    List<String> importsAndSnippet = extractImports(snippet.getSnippet());
    String imports = importsAndSnippet.get(0);
    String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
    String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
    Script script;
    try {
        script = groovyShell.parse(fullSnippet, snippet.getClassName());
    } catch (MultipleCompilationErrorsException e) {
        Message error = e.getErrorCollector().getError(0);
        if (error instanceof SyntaxErrorMessage) {
            //noinspection ThrowableResultOfMethodCallIgnored
            System.out.println(snippet.getSnippet());
            throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
        } else {
            throw e;
        }
    }
    ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
        fixture.around(script::run);
    } finally {
        Thread.currentThread().setContextClassLoader(previousContextClassLoader);
    }
}
Also used : Binding(groovy.lang.Binding) ClassNode(org.codehaus.groovy.ast.ClassNode) Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) GroovyShell(groovy.lang.GroovyShell) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 5 with Message

use of org.codehaus.groovy.control.messages.Message in project groovy-core by groovy.

the class ErrorCollector method getSyntaxError.

/**
     * Convenience routine to return the specified error's
     * underlying SyntaxException, or null if it isn't one.
     */
public SyntaxException getSyntaxError(int index) {
    SyntaxException exception = null;
    Message message = getError(index);
    if (message != null && message instanceof SyntaxErrorMessage) {
        exception = ((SyntaxErrorMessage) message).getCause();
    }
    return exception;
}
Also used : LocatedMessage(org.codehaus.groovy.control.messages.LocatedMessage) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Aggregations

Message (org.codehaus.groovy.control.messages.Message)12 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)12 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)6 LocatedMessage (org.codehaus.groovy.control.messages.LocatedMessage)6 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)6 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)5 MismatchedCharException (antlr.MismatchedCharException)2 MismatchedTokenException (antlr.MismatchedTokenException)2 NoViableAltException (antlr.NoViableAltException)2 NoViableAltForCharException (antlr.NoViableAltForCharException)2 GroovyShell (groovy.lang.GroovyShell)2 Script (groovy.lang.Script)2 Iterator (java.util.Iterator)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 Binding (groovy.lang.Binding)1 CompileStatic (groovy.transform.CompileStatic)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1