use of groovy.lang.GroovyClassLoader in project groovy by apache.
the class JavacJavaCompiler method findJavac.
private Class findJavac(CompilationUnit cu) throws ClassNotFoundException {
String main = "com.sun.tools.javac.Main";
try {
return Class.forName(main);
} catch (ClassNotFoundException e) {
}
try {
ClassLoader cl = this.getClass().getClassLoader();
return cl.loadClass(main);
} catch (ClassNotFoundException e) {
}
try {
return ClassLoader.getSystemClassLoader().loadClass(main);
} catch (ClassNotFoundException e) {
}
try {
return cu.getClassLoader().getParent().loadClass(main);
} catch (ClassNotFoundException e3) {
}
// couldn't find compiler - try to find tools.jar
// based on java.home setting
String javaHome = System.getProperty("java.home");
if (javaHome.toLowerCase(Locale.US).endsWith("jre")) {
javaHome = javaHome.substring(0, javaHome.length() - 4);
}
File toolsJar = new File((javaHome + "/lib/tools.jar"));
if (toolsJar.exists()) {
GroovyClassLoader loader = cu.getClassLoader();
loader.addClasspath(toolsJar.getAbsolutePath());
return loader.loadClass(main);
}
throw new ClassNotFoundException("unable to locate the java compiler com.sun.tools.javac.Main, please change your classloader settings");
}
use of groovy.lang.GroovyClassLoader in project groovy by apache.
the class JavacJavaCompiler method makeParameters.
private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) {
Map options = config.getJointCompilationOptions();
LinkedList<String> paras = new LinkedList<String>();
File target = config.getTargetDirectory();
if (target == null)
target = new File(".");
// defaults
paras.add("-d");
paras.add(target.getAbsolutePath());
paras.add("-sourcepath");
paras.add(((File) options.get("stubDir")).getAbsolutePath());
// add flags
String[] flags = (String[]) options.get("flags");
if (flags != null) {
for (String flag : flags) {
paras.add('-' + flag);
}
}
boolean hadClasspath = false;
// add namedValues
String[] namedValues = (String[]) options.get("namedValues");
if (namedValues != null) {
for (int i = 0; i < namedValues.length; i += 2) {
String name = namedValues[i];
if (name.equals("classpath"))
hadClasspath = true;
paras.add('-' + name);
paras.add(namedValues[i + 1]);
}
}
// append classpath if not already defined
if (!hadClasspath) {
// add all classpaths that compilation unit sees
List<String> paths = new ArrayList<String>(config.getClasspath());
ClassLoader cl = parentClassLoader;
while (cl != null) {
if (cl instanceof URLClassLoader) {
for (URL u : ((URLClassLoader) cl).getURLs()) {
try {
paths.add(new File(u.toURI()).getPath());
} catch (URISyntaxException e) {
// ignore it
}
}
}
cl = cl.getParent();
}
try {
CodeSource codeSource = AccessController.doPrivileged(new PrivilegedAction<CodeSource>() {
@Override
public CodeSource run() {
return GroovyObject.class.getProtectionDomain().getCodeSource();
}
});
if (codeSource != null) {
paths.add(new File(codeSource.getLocation().toURI()).getPath());
}
} catch (URISyntaxException e) {
// ignore it
}
StringBuilder resultPath = new StringBuilder(DefaultGroovyMethods.join((Iterable) paths, File.pathSeparator));
paras.add("-classpath");
paras.add(resultPath.toString());
}
// files to compile
paras.addAll(files);
return paras.toArray(new String[paras.size()]);
}
use of groovy.lang.GroovyClassLoader in project intellij-community by JetBrains.
the class DependentGroovycRunner method createCompilationUnit.
private static CompilationUnit createCompilationUnit(final boolean forStubs, final CompilerConfiguration config, final GroovyClassLoader classLoader, Queue mailbox, GroovyCompilerWrapper wrapper) {
final GroovyClassLoader transformLoader = new GroovyClassLoader(classLoader);
try {
if (forStubs) {
return createStubGenerator(config, classLoader, transformLoader, mailbox, wrapper);
}
} catch (NoClassDefFoundError ignore) {
// older groovy distributions just don't have stub generation capability
}
CompilationUnit unit;
try {
unit = new CompilationUnit(config, null, classLoader, transformLoader) {
public void gotoPhase(int phase) throws CompilationFailedException {
super.gotoPhase(phase);
if (phase <= Phases.ALL) {
System.out.println(GroovyRtConstants.PRESENTABLE_MESSAGE + "Groovyc: " + getPhaseDescription());
}
}
};
} catch (NoSuchMethodError e) {
//groovy 1.5.x
unit = new CompilationUnit(config, null, classLoader) {
public void gotoPhase(int phase) throws CompilationFailedException {
super.gotoPhase(phase);
if (phase <= Phases.ALL) {
System.out.println(GroovyRtConstants.PRESENTABLE_MESSAGE + "Groovyc: " + getPhaseDescription());
}
}
};
}
return unit;
}
use of groovy.lang.GroovyClassLoader in project intellij-community by JetBrains.
the class DependentGroovycRunner method buildClassLoaderFor.
static GroovyClassLoader buildClassLoaderFor(final CompilerConfiguration compilerConfiguration, final AstAwareResourceLoader resourceLoader) {
final ClassDependencyLoader checkWellFormed = new ClassDependencyLoader() {
@Override
protected void loadClassDependencies(Class aClass) throws ClassNotFoundException {
if (resourceLoader.getSourceFile(aClass.getName()) == null)
return;
super.loadClassDependencies(aClass);
}
};
GroovyClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() {
public GroovyClassLoader run() {
return new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), compilerConfiguration) {
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript) throws ClassNotFoundException, CompilationFailedException {
Class aClass;
try {
aClass = super.loadClass(name, lookupScriptFiles, preferClassOverScript);
} catch (NoClassDefFoundError e) {
throw new ClassNotFoundException(name);
} catch (LinkageError e) {
throw new RuntimeException("Problem loading class " + name, e);
}
return checkWellFormed.loadDependencies(aClass);
}
};
}
});
classLoader.setResourceLoader(resourceLoader);
return classLoader;
}
use of groovy.lang.GroovyClassLoader in project spring-boot by spring-projects.
the class AetherGrapeEngine method grab.
@Override
public Object grab(Map args, Map... dependencyMaps) {
List<Exclusion> exclusions = createExclusions(args);
List<Dependency> dependencies = createDependencies(dependencyMaps, exclusions);
try {
List<File> files = resolve(dependencies);
GroovyClassLoader classLoader = getClassLoader(args);
for (File file : files) {
classLoader.addURL(file.toURI().toURL());
}
} catch (ArtifactResolutionException ex) {
throw new DependencyResolutionFailedException(ex);
} catch (MalformedURLException ex) {
throw new DependencyResolutionFailedException(ex);
}
return null;
}
Aggregations