Search in sources :

Example 76 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project spring-boot by spring-projects.

the class AetherGrapeEngineTests method dependencyResolutionWithCustomClassLoader.

@Test
public void dependencyResolutionWithCustomClassLoader() {
    Map<String, Object> args = new HashMap<>();
    GroovyClassLoader customClassLoader = new GroovyClassLoader();
    args.put("classLoader", customClassLoader);
    createGrapeEngine(this.springMilestones).grab(args, createDependency("org.springframework", "spring-jdbc", "3.2.4.RELEASE"));
    assertThat(this.groovyClassLoader.getURLs().length).isEqualTo(0);
    assertThat(customClassLoader.getURLs().length).isEqualTo(5);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 77 with GroovyClassLoader

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

the class Groovyc method loadRegisteredScriptExtensions.

private void loadRegisteredScriptExtensions() {
    if (scriptExtensions.isEmpty()) {
        // first extension will be the one set explicitly on <groovyc>
        scriptExtensions.add(getScriptExtension().substring(2));
        Path classpath = getClasspath() != null ? getClasspath() : new Path(getProject());
        final String[] pe = classpath.list();
        final GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
        for (String file : pe) {
            loader.addClasspath(file);
        }
        scriptExtensions.addAll(SourceExtensionHandler.getRegisteredExtensions(loader));
    }
}
Also used : Path(org.apache.tools.ant.types.Path) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 78 with GroovyClassLoader

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

the class GroovycTask method compile.

protected void compile() {
    Path path = getClasspath();
    if (path != null) {
        config.setClasspath(path.toString());
    }
    config.setTargetDirectory(destdir);
    GroovyClassLoader gcl = createClassLoader();
    CompilationUnit compilation = new CompilationUnit(config, null, gcl);
    GlobPatternMapper mapper = new GlobPatternMapper();
    mapper.setFrom("*.groovy");
    mapper.setTo("*.class");
    int count = 0;
    String[] list = src.list();
    for (int i = 0; i < list.length; i++) {
        File basedir = getProject().resolveFile(list[i]);
        if (!basedir.exists()) {
            throw new BuildException("Source directory does not exist: " + basedir, getLocation());
        }
        DirectoryScanner scanner = getDirectoryScanner(basedir);
        String[] includes = scanner.getIncludedFiles();
        if (force) {
            log.debug("Forcefully including all files from: " + basedir);
            for (int j = 0; j < includes.length; j++) {
                File file = new File(basedir, includes[j]);
                log.debug("    " + file);
                compilation.addSource(file);
                count++;
            }
        } else {
            log.debug("Including changed files from: " + basedir);
            SourceFileScanner sourceScanner = new SourceFileScanner(this);
            File[] files = sourceScanner.restrictAsFiles(includes, basedir, destdir, mapper);
            for (int j = 0; j < files.length; j++) {
                log.debug("    " + files[j]);
                compilation.addSource(files[j]);
                count++;
            }
        }
    }
    if (count > 0) {
        log.info("Compiling " + count + " source file" + (count > 1 ? "s" : "") + " to " + destdir);
        compilation.compile();
    } else {
        log.info("No sources found to compile");
    }
}
Also used : Path(org.apache.tools.ant.types.Path) GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) File(java.io.File)

Example 79 with GroovyClassLoader

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

the class JdkDynamicProxyTest method testJdkDynamicProxySameLoader.

public void testJdkDynamicProxySameLoader() throws Exception {
    // Instantiate all beans.
    final GroovyClassLoader loader = new GroovyClassLoader();
    JdkDynamicProxyServiceBean sb1 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(loader.loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl1").newInstance());
    JdkDynamicProxyServiceBean sb2 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(loader.loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl2").newInstance());
    // Manually wire beans together.
    sb1.setJdkDynamicProxyServiceBean(sb2);
    assertEquals("SERVICE", sb1.doService());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 80 with GroovyClassLoader

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

the class StaticInitTest method testInitOrder.

public void testInitOrder() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
    final Field f = new GroovyClassLoader().loadClass("org.codehaus.groovy.runtime.X", false, false, false).getField("field");
    assertTrue(!failed);
    f.getInt(null);
    assertTrue(failed);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Field(java.lang.reflect.Field)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)126 File (java.io.File)26 GroovyObject (groovy.lang.GroovyObject)17 Test (org.junit.jupiter.api.Test)17 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)13 IOException (java.io.IOException)11 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 HashMap (java.util.HashMap)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 Map (java.util.Map)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 BuildException (org.apache.tools.ant.BuildException)8 ArtefactHandler (grails.core.ArtefactHandler)7 GroovyShell (groovy.lang.GroovyShell)7 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7 Script (groovy.lang.Script)6 Decorator (com.opensymphony.module.sitemesh.Decorator)5