Search in sources :

Example 1 with GroovyClassLoader

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");
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 2 with GroovyClassLoader

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()]);
}
Also used : URISyntaxException(java.net.URISyntaxException) CodeSource(java.security.CodeSource) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 3 with GroovyClassLoader

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;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit)

Example 4 with GroovyClassLoader

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;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 5 with GroovyClassLoader

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;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) Exclusion(org.eclipse.aether.graph.Exclusion) Dependency(org.eclipse.aether.graph.Dependency) File(java.io.File)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)136 File (java.io.File)28 GroovyObject (groovy.lang.GroovyObject)19 Test (org.junit.jupiter.api.Test)18 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Map (java.util.Map)12 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 BuildException (org.apache.tools.ant.BuildException)9 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)9 ClassNode (org.codehaus.groovy.ast.ClassNode)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 ArtefactHandler (grails.core.ArtefactHandler)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7