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;
}
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());
}
}
}
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;
}
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());
}
}
}
Aggregations