Search in sources :

Example 6 with ErrorCollector

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

the class TypeCheckingContext method pushErrorCollector.

public ErrorCollector pushErrorCollector() {
    ErrorCollector current = getErrorCollector();
    ErrorCollector collector = new ErrorCollector(current.getConfiguration());
    errorCollectors.add(0, collector);
    return collector;
}
Also used : ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 7 with ErrorCollector

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

the class StaticTypeCheckingVisitor method silentlyVisitMethodNode.

/**
 * Visits a method call target, to infer the type. Don't report errors right
 * away, that will be done by a later visitMethod call.
 */
protected void silentlyVisitMethodNode(final MethodNode directMethodCallCandidate) {
    // visit is authorized because the classnode belongs to the same source unit
    ErrorCollector collector = new ErrorCollector(typeCheckingContext.getErrorCollector().getConfiguration());
    startMethodInference(directMethodCallCandidate, collector);
}
Also used : ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 8 with ErrorCollector

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

the class StaticTypeCheckingVisitor method visitMethod.

@Override
public void visitMethod(final MethodNode node) {
    if (shouldSkipMethodNode(node)) {
        return;
    }
    if (!extension.beforeVisitMethod(node)) {
        ErrorCollector collector = node.getNodeMetaData(ERROR_COLLECTOR);
        if (collector != null) {
            typeCheckingContext.getErrorCollector().addCollectorContents(collector);
        } else {
            startMethodInference(node, typeCheckingContext.getErrorCollector());
        }
        node.removeNodeMetaData(ERROR_COLLECTOR);
    }
    extension.afterVisitMethod(node);
}
Also used : ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 9 with ErrorCollector

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

the class ProxyGeneratorAdapter method adjustSuperClass.

private Class adjustSuperClass(Class superClass, final Class[] interfaces) {
    boolean isSuperClassAnInterface = superClass.isInterface();
    if (!isSuperClassAnInterface) {
        return superClass;
    }
    Class result = Object.class;
    Set<ClassNode> traits = new LinkedHashSet<ClassNode>();
    // check if it's a trait
    collectTraits(superClass, traits);
    if (interfaces != null) {
        for (Class anInterface : interfaces) {
            collectTraits(anInterface, traits);
        }
    }
    if (!traits.isEmpty()) {
        String name = superClass.getName() + "$TraitAdapter";
        ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(new ClassNode[traits.size()]), null);
        CompilationUnit cu = new CompilationUnit(loader);
        CompilerConfiguration config = new CompilerConfiguration();
        SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
        cu.addSource(su);
        cu.compile(Phases.CONVERSION);
        su.getAST().addClass(cn);
        cu.compile(Phases.CLASS_GENERATION);
        @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses();
        for (GroovyClass groovyClass : classes) {
            if (groovyClass.getName().equals(name)) {
                return loader.defineClass(name, groovyClass.getBytes());
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GroovyObject(groovy.lang.GroovyObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project hale by halestudio.

the class GroovyScriptPage method addGroovyErrorAnnotation.

/**
 * Add an error annotation to the given annotation model based on an
 * exception that occurred while compiling or executing the Groovy Script.
 *
 * @param annotationModel the annotation model
 * @param document the current document
 * @param script the executed script, or <code>null</code>
 * @param exception the occurred exception
 */
public static void addGroovyErrorAnnotation(IAnnotationModel annotationModel, IDocument document, Script script, Exception exception) {
    // handle multiple groovy compilation errors
    if (exception instanceof MultipleCompilationErrorsException) {
        ErrorCollector errors = ((MultipleCompilationErrorsException) exception).getErrorCollector();
        for (int i = 0; i < errors.getErrorCount(); i++) {
            SyntaxException ex = errors.getSyntaxError(i);
            if (ex != null) {
                addGroovyErrorAnnotation(annotationModel, document, script, ex);
            }
        }
        return;
    }
    Annotation annotation = new Annotation(SimpleAnnotations.TYPE_ERROR, false, exception.getLocalizedMessage());
    Position position = null;
    // single syntax exception
    if (exception instanceof SyntaxException) {
        int line = ((SyntaxException) exception).getStartLine() - 1;
        if (line >= 0) {
            try {
                position = new Position(document.getLineOffset(line));
            } catch (BadLocationException e1) {
                log.warn("Wrong error position in document", e1);
            }
        }
    }
    // try to determine position from stack trace of script execution
    if (position == null && script != null) {
        for (StackTraceElement ste : exception.getStackTrace()) {
            if (ste.getClassName().startsWith(script.getClass().getName())) {
                int line = ste.getLineNumber() - 1;
                if (line >= 0) {
                    try {
                        position = new Position(document.getLineOffset(line));
                        break;
                    } catch (BadLocationException e1) {
                        log.warn("Wrong error position in document", e1);
                    }
                }
            }
        }
    }
    // fallback
    if (position == null) {
        position = new Position(0);
    }
    annotationModel.addAnnotation(annotation, position);
}
Also used : Position(org.eclipse.jface.text.Position) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

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