Search in sources :

Example 1 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector 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 ErrorCollector

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

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class ExtendedVerifier method visitAnnotation.

/**
     * Resolve metadata and details of the annotation.
     *
     * @param unvisited the node to visit
     * @return the visited node
     */
private AnnotationNode visitAnnotation(AnnotationNode unvisited) {
    ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
    AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
    AnnotationNode visited = visitor.visit(unvisited);
    this.source.getErrorCollector().addCollectorContents(errorCollector);
    return visited;
}
Also used : ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 4 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovity by disney.

the class Groovity method compileLoop.

@SuppressWarnings("rawtypes")
private void compileLoop(Map<String, Class<Script>> newScripts, Map<String, Long> newScriptDates, Map<String, File> deletedScripts, Map<String, Collection<String>> scriptDependencies, Map<String, Boolean> scriptInits, boolean force, boolean init, int numErrors, ConcurrentHashMap<String, Class> compilerTraits, GroovitySource... sources) {
    HashMap<String, GroovitySource> errorSources = new HashMap<>();
    for (GroovitySource source : sources) {
        try {
            GroovityCompilerEvent event = new GroovityCompilerEvent();
            event.setPath(source.getPath());
            event.setTime(System.currentTimeMillis());
            String name = getScriptName(source.getPath());
            String nameCaseFixed = fixCase(name);
            String className = name.replaceAll("_", "___").replaceAll("/", "_");
            // System.out.println("Compiling source "+source.getPath()+" "+name+" "+className);
            if (source.exists()) {
                if (scripts.containsKey(nameCaseFixed)) {
                    event.setChange(Change.update);
                } else {
                    event.setChange(Change.add);
                }
                if (!force) {
                    Long cur = scriptDates.get(nameCaseFixed);
                    // System.out.println("comparing "+cur+" to "+source.getLastModified()+" for "+name);
                    if (cur != null && cur.longValue() == source.getLastModified()) {
                        continue;
                    }
                }
                String sourceCode = null;
                try {
                    sourceCode = source.getSourceCode();
                    // code transformations pre-compile
                    TransformedSource transformed = GroovitySourceTransformer.transformSource(sourceCode);
                    sourceCode = transformed.source;
                    // groovy script compiler
                    long time1 = System.currentTimeMillis();
                    ArrayList<String> dependencies = new ArrayList<String>();
                    CompilerConfiguration compilerConfiguration = createCompilerConfiguration(transformed != null ? transformed.sourceLineNumbers : null, dependencies);
                    // a classloader only gets the traits that are available when it is created, so we make a copy
                    ConcurrentHashMap<String, Class> traitsCopy = new ConcurrentHashMap<>(compilerTraits);
                    GroovityClassLoader loader = getParentLoader() != null ? new GroovityClassLoader(source.getPath(), getParentLoader(), compilerConfiguration, this, cacheRefreshExecutor, traitsCopy) : new GroovityClassLoader(source.getPath(), Thread.currentThread().getContextClassLoader(), compilerConfiguration, this, cacheRefreshExecutor, traitsCopy);
                    CompilationUnit cu = new CompilationUnit(compilerConfiguration, null, loader);
                    SourceUnit su = new TransformedSourceUnit(className.concat(GROOVITY_SOURCE_EXTENSION), transformed, compilerConfiguration, loader, new ErrorCollector(compilerConfiguration));
                    // errorCollector.sourceUnit = su;
                    cu.addSource(su);
                    // Don't compile all or extra class files get generated!
                    cu.compile(Phases.CLASS_GENERATION);
                    @SuppressWarnings("unchecked") GroovyClass[] gcs = (GroovyClass[]) cu.getClasses().toArray(new GroovyClass[0]);
                    Class<Script> scriptClass = loadGroovyClassesAndFindScript(loader, gcs, compilerTraits, traitsCopy, traits);
                    if (scriptClass != null) {
                        long time2 = System.currentTimeMillis();
                        log.info("Compiled Groovy Script: ".concat(source.getPath()) + " in " + (time2 - time1));
                    } else {
                        log.severe("UHOH!!  Unable to find main class for " + source.getPath());
                    }
                    if (jarPhases != null && jarPhases.contains(GroovityPhase.RUNTIME)) {
                        storeClasses(source, gcs);
                    }
                    if (log.isLoggable(Level.FINE)) {
                        log.fine("Registering script class " + scriptClass.getName() + " for name " + name);
                    }
                    newScripts.put(name, scriptClass);
                    newScriptDates.put(name, source.getLastModified());
                    scriptDependencies.put(name, dependencies);
                    scriptInits.put(name, hasInit((GroovityClassLoader) scriptClass.getClassLoader()));
                    if (log.isLoggable(Level.FINE)) {
                        log.fine("Found dependencies " + dependencies + " for script " + name);
                    }
                } catch (Throwable th) {
                    errorSources.put(nameCaseFixed, source);
                    log.log(Level.FINE, "Error compiling " + source.getPath(), th);
                    if (sourceCode != null && log.isLoggable(Level.FINE)) {
                        log.fine("Source code in trouble: \n" + sourceCode);
                    }
                    event.setError(th);
                } finally {
                    compileEvents.put(nameCaseFixed, event);
                }
            } else {
                // remove from memory and disk
                if (jarDirectory != null) {
                    deletedScripts.put(name, getClassesFile(source));
                } else {
                    deletedScripts.put(name, null);
                }
                if (scripts.containsKey(nameCaseFixed)) {
                    event.setChange(Change.remove);
                    compileEvents.put(fixCase(name), event);
                }
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, "Error compiling groovy " + source.getPath(), e);
        }
    }
    if (!errorSources.isEmpty()) {
        boolean retry = (numErrors == 0 || errorSources.size() < numErrors);
        if (!retry) {
            // let's check if the compiling set of traits is missing anything
            for (Map.Entry<String, Class> entry : traits.entrySet()) {
                if (!compilerTraits.containsKey(entry.getKey())) {
                    retry = true;
                    compilerTraits.put(entry.getKey(), entry.getValue());
                }
            }
        }
        if (retry) {
            if (log.isLoggable(Level.FINE)) {
                log.fine("Retrying error compile on " + errorSources.size());
            }
            // retry failed compiles after all the rest in case they just need to pick up traits
            compileLoop(newScripts, newScriptDates, deletedScripts, scriptDependencies, scriptInits, force, init, errorSources.size(), compilerTraits, errorSources.values().toArray(new GroovitySource[0]));
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) GroovyClass(org.codehaus.groovy.tools.GroovyClass) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovitySource(com.disney.groovity.source.GroovitySource) GroovityCompilerEvent(com.disney.groovity.compile.GroovityCompilerEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Script(groovy.lang.Script) TransformedSource(com.disney.groovity.compile.GroovitySourceTransformer.TransformedSource) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GroovyClass(org.codehaus.groovy.tools.GroovyClass) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with ErrorCollector

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

the class ExtendedVerifier method visitAnnotation.

/**
 * Resolve metadata and details of the annotation.
 *
 * @param unvisited the node to visit
 * @return the visited node
 */
private AnnotationNode visitAnnotation(AnnotationNode unvisited) {
    ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
    AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
    AnnotationNode visited = visitor.visit(unvisited);
    this.source.getErrorCollector().addCollectorContents(errorCollector);
    return visited;
}
Also used : ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Aggregations

ErrorCollector (org.codehaus.groovy.control.ErrorCollector)19 ArrayList (java.util.ArrayList)6 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)6 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)5 SourceUnit (org.codehaus.groovy.control.SourceUnit)5 List (java.util.List)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GroovyClass (org.codehaus.groovy.tools.GroovyClass)4 GroovyObject (groovy.lang.GroovyObject)3 LinkedHashSet (java.util.LinkedHashSet)3 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)3 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)3 Script (groovy.lang.Script)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)2 Statement (org.codehaus.groovy.ast.stmt.Statement)2 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)1 GroovityCompilerEvent (com.disney.groovity.compile.GroovityCompilerEvent)1 TransformedSource (com.disney.groovity.compile.GroovitySourceTransformer.TransformedSource)1