Search in sources :

Example 6 with SourceUnit

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

the class TraitASTTransformation method registerASTTranformations.

private void registerASTTranformations(final ClassNode helper) {
    ASTTransformationCollectorCodeVisitor collector = new ASTTransformationCollectorCodeVisitor(unit, compilationUnit.getTransformLoader());
    collector.visitClass(helper);
    // Perform an additional phase which has to be done *after* type checking
    compilationUnit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {

        @Override
        public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException {
            if (classNode == helper) {
                PostTypeCheckingExpressionReplacer replacer = new PostTypeCheckingExpressionReplacer(source);
                replacer.visitClass(helper);
            }
        }
    }, CompilePhase.INSTRUCTION_SELECTION.getPhaseNumber());
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SourceUnit(org.codehaus.groovy.control.SourceUnit) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) ASTTransformationCollectorCodeVisitor(org.codehaus.groovy.transform.ASTTransformationCollectorCodeVisitor)

Example 7 with SourceUnit

use of org.codehaus.groovy.control.SourceUnit 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) MalformedURLException(java.net.MalformedURLException) GradleException(org.gradle.api.GradleException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptCompilationException(org.gradle.groovy.scripts.ScriptCompilationException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 8 with SourceUnit

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

the class GradleResolveVisitor method resolveToOuter.

private boolean resolveToOuter(ClassNode type) {
    String name = type.getName();
    // method again we set a NO_CLASS for this name
    if (type instanceof LowerCaseClass) {
        classNodeResolver.cacheClass(name, NO_CLASS);
        return false;
    }
    if (name.contains("$")) {
        return false;
    }
    if (currentClass.getModule().hasPackageName() && name.indexOf('.') == -1) {
        return false;
    }
    ClassNodeResolver.LookupResult lr = null;
    lr = classNodeResolver.resolveName(name, compilationUnit);
    if (lr != null) {
        if (lr.isSourceUnit()) {
            SourceUnit su = lr.getSourceUnit();
            currentClass.getCompileUnit().addClassNodeToCompile(type, su);
        } else {
            type.setRedirect(lr.getClassNode());
        }
        return true;
    }
    return false;
}
Also used : ClassNodeResolver(org.codehaus.groovy.control.ClassNodeResolver) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Example 9 with SourceUnit

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

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

the class SyntaxErrorMessageTest method testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated.

public void testSetsTheSourceLocatorOfItsSyntaxExceptionAsTheNameOfTheCorrespondingSourceUnitWhenInstantiated() {
    SyntaxException syntaxException = new SyntaxException(someString(), -1, -1);
    assertEquals("source locator", null, syntaxException.getSourceLocator());
    String sourceUnitName = someString();
    SourceUnit sourceUnit = SourceUnit.create(sourceUnitName, someString());
    new SyntaxErrorMessage(syntaxException, sourceUnit);
    assertEquals("source locator", sourceUnitName, syntaxException.getSourceLocator());
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) SourceUnit(org.codehaus.groovy.control.SourceUnit)

Aggregations

SourceUnit (org.codehaus.groovy.control.SourceUnit)26 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)10 ClassNode (org.codehaus.groovy.ast.ClassNode)8 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)8 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)6 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)5 GeneratorContext (org.codehaus.groovy.classgen.GeneratorContext)4 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 GroovyObject (groovy.lang.GroovyObject)2 IncorrectTypeHintException (groovy.transform.stc.IncorrectTypeHintException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AntlrParserPlugin (org.codehaus.groovy.antlr.AntlrParserPlugin)2 GroovyLexer (org.codehaus.groovy.antlr.parser.GroovyLexer)2 GroovyRecognizer (org.codehaus.groovy.antlr.parser.GroovyRecognizer)2 GenericsType (org.codehaus.groovy.ast.GenericsType)2