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