Search in sources :

Example 6 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsClassTests method testAbstractGrailsClassNoPackage.

@Test
public void testAbstractGrailsClassNoPackage() throws Exception {
    GroovyClassLoader cl = new GroovyClassLoader();
    Class<?> clazz = cl.parseClass("class TestService { }");
    GrailsClass grailsClass = new AbstractGrailsClass(clazz, "Service") {
    };
    assertEquals("TestService", clazz.getName());
    assertEquals("Test", grailsClass.getName());
    assertEquals("TestService", grailsClass.getFullName());
    assertNotNull(grailsClass.newInstance());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) GrailsClass(grails.core.GrailsClass) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) Test(org.junit.jupiter.api.Test)

Example 7 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsClassTests method testAbstractGrailsClassPackage.

@Test
public void testAbstractGrailsClassPackage() throws Exception {
    GroovyClassLoader cl = new GroovyClassLoader();
    Class<?> clazz = cl.parseClass("package test.casey; class TestService { }");
    GrailsClass grailsClass = new AbstractGrailsClass(clazz, "Service") {
    };
    assertEquals("test.casey.TestService", clazz.getName());
    assertEquals("Test", grailsClass.getName());
    assertEquals("test.casey.TestService", grailsClass.getFullName());
    assertNotNull(grailsClass.newInstance());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) GrailsClass(grails.core.GrailsClass) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) Test(org.junit.jupiter.api.Test)

Example 8 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsConfigUtils method executeGrailsBootstraps.

/**
 * Executes Grails bootstrap classes
 *
 * @param application The Grails ApplicationContext instance
 * @param webContext The WebApplicationContext instance
 * @param servletContext The ServletContext instance
 */
public static void executeGrailsBootstraps(GrailsApplication application, WebApplicationContext webContext, ServletContext servletContext, GrailsPluginManager grailsPluginManager) {
    configureServletContextAttributes(servletContext, application, grailsPluginManager, webContext);
    PersistenceContextInterceptor interceptor = null;
    String[] beanNames = webContext.getBeanNamesForType(PersistenceContextInterceptor.class);
    if (beanNames.length > 0) {
        interceptor = (PersistenceContextInterceptor) webContext.getBean(beanNames[0]);
    }
    if (interceptor != null) {
        interceptor.init();
    }
    // init the Grails application
    try {
        GrailsClass[] bootstraps = application.getArtefacts(BootstrapArtefactHandler.TYPE);
        for (GrailsClass bootstrap : bootstraps) {
            final GrailsBootstrapClass bootstrapClass = (GrailsBootstrapClass) bootstrap;
            final Object instance = bootstrapClass.getReferenceInstance();
            webContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
            bootstrapClass.callInit(servletContext);
        }
        if (interceptor != null) {
            interceptor.flush();
        }
    } finally {
        if (interceptor != null) {
            interceptor.destroy();
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) PersistenceContextInterceptor(grails.persistence.support.PersistenceContextInterceptor) GrailsBootstrapClass(grails.web.servlet.bootstrap.GrailsBootstrapClass)

Example 9 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class DefaultArtefactInfo method addGrailsClassInternal.

private void addGrailsClassInternal(GrailsClass artefactClass, boolean atStart) {
    grailsClassesByName = new LinkedHashMap<String, GrailsClass>(grailsClassesByName);
    classesByName = new LinkedHashMap<String, Class<?>>(classesByName);
    Class<?> actualClass = artefactClass.getClazz();
    boolean addToGrailsClasses = true;
    if (artefactClass instanceof InjectableGrailsClass) {
        addToGrailsClasses = ((InjectableGrailsClass) artefactClass).getAvailable();
    }
    if (addToGrailsClasses) {
        GrailsClass oldVersion = grailsClassesByName.put(actualClass.getName(), artefactClass);
        grailsClasses.remove(oldVersion);
    }
    classesByName.put(actualClass.getName(), actualClass);
    logicalPropertyNameToClassMap.put(artefactClass.getLogicalPropertyName(), artefactClass);
    if (!grailsClasses.contains(artefactClass)) {
        if (atStart) {
            grailsClasses.addFirst(artefactClass);
        } else {
            grailsClasses.addLast(artefactClass);
        }
    }
}
Also used : GrailsClass(grails.core.GrailsClass) InjectableGrailsClass(grails.core.InjectableGrailsClass) InjectableGrailsClass(grails.core.InjectableGrailsClass) GrailsClass(grails.core.GrailsClass) InjectableGrailsClass(grails.core.InjectableGrailsClass)

Example 10 with GrailsClass

use of grails.core.GrailsClass in project grails-core by grails.

the class GrailsClassTests method testGrailsClassNonPublicConstructor.

@Test
public void testGrailsClassNonPublicConstructor() throws Exception {
    GroovyClassLoader cl = new GroovyClassLoader();
    Class<?> clazz = cl.parseClass("class ProtectedConstructor { protected ProtectedConstructor() {}}");
    GrailsClass grailsClass = new AbstractGrailsClass(clazz, "ProtectedConstructor") {
    };
    assertNotNull(grailsClass.newInstance());
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) GrailsClass(grails.core.GrailsClass) AbstractGrailsClass(org.grails.core.AbstractGrailsClass) Test(org.junit.jupiter.api.Test)

Aggregations

GrailsClass (grails.core.GrailsClass)12 GroovyClassLoader (groovy.lang.GroovyClassLoader)3 AbstractGrailsClass (org.grails.core.AbstractGrailsClass)3 Test (org.junit.jupiter.api.Test)3 PersistenceContextInterceptor (grails.persistence.support.PersistenceContextInterceptor)2 GrailsBootstrapClass (grails.web.servlet.bootstrap.GrailsBootstrapClass)2 Config (grails.config.Config)1 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)1 GrailsApplication (grails.core.GrailsApplication)1 GrailsControllerClass (grails.core.GrailsControllerClass)1 GrailsDomainClass (grails.core.GrailsDomainClass)1 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)1 GrailsTagLibClass (grails.core.GrailsTagLibClass)1 GrailsUrlMappingsClass (grails.core.GrailsUrlMappingsClass)1 InjectableGrailsClass (grails.core.InjectableGrailsClass)1 ArtefactAdditionEvent (grails.core.events.ArtefactAdditionEvent)1 UrlConverter (grails.web.UrlConverter)1 UrlMapping (grails.web.mapping.UrlMapping)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1