Search in sources :

Example 1 with ExceptionMessage

use of org.codehaus.groovy.control.messages.ExceptionMessage in project intellij-community by JetBrains.

the class GrapeRunner method main.

public static void main(String[] args) {
    final GroovyShell shell = new GroovyShell();
    try {
        shell.parse(args[0] + " import java.lang.*");
    } catch (MultipleCompilationErrorsException e) {
        List errors = e.getErrorCollector().getErrors();
        for (Iterator iterator = errors.iterator(); iterator.hasNext(); ) {
            Object o = iterator.next();
            if (o instanceof ExceptionMessage) {
                Exception cause = ((ExceptionMessage) o).getCause();
                String message = cause.getMessage();
                if (message != null && message.startsWith("Error grabbing Grapes")) {
                    System.out.println(message);
                    return;
                }
            }
        }
        e.printStackTrace();
        return;
    } catch (Throwable e) {
        e.printStackTrace();
        return;
    }
    URL[] urls = shell.getClassLoader().getURLs();
    for (int i = 0; i < urls.length; i++) {
        System.out.println(URL_PREFIX + urls[i]);
    }
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) Iterator(java.util.Iterator) List(java.util.List) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) GroovyShell(groovy.lang.GroovyShell) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) URL(java.net.URL)

Example 2 with ExceptionMessage

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

the class JavacJavaCompiler method compile.

public void compile(List<String> files, CompilationUnit cu) {
    String[] javacParameters = makeParameters(files, cu.getClassLoader());
    StringWriter javacOutput = null;
    int javacReturnValue = 0;
    try {
        Class javac = findJavac(cu);
        Method method = null;
        try {
            method = javac.getMethod("compile", new Class[] { String[].class, PrintWriter.class });
            javacOutput = new StringWriter();
            PrintWriter writer = new PrintWriter(javacOutput);
            Object ret = method.invoke(null, javacParameters, writer);
            javacReturnValue = (Integer) ret;
        } catch (NoSuchMethodException e) {
        }
        if (method == null) {
            method = javac.getMethod("compile", new Class[] { String[].class });
            Object ret = method.invoke(null, new Object[] { javacParameters });
            javacReturnValue = (Integer) ret;
        }
        cu.getConfiguration().getOutput();
    } catch (InvocationTargetException ite) {
        cu.getErrorCollector().addFatalError(new ExceptionMessage((Exception) ite.getCause(), true, cu));
    } catch (Exception e) {
        cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
    }
    if (javacReturnValue != 0) {
        switch(javacReturnValue) {
            case 1:
                addJavacError("Compile error during compilation with javac.", cu, javacOutput);
                break;
            case 2:
                addJavacError("Invalid commandline usage for javac.", cu, javacOutput);
                break;
            case 3:
                addJavacError("System error during compilation with javac.", cu, javacOutput);
                break;
            case 4:
                addJavacError("Abnormal termination of javac.", cu, javacOutput);
                break;
            default:
                addJavacError("unexpected return value by javac.", cu, javacOutput);
                break;
        }
    } else {
        // print warnings if any
        System.out.print(javacOutput);
    }
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) StringWriter(java.io.StringWriter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrintWriter(java.io.PrintWriter)

Example 3 with ExceptionMessage

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

the class ASTTransformationCollectorCodeVisitor method addCollectedAnnotations.

private boolean addCollectedAnnotations(List<AnnotationNode> collected, AnnotationNode aliasNode, AnnotatedNode origin) {
    ClassNode classNode = aliasNode.getClassNode();
    boolean ret = false;
    for (AnnotationNode annotation : classNode.getAnnotations()) {
        if (annotation.getClassNode().getName().equals(AnnotationCollector.class.getName())) {
            Expression processorExp = annotation.getMember("processor");
            AnnotationCollectorTransform act = null;
            assertStringConstant(processorExp);
            if (processorExp != null) {
                String className = (String) ((ConstantExpression) processorExp).getValue();
                Class klass = loadTransformClass(className, aliasNode);
                if (klass != null) {
                    try {
                        act = (AnnotationCollectorTransform) klass.newInstance();
                    } catch (InstantiationException e) {
                        source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
                    } catch (IllegalAccessException e) {
                        source.getErrorCollector().addErrorAndContinue(new ExceptionMessage(e, true, source));
                    }
                }
            } else {
                act = new AnnotationCollectorTransform();
            }
            if (act != null)
                collected.addAll(act.visit(annotation, aliasNode, origin, source));
            ret = true;
        }
    }
    return ret;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) AnnotationCollector(groovy.transform.AnnotationCollector) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression)

Example 4 with ExceptionMessage

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

the class CompilationUnit method convertUncaughtExceptionToCompilationError.

private void convertUncaughtExceptionToCompilationError(final Throwable e) {
    // check the exception for a nested compilation exception
    ErrorCollector nestedCollector = null;
    for (Throwable next = e.getCause(); next != e && next != null; next = next.getCause()) {
        if (!(next instanceof MultipleCompilationErrorsException))
            continue;
        MultipleCompilationErrorsException mcee = (MultipleCompilationErrorsException) next;
        nestedCollector = mcee.collector;
        break;
    }
    if (nestedCollector != null) {
        getErrorCollector().addCollectorContents(nestedCollector);
    } else {
        Exception err = e instanceof Exception ? ((Exception) e) : new RuntimeException(e);
        getErrorCollector().addError(new ExceptionMessage(err, configuration.getDebug(), this));
    }
}
Also used : ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 5 with ExceptionMessage

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

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)

Aggregations

ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)12 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 AnnotationCollector (groovy.transform.AnnotationCollector)2 Annotation (java.lang.annotation.Annotation)2 URISyntaxException (java.net.URISyntaxException)2 Iterator (java.util.Iterator)2 List (java.util.List)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 LocatedMessage (org.codehaus.groovy.control.messages.LocatedMessage)2 Message (org.codehaus.groovy.control.messages.Message)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)2 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyShell (groovy.lang.GroovyShell)1