Search in sources :

Example 6 with SyntaxErrorMessage

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

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 7 with SyntaxErrorMessage

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

the class StaticTypeCheckingVisitor method addError.

@Override
public void addError(final String msg, final ASTNode expr) {
    Long err = ((long) expr.getLineNumber()) << 16 + expr.getColumnNumber();
    if ((DEBUG_GENERATED_CODE && expr.getLineNumber() < 0) || !typeCheckingContext.reportedErrors.contains(err)) {
        typeCheckingContext.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException(msg + '\n', expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(), expr.getLastColumnNumber()), typeCheckingContext.source));
        typeCheckingContext.reportedErrors.add(err);
    }
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 8 with SyntaxErrorMessage

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

the class GroovySnippetExecuter method execute.

@Override
public void execute(TestCodeSnippet snippet) throws Exception {
    CompilerConfiguration config = new CompilerConfiguration();
    config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (compileStatic) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
        }
    });
    ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
    GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
    List<String> importsAndSnippet = extractImports(snippet.getSnippet());
    String imports = importsAndSnippet.get(0);
    String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
    String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
    Script script;
    try {
        script = groovyShell.parse(fullSnippet, snippet.getClassName());
    } catch (MultipleCompilationErrorsException e) {
        Message error = e.getErrorCollector().getError(0);
        if (error instanceof SyntaxErrorMessage) {
            //noinspection ThrowableResultOfMethodCallIgnored
            System.out.println(snippet.getSnippet());
            throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
        } else {
            throw e;
        }
    }
    ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
        fixture.around(script::run);
    } finally {
        Thread.currentThread().setContextClassLoader(previousContextClassLoader);
    }
}
Also used : Binding(groovy.lang.Binding) ClassNode(org.codehaus.groovy.ast.ClassNode) 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) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) GroovyShell(groovy.lang.GroovyShell) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 9 with SyntaxErrorMessage

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

the class ErrorCollector method getSyntaxError.

/**
     * Convenience routine to return the specified error's
     * underlying SyntaxException, or null if it isn't one.
     */
public SyntaxException getSyntaxError(int index) {
    SyntaxException exception = null;
    Message message = getError(index);
    if (message != null && message instanceof SyntaxErrorMessage) {
        exception = ((SyntaxErrorMessage) message).getCause();
    }
    return exception;
}
Also used : 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)

Example 10 with SyntaxErrorMessage

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

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