Search in sources :

Example 11 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class ConstraintsEvaluatingPropertyTests method testGet.

/*
     * Test method for 'ConstraintsDynamicProperty.get(Object)'
     */
@SuppressWarnings("rawtypes")
public void testGet() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class<?> groovyClass = gcl.parseClass("package org.grails.validation\n" + "class Test {\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
    "   Long id\n" + // WE NEED this even though GORM 2 doesn't, as we're not a "domain" class within grails-app
    "   Long version\n" + " String name\n" + "}");
    GrailsApplication ga = new DefaultGrailsApplication(groovyClass);
    ga.initialise();
    new MappingContextBuilder(ga).build(groovyClass);
    GrailsDomainClass domainClass = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, groovyClass.getName());
    Map constraints = domainClass.getConstrainedProperties();
    assertNotNull(constraints);
    assertFalse(constraints.isEmpty());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) Map(java.util.Map)

Example 12 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class HeirarchyDomainClassTests method testClassHeirarchy.

public void testClassHeirarchy() throws Exception {
    GroovyClassLoader gcl = new GroovyClassLoader();
    gcl.parseClass("@grails.persistence.Entity class Super { Long id;Long version;}\n" + "class Sub extends Super { }\n" + "class Sub2 extends Sub { }");
    GrailsApplication ga = new DefaultGrailsApplication(gcl.getLoadedClasses(), gcl);
    ga.initialise();
    new MappingContextBuilder(ga).build(gcl.getLoadedClasses());
    GrailsDomainClass gdc1 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Super");
    assertNotNull(gdc1);
    assertTrue(gdc1.isRoot());
    assertEquals(2, gdc1.getSubClasses().size());
    assertFalse(gdc1.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc2 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub");
    assertFalse(gdc2.isRoot());
    assertEquals(1, gdc2.getSubClasses().size());
    assertTrue(gdc2.getPropertyByName("id").isInherited());
    GrailsDomainClass gdc3 = (GrailsDomainClass) ga.getArtefact(DomainClassArtefactHandler.TYPE, "Sub2");
    assertFalse(gdc3.isRoot());
    assertEquals(0, gdc3.getSubClasses().size());
    assertTrue(gdc3.getPropertyByName("id").isInherited());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GrailsApplication(grails.core.GrailsApplication) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) GrailsDomainClass(grails.core.GrailsDomainClass) DefaultGrailsApplication(grails.core.DefaultGrailsApplication) MappingContextBuilder(org.grails.core.support.MappingContextBuilder)

Example 13 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class GroovyPagesTemplateEngine method compileGroovyPage.

/**
     * Attempts to compile the given InputStream into a Groovy script using the given name
     * @param in The InputStream to read the Groovy code from
     * @param name The name of the class to use
     * @param pageName The page name
     * @param metaInfo
     * @return The compiled java.lang.Class, which is an instance of groovy.lang.Script
     */
private Class<?> compileGroovyPage(InputStream in, String name, String pageName, GroovyPageMetaInfo metaInfo) {
    GroovyClassLoader groovyClassLoader = findOrInitGroovyClassLoader();
    // Compile the script into an object
    Class<?> scriptClass;
    try {
        String groovySource = IOGroovyMethods.getText(in, GroovyPageParser.GROOVY_SOURCE_CHAR_ENCODING);
        //System.out.println(groovySource);
        scriptClass = groovyClassLoader.parseClass(groovySource, name);
    } catch (CompilationFailedException e) {
        LOG.error("Compilation error compiling GSP [" + name + "]:" + e.getMessage(), e);
        int lineNumber = ExceptionUtils.extractLineNumber(e);
        final int[] lineMappings = metaInfo.getLineNumbers();
        if (lineNumber > 0 && lineNumber < lineMappings.length) {
            lineNumber = lineMappings[lineNumber - 1];
        }
        String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
        throw new GroovyPagesException("Could not parse script [" + relativePageName + "]: " + e.getMessage(), e, lineNumber, pageName);
    } catch (IOException e) {
        String relativePageName = DefaultErrorsPrinter.makeRelativeIfPossible(pageName);
        throw new GroovyPagesException("IO exception parsing script [" + relativePageName + "]: " + e.getMessage(), e);
    }
    GroovyPagesMetaUtils.registerMethodMissingForGSP(scriptClass, tagLibraryLookup);
    return scriptClass;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 14 with GroovyClassLoader

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

the class ProcessingUnit method setClassLoader.

/**
     * Sets the class loader for use by this ProcessingUnit.
     */
public void setClassLoader(GroovyClassLoader loader) {
    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    if (parent == null)
        parent = ProcessingUnit.class.getClassLoader();
    this.classLoader = (loader == null ? new GroovyClassLoader(parent, configuration) : loader);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 15 with GroovyClassLoader

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

the class ScriptEngine method createClassLoader.

private GroovyClassLoader createClassLoader(final Path scriptPath) {
    final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    if (!scriptBaseClass.equals(Script.class)) {
        compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
    }
    compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (staticCompile) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
            classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
            if (scriptPath != null) {
                AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
                scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
                classNode.addAnnotation(scriptPathAnnotation);
            }
        }
    });
    return new GroovyClassLoader(parentLoader, compilerConfiguration) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
            return new CompilationUnit(config, source, this) {

                {
                    verifier = new Verifier() {

                        @Override
                        public void visitClass(ClassNode node) {
                            if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
                                AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
                                lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
                                node.addAnnotation(lineNumberAnnotation);
                            }
                            super.visitClass(node);
                        }
                    };
                }
            };
        }
    };
}
Also used : Script(groovy.lang.Script) ClassNode(org.codehaus.groovy.ast.ClassNode) InheritConstructors(groovy.transform.InheritConstructors) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) CodeSource(java.security.CodeSource) Verifier(org.codehaus.groovy.classgen.Verifier) GroovyClassLoader(groovy.lang.GroovyClassLoader) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

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