Search in sources :

Example 1 with MultipleCompilationErrorsException

use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project OpenAM by OpenRock.

the class StandardScriptValidator method getScriptErrors.

/**
     * Convert from ScriptException to ScriptError.
     *
     * @param script the script that was validated.
     * @param se the error thrown by the validation task.
     * @return the converted script error.
     */
private List<ScriptError> getScriptErrors(ScriptObject script, ScriptException se) {
    final List<ScriptError> scriptErrorList = new ArrayList<ScriptError>();
    final Throwable cause = se.getCause();
    // with useful information so we have to retrieve it from the cause exception.
    if (cause instanceof EvaluatorException) {
        final EvaluatorException ee = (EvaluatorException) cause;
        final ScriptError error = new ScriptError();
        error.setScriptName(script.getName());
        error.setMessage(ee.details());
        error.setLineNumber(ee.lineNumber());
        error.setColumnNumber(ee.columnNumber());
        scriptErrorList.add(error);
    } else if (cause instanceof MultipleCompilationErrorsException) {
        ErrorCollector errorCollector = ((MultipleCompilationErrorsException) cause).getErrorCollector();
        for (int i = 0; i < errorCollector.getErrorCount(); i++) {
            final SyntaxException syntaxException = errorCollector.getSyntaxError(i);
            final ScriptError error = new ScriptError();
            error.setScriptName(script.getName());
            error.setMessage(syntaxException.getOriginalMessage());
            error.setLineNumber(syntaxException.getLine());
            error.setColumnNumber(syntaxException.getStartColumn());
            scriptErrorList.add(error);
        }
    } else {
        final ScriptError error = new ScriptError();
        error.setScriptName(script.getName());
        error.setMessage(se.getMessage());
        error.setLineNumber(se.getLineNumber());
        error.setColumnNumber(se.getColumnNumber());
        scriptErrorList.add(error);
    }
    return scriptErrorList;
}
Also used : EvaluatorException(org.mozilla.javascript.EvaluatorException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ArrayList(java.util.ArrayList) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 2 with MultipleCompilationErrorsException

use of org.codehaus.groovy.control.MultipleCompilationErrorsException 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 3 with MultipleCompilationErrorsException

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

the class ScriptSupport method checkValidScript.

public static String checkValidScript(String scriptText) {
    try {
        final File scriptFile = new File(scriptText);
        final GroovyShell shell = new GroovyShell();
        final Script script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(scriptText);
        return null;
    } catch (IOException e) {
        return e.getMessage();
    } catch (MultipleCompilationErrorsException e) {
        final ErrorCollector errorCollector = e.getErrorCollector();
        final List<Message> errors = errorCollector.getErrors();
        for (Message error : errors) {
            if (error instanceof SyntaxErrorMessage) {
                final SyntaxErrorMessage errorMessage = (SyntaxErrorMessage) error;
                final SyntaxException cause = errorMessage.getCause();
                return cause.getMessage();
            }
        }
        return e.getMessage();
    } catch (CompilationFailedException ex) {
        return ex.getLocalizedMessage();
    }
}
Also used : 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) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) ArrayList(java.util.ArrayList) List(java.util.List) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 4 with MultipleCompilationErrorsException

use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project workflow-cps-plugin by jenkinsci.

the class CpsFlowDefinitionValidator method toCheckStatus.

public static List<CheckStatus> toCheckStatus(CompilationFailedException x) {
    List<CheckStatus> errors = new ArrayList<CheckStatus>();
    if (x instanceof MultipleCompilationErrorsException) {
        for (Object o : ((MultipleCompilationErrorsException) x).getErrorCollector().getErrors()) {
            if (o instanceof SyntaxErrorMessage) {
                SyntaxException cause = ((SyntaxErrorMessage) o).getCause();
                CheckStatus st = new CheckStatus(cause.getOriginalMessage(), FAIL_KEY);
                st.setLine(cause.getLine());
                st.setColumn(cause.getStartColumn());
                errors.add(st);
            }
        }
    }
    if (errors.isEmpty()) {
        // Fallback to simple message
        CheckStatus st = new CheckStatus(x.getMessage(), FAIL_KEY);
        st.setLine(1);
        st.setColumn(0);
        errors.add(st);
    }
    return errors;
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ArrayList(java.util.ArrayList) JSONObject(net.sf.json.JSONObject) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 5 with MultipleCompilationErrorsException

use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project gradle by gradle.

the class DefaultScriptCompilationHandler method wrapCompilationFailure.

private void wrapCompilationFailure(ScriptSource source, MultipleCompilationErrorsException e) {
    // Fix the source file name displayed in the error messages
    for (Object message : e.getErrorCollector().getErrors()) {
        if (message instanceof SyntaxErrorMessage) {
            try {
                SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage) message;
                Field sourceField = SyntaxErrorMessage.class.getDeclaredField("source");
                sourceField.setAccessible(true);
                SourceUnit sourceUnit = (SourceUnit) sourceField.get(syntaxErrorMessage);
                Field nameField = SourceUnit.class.getDeclaredField("name");
                nameField.setAccessible(true);
                nameField.set(sourceUnit, source.getDisplayName());
            } catch (Exception failure) {
                throw UncheckedException.throwAsUncheckedException(failure);
            }
        }
    }
    SyntaxException syntaxError = e.getErrorCollector().getSyntaxError(0);
    Integer lineNumber = syntaxError == null ? null : syntaxError.getLine();
    throw new ScriptCompilationException(String.format("Could not compile %s.", source.getDisplayName()), e, source, lineNumber);
}
Also used : Field(java.lang.reflect.Field) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SourceUnit(org.codehaus.groovy.control.SourceUnit) UncheckedException(org.gradle.internal.UncheckedException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) GradleException(org.gradle.api.GradleException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Aggregations

MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)8 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)5 GroovyShell (groovy.lang.GroovyShell)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 ErrorCollector (org.codehaus.groovy.control.ErrorCollector)3 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)3 IOException (java.io.IOException)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)2 Message (org.codehaus.groovy.control.messages.Message)2 GradleException (org.gradle.api.GradleException)2 Binding (groovy.lang.Binding)1 GString (groovy.lang.GString)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyCodeSource (groovy.lang.GroovyCodeSource)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 Script (groovy.lang.Script)1 File (java.io.File)1