Search in sources :

Example 16 with SyntaxErrorMessage

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

the class VetoableASTTransformation method addListenerToProperty.

private void addListenerToProperty(SourceUnit source, AnnotationNode node, AnnotatedNode parent) {
    ClassNode declaringClass = parent.getDeclaringClass();
    FieldNode field = ((FieldNode) parent);
    String fieldName = field.getName();
    for (PropertyNode propertyNode : declaringClass.getProperties()) {
        boolean bindable = BindableASTTransformation.hasBindableAnnotation(parent) || BindableASTTransformation.hasBindableAnnotation(parent.getDeclaringClass());
        if (propertyNode.getName().equals(fieldName)) {
            if (field.isStatic()) {
                //noinspection ThrowableInstanceNeverThrown
                source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable cannot annotate a static property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
            } else {
                createListenerSetter(source, bindable, declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) PropertyNode(org.codehaus.groovy.ast.PropertyNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 17 with SyntaxErrorMessage

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

the class CompileUnit method addClass.

/**
     * Adds a class to the unit.
     */
public void addClass(ClassNode node) {
    node = node.redirect();
    String name = node.getName();
    ClassNode stored = classes.get(name);
    if (stored != null && stored != node) {
        // we have a duplicate class!
        // One possibility for this is, that we declared a script and a
        // class in the same file and named the class like the file
        SourceUnit nodeSource = node.getModule().getContext();
        SourceUnit storedSource = stored.getModule().getContext();
        String txt = "Invalid duplicate class definition of class " + node.getName() + " : ";
        if (nodeSource == storedSource) {
            // same class in same source
            txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n";
            if (node.isScriptBody() || stored.isScriptBody()) {
                txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + " the script body based on the file name. Solutions are to change the file name or to change the class name.\n";
            }
        } else {
            txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n";
        }
        nodeSource.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(txt, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), nodeSource));
    }
    classes.put(name, node);
    if (classesToCompile.containsKey(name)) {
        ClassNode cn = classesToCompile.get(name);
        cn.setRedirect(node);
        classesToCompile.remove(name);
    }
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 18 with SyntaxErrorMessage

use of org.codehaus.groovy.control.messages.SyntaxErrorMessage 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 19 with SyntaxErrorMessage

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

the class ClassCodeVisitorSupport method addError.

protected void addError(String msg, ASTNode expr) {
    SourceUnit source = getSourceUnit();
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(msg + '\n', expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(), expr.getLastColumnNumber()), source));
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 20 with SyntaxErrorMessage

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

the class BindableASTTransformation method addListenerToProperty.

private void addListenerToProperty(SourceUnit source, AnnotationNode node, ClassNode declaringClass, FieldNode field) {
    String fieldName = field.getName();
    for (PropertyNode propertyNode : declaringClass.getProperties()) {
        if (propertyNode.getName().equals(fieldName)) {
            if (field.isStatic()) {
                //noinspection ThrowableInstanceNeverThrown
                source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable cannot annotate a static property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
            } else {
                if (needsPropertyChangeSupport(declaringClass, source)) {
                    addPropertyChangeSupport(declaringClass);
                }
                createListenerSetter(declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) PropertyNode(org.codehaus.groovy.ast.PropertyNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Aggregations

SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)28 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)23 Message (org.codehaus.groovy.control.messages.Message)10 ClassNode (org.codehaus.groovy.ast.ClassNode)7 FieldNode (org.codehaus.groovy.ast.FieldNode)6 PropertyNode (org.codehaus.groovy.ast.PropertyNode)6 SourceUnit (org.codehaus.groovy.control.SourceUnit)6 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)6 LocatedMessage (org.codehaus.groovy.control.messages.LocatedMessage)6 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)6 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)5 MismatchedCharException (antlr.MismatchedCharException)2 MismatchedTokenException (antlr.MismatchedTokenException)2 NoViableAltException (antlr.NoViableAltException)2 NoViableAltForCharException (antlr.NoViableAltForCharException)2 GroovyShell (groovy.lang.GroovyShell)2 Reference (groovy.lang.Reference)2 Script (groovy.lang.Script)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2