Search in sources :

Example 1 with GroovyResourceLoader

use of groovy.lang.GroovyResourceLoader in project groovy by apache.

the class Groovyc method buildClassLoaderFor.

protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) parent;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
             * Iterate over the classpath provided to groovyc, and add any missing path
             * entries to the AntClassLoader.  This is a workaround, since for some reason
             * 'directory' classpath entries were not added to the AntClassLoader' classpath.
             */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
                 * fix for GROOVY-2284
                 * seems like AntClassLoader doesn't check if the file
                 * may not exist in the classpath yet
                 */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }
    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        loader.setResourceLoader(new GroovyResourceLoader() {

            public URL loadGroovySource(String filename) throws MalformedURLException {
                return null;
            }
        });
    }
    return loader;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with GroovyResourceLoader

use of groovy.lang.GroovyResourceLoader in project groovy by apache.

the class FileSystemCompiler method doCompilation.

public static void doCompilation(CompilerConfiguration configuration, CompilationUnit unit, String[] filenames, boolean lookupUnnamedFiles) throws Exception {
    File tmpDir = null;
    // if there are any joint compilation options set stubDir if not set
    try {
        if ((configuration.getJointCompilationOptions() != null) && !configuration.getJointCompilationOptions().containsKey("stubDir")) {
            tmpDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source");
            configuration.getJointCompilationOptions().put("stubDir", tmpDir);
        }
        FileSystemCompiler compiler = new FileSystemCompiler(configuration, unit);
        if (lookupUnnamedFiles) {
            for (String filename : filenames) {
                File file = new File(filename);
                if (file.isFile()) {
                    URL url = file.getAbsoluteFile().getParentFile().toURI().toURL();
                    compiler.unit.getClassLoader().addURL(url);
                }
            }
        } else {
            compiler.unit.getClassLoader().setResourceLoader(new GroovyResourceLoader() {

                public URL loadGroovySource(String filename) throws MalformedURLException {
                    return null;
                }
            });
        }
        compiler.compile(filenames);
    } finally {
        try {
            if (tmpDir != null)
                deleteRecursive(tmpDir);
        } catch (Throwable t) {
            System.err.println("error: could not delete temp files - " + tmpDir.getPath());
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) File(java.io.File) URL(java.net.URL)

Example 3 with GroovyResourceLoader

use of groovy.lang.GroovyResourceLoader in project groovy-core by groovy.

the class Groovyc method buildClassLoaderFor.

protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) parent;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
             * Iterate over the classpath provided to groovyc, and add any missing path
             * entries to the AntClassLoader.  This is a workaround, since for some reason
             * 'directory' classpath entries were not added to the AntClassLoader' classpath.
             */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
                 * fix for GROOVY-2284
                 * seems like AntClassLoader doesn't check if the file
                 * may not exist in the classpath yet
                 */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }
    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        loader.setResourceLoader(new GroovyResourceLoader() {

            public URL loadGroovySource(String filename) throws MalformedURLException {
                return null;
            }
        });
    }
    return loader;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 4 with GroovyResourceLoader

use of groovy.lang.GroovyResourceLoader in project groovy-core by groovy.

the class FileSystemCompiler method doCompilation.

public static void doCompilation(CompilerConfiguration configuration, CompilationUnit unit, String[] filenames, boolean lookupUnnamedFiles) throws Exception {
    File tmpDir = null;
    // if there are any joint compilation options set stubDir if not set
    try {
        if ((configuration.getJointCompilationOptions() != null) && !configuration.getJointCompilationOptions().containsKey("stubDir")) {
            tmpDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source");
            configuration.getJointCompilationOptions().put("stubDir", tmpDir);
        }
        FileSystemCompiler compiler = new FileSystemCompiler(configuration, unit);
        if (lookupUnnamedFiles) {
            for (String filename : filenames) {
                File file = new File(filename);
                if (file.isFile()) {
                    URL url = file.getAbsoluteFile().getParentFile().toURI().toURL();
                    compiler.unit.getClassLoader().addURL(url);
                }
            }
        } else {
            compiler.unit.getClassLoader().setResourceLoader(new GroovyResourceLoader() {

                public URL loadGroovySource(String filename) throws MalformedURLException {
                    return null;
                }
            });
        }
        compiler.compile(filenames);
    } finally {
        try {
            if (tmpDir != null)
                deleteRecursive(tmpDir);
        } catch (Throwable t) {
            System.err.println("error: could not delete temp files - " + tmpDir.getPath());
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) URL(java.net.URL)

Aggregations

GroovyResourceLoader (groovy.lang.GroovyResourceLoader)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 File (java.io.File)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 AntClassLoader (org.apache.tools.ant.AntClassLoader)2 BuildException (org.apache.tools.ant.BuildException)2 RootLoader (org.codehaus.groovy.tools.RootLoader)2