Search in sources :

Example 1 with ErrorReporter

use of org.codehaus.groovy.tools.ErrorReporter in project groovy-core by groovy.

the class Groovyc method runCompiler.

private void runCompiler(String[] commandLine) {
    // hand crank it so we can add our own compiler configuration
    try {
        Options options = FileSystemCompiler.createCompilationOptions();
        CommandLineParser cliParser = new GroovyInternalPosixParser();
        CommandLine cli;
        cli = cliParser.parse(options, commandLine);
        configuration = FileSystemCompiler.generateCompilerConfigurationFromOptions(cli);
        configuration.setScriptExtensions(getScriptExtensions());
        String tmpExtension = getScriptExtension();
        if (tmpExtension.startsWith("*."))
            tmpExtension = tmpExtension.substring(1);
        configuration.setDefaultScriptExtension(tmpExtension);
        // Load the file name list
        String[] filenames = FileSystemCompiler.generateFileNamesFromOptions(cli);
        boolean fileNameErrors = filenames == null;
        fileNameErrors = fileNameErrors && !FileSystemCompiler.validateFiles(filenames);
        if (targetBytecode != null) {
            configuration.setTargetBytecode(targetBytecode);
        }
        if (!fileNameErrors) {
            FileSystemCompiler.doCompilation(configuration, makeCompileUnit(), filenames, forceLookupUnnamedFiles);
        }
    } catch (Exception re) {
        Throwable t = re;
        if ((re.getClass() == RuntimeException.class) && (re.getCause() != null)) {
            // unwrap to the real exception
            t = re.getCause();
        }
        StringWriter writer = new StringWriter();
        new ErrorReporter(t, false).write(new PrintWriter(writer));
        String message = writer.toString();
        taskSuccess = false;
        if (errorProperty != null) {
            getProject().setNewProperty(errorProperty, "true");
        }
        if (failOnError) {
            log.error(message);
            throw new BuildException("Compilation Failed", t, getLocation());
        } else {
            log.error(message);
        }
    }
}
Also used : Options(org.apache.commons.cli.Options) GroovyInternalPosixParser(org.apache.commons.cli.GroovyInternalPosixParser) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) CommandLine(org.apache.commons.cli.CommandLine) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) CommandLineParser(org.apache.commons.cli.CommandLineParser) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Example 2 with ErrorReporter

use of org.codehaus.groovy.tools.ErrorReporter in project groovy by apache.

the class Groovy method processError.

private void processError(Exception e) {
    Writer writer = new StringBuilderWriter();
    new ErrorReporter(e, false).write(new PrintWriter(writer));
    String message = writer.toString();
    throw new BuildException("Script Failed: " + message, e, getLocation());
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) BuildException(org.apache.tools.ant.BuildException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 3 with ErrorReporter

use of org.codehaus.groovy.tools.ErrorReporter in project groovy by apache.

the class Groovyc method runCompiler.

private void runCompiler(String[] commandLine) {
    // hand crank it so we can add our own compiler configuration
    try {
        FileSystemCompiler.CompilationOptions options = new FileSystemCompiler.CompilationOptions();
        CommandLine parser = FileSystemCompiler.configureParser(options);
        parser.parseArgs(commandLine);
        configuration = options.toCompilerConfiguration();
        configuration.setScriptExtensions(getScriptExtensions());
        String tmpExtension = getScriptExtension();
        if (tmpExtension.startsWith("*."))
            tmpExtension = tmpExtension.substring(1);
        configuration.setDefaultScriptExtension(tmpExtension);
        if (targetBytecode != null) {
            configuration.setTargetBytecode(targetBytecode);
        }
        // Load the file name list
        String[] fileNames = options.generateFileNames();
        boolean fileNameErrors = (fileNames == null || !FileSystemCompiler.validateFiles(fileNames));
        if (!fileNameErrors) {
            try (GroovyClassLoader loader = buildClassLoaderFor()) {
                FileSystemCompiler.doCompilation(configuration, makeCompileUnit(loader), fileNames, forceLookupUnnamedFiles);
            }
        }
    } catch (Exception e) {
        Throwable t = e;
        if (e.getClass() == RuntimeException.class && e.getCause() != null) {
            // unwrap to the real exception
            t = e.getCause();
        }
        Writer writer = new StringBuilderWriter();
        new ErrorReporter(t, false).write(new PrintWriter(writer));
        String message = writer.toString();
        taskSuccess = false;
        if (errorProperty != null) {
            getProject().setNewProperty(errorProperty, "true");
        }
        if (failOnError) {
            log.error(message);
            throw new BuildException("Compilation Failed", t, getLocation());
        } else {
            log.error(message);
        }
    }
}
Also used : FileSystemCompiler(org.codehaus.groovy.tools.FileSystemCompiler) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) URISyntaxException(java.net.URISyntaxException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) GroovyClassLoader(groovy.lang.GroovyClassLoader) CommandLine(picocli.CommandLine) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) BuildException(org.apache.tools.ant.BuildException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 4 with ErrorReporter

use of org.codehaus.groovy.tools.ErrorReporter in project groovy-core by groovy.

the class Groovy method processError.

private void processError(Exception e) {
    StringWriter writer = new StringWriter();
    new ErrorReporter(e, false).write(new PrintWriter(writer));
    String message = writer.toString();
    throw new BuildException("Script Failed: " + message, e, getLocation());
}
Also used : ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Example 5 with ErrorReporter

use of org.codehaus.groovy.tools.ErrorReporter in project groovy-core by groovy.

the class Groovy method execGroovy.

/**
 * Exec the statement.
 *
 * @param txt the groovy source to exec
 * @param out not used?
 */
protected void execGroovy(final String txt, final PrintStream out) {
    log.debug("execGroovy()");
    // Check and ignore empty statements
    if ("".equals(txt.trim())) {
        return;
    }
    log.verbose("Script: " + txt);
    if (classpath != null) {
        log.debug("Explicit Classpath: " + classpath.toString());
    }
    if (fork) {
        log.debug("Using fork mode");
        try {
            createClasspathParts();
            createNewArgs(txt);
            super.setFork(fork);
            super.setClassname(useGroovyShell ? "groovy.lang.GroovyShell" : "org.codehaus.groovy.ant.Groovy");
            configureCompiler();
            super.execute();
        } catch (Exception e) {
            StringWriter writer = new StringWriter();
            new ErrorReporter(e, false).write(new PrintWriter(writer));
            String message = writer.toString();
            throw new BuildException("Script Failed: " + message, e, getLocation());
        }
        return;
    }
    Object mavenPom = null;
    final Project project = getProject();
    final ClassLoader baseClassLoader;
    ClassLoader savedLoader = null;
    final Thread thread = Thread.currentThread();
    boolean maven = "org.apache.commons.grant.GrantProject".equals(project.getClass().getName());
    // treat the case Ant is run through Maven, and
    if (maven) {
        if (contextClassLoader) {
            throw new BuildException("Using setContextClassLoader not permitted when using Maven.", getLocation());
        }
        try {
            final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
            final Field contextField = propsHandler.getClass().getDeclaredField("context");
            contextField.setAccessible(true);
            final Object context = contextField.get(propsHandler);
            mavenPom = InvokerHelper.invokeMethod(context, "getProject", EMPTY_OBJECT_ARRAY);
        } catch (Exception e) {
            throw new BuildException("Impossible to retrieve Maven's Ant project: " + e.getMessage(), getLocation());
        }
        // load groovy into "root.maven" classloader instead of "root" so that
        // groovy script can access Maven classes
        baseClassLoader = mavenPom.getClass().getClassLoader();
    } else {
        baseClassLoader = GroovyShell.class.getClassLoader();
    }
    if (contextClassLoader || maven) {
        savedLoader = thread.getContextClassLoader();
        thread.setContextClassLoader(GroovyShell.class.getClassLoader());
    }
    final String scriptName = computeScriptName();
    final GroovyClassLoader classLoader = new GroovyClassLoader(baseClassLoader);
    addClassPathes(classLoader);
    configureCompiler();
    final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
    try {
        parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
    } finally {
        groovy.resetLoadedClasses();
        groovy.getClassLoader().clearCache();
        if (contextClassLoader || maven)
            thread.setContextClassLoader(savedLoader);
    }
}
Also used : Binding(groovy.lang.Binding) AntBuilder(groovy.util.AntBuilder) MissingMethodException(groovy.lang.MissingMethodException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyShell(groovy.lang.GroovyShell) GroovyClassLoader(groovy.lang.GroovyClassLoader) Project(org.apache.tools.ant.Project) Field(java.lang.reflect.Field) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) StringWriter(java.io.StringWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)8 BuildException (org.apache.tools.ant.BuildException)8 ErrorReporter (org.codehaus.groovy.tools.ErrorReporter)8 IOException (java.io.IOException)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 StringBuilderWriter (org.apache.groovy.io.StringBuilderWriter)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 Binding (groovy.lang.Binding)2 GroovyShell (groovy.lang.GroovyShell)2 MissingMethodException (groovy.lang.MissingMethodException)2 Field (java.lang.reflect.Field)2 Project (org.apache.tools.ant.Project)2 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)2 AntBuilder (groovy.ant.AntBuilder)1 AntBuilder (groovy.util.AntBuilder)1 FileWriter (java.io.FileWriter)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 CommandLine (org.apache.commons.cli.CommandLine)1