Search in sources :

Example 1 with ClassInjector

use of grails.compiler.ast.ClassInjector in project grails-core by grails.

the class GroovyPageInjectionOperation method getGroovyPageInjectors.

private GroovyPageInjector[] getGroovyPageInjectors() {
    if (groovyPageInjectors == null) {
        List<GroovyPageInjector> injectors = new ArrayList<GroovyPageInjector>();
        for (ClassInjector ci : getClassInjectors()) {
            if (ci instanceof GroovyPageInjector) {
                injectors.add((GroovyPageInjector) ci);
            }
        }
        groovyPageInjectors = injectors.toArray(new GroovyPageInjector[injectors.size()]);
    }
    return groovyPageInjectors;
}
Also used : GroovyPageInjector(grails.compiler.ast.GroovyPageInjector) ArrayList(java.util.ArrayList) ClassInjector(grails.compiler.ast.ClassInjector)

Example 2 with ClassInjector

use of grails.compiler.ast.ClassInjector in project grails-core by grails.

the class AbstractArtefactTypeAstTransformation method performInjectionOnArtefactType.

protected void performInjectionOnArtefactType(SourceUnit sourceUnit, ClassNode cNode, String artefactType) {
    try {
        ClassInjector[] classInjectors = GrailsAwareInjectionOperation.getClassInjectors();
        List<ClassInjector> injectors = ArtefactTypeAstTransformation.findInjectors(artefactType, classInjectors);
        if (!injectors.isEmpty()) {
            AbstractGrailsArtefactTransformer.addToTransformedClasses(cNode.getName());
            for (ClassInjector injector : injectors) {
                if (injector instanceof AllArtefactClassInjector) {
                    injector.performInjection(sourceUnit, cNode);
                } else if (injector instanceof AnnotatedClassInjector) {
                    ((AnnotatedClassInjector) injector).performInjectionOnAnnotatedClass(sourceUnit, null, cNode);
                }
            }
        }
    } catch (RuntimeException e) {
        System.err.println("Error occurred calling AST injector [" + getClass() + "]: " + e.getMessage());
        throw e;
    }
}
Also used : AllArtefactClassInjector(grails.compiler.ast.AllArtefactClassInjector) AnnotatedClassInjector(grails.compiler.ast.AnnotatedClassInjector) AnnotatedClassInjector(grails.compiler.ast.AnnotatedClassInjector) ClassInjector(grails.compiler.ast.ClassInjector) AllArtefactClassInjector(grails.compiler.ast.AllArtefactClassInjector)

Example 3 with ClassInjector

use of grails.compiler.ast.ClassInjector in project grails-core by grails.

the class GrailsSpringLoadedPlugin method reloadEvent.

@Override
public void reloadEvent(String typename, Class<?> clazz, String encodedTimestamp) {
    CachedIntrospectionResults.clearClassLoader(clazz.getClassLoader());
    Introspector.flushFromCaches(clazz);
    ClassInjector[] classInjectors = GrailsAwareInjectionOperation.getClassInjectors();
    for (ClassInjector classInjector : classInjectors) {
        if (classInjector instanceof AbstractGrailsArtefactTransformer) {
            ((AbstractGrailsArtefactTransformer) classInjector).clearCachedState();
        }
    }
    pluginManager.informOfClassChange(clazz);
}
Also used : AbstractGrailsArtefactTransformer(org.grails.compiler.injection.AbstractGrailsArtefactTransformer) ClassInjector(grails.compiler.ast.ClassInjector)

Example 4 with ClassInjector

use of grails.compiler.ast.ClassInjector in project grails-core by grails.

the class EntityASTTransformation method applyTransformation.

public void applyTransformation(SourceUnit sourceUnit, ClassNode classNode) {
    if (GrailsASTUtils.isApplied(classNode, EntityASTTransformation.class)) {
        return;
    }
    GrailsASTUtils.markApplied(classNode, EntityASTTransformation.class);
    GrailsDomainClassInjector domainInjector = new DefaultGrailsDomainClassInjector();
    domainInjector.performInjectionOnAnnotatedEntity(classNode);
    ClassInjector[] classInjectors = GrailsAwareInjectionOperation.getClassInjectors();
    final List<ClassInjector> domainInjectors = ArtefactTypeAstTransformation.findInjectors(DomainClassArtefactHandler.TYPE, classInjectors);
    for (ClassInjector injector : domainInjectors) {
        try {
            injector.performInjection(sourceUnit, classNode);
        } catch (RuntimeException e) {
            try {
                System.err.println("Error occurred calling AST injector [" + injector.getClass().getName() + "]: " + e.getMessage());
            } catch (Throwable t) {
            // ignore
            }
            throw e;
        }
    }
    if (compilationUnit != null) {
        TraitInjectionUtils.processTraitsForNode(sourceUnit, classNode, DomainClassArtefactHandler.TYPE, compilationUnit);
    }
}
Also used : GrailsDomainClassInjector(grails.compiler.ast.GrailsDomainClassInjector) GrailsDomainClassInjector(grails.compiler.ast.GrailsDomainClassInjector) ClassInjector(grails.compiler.ast.ClassInjector)

Example 5 with ClassInjector

use of grails.compiler.ast.ClassInjector in project grails-core by grails.

the class GrailsAwareInjectionOperation method initializeState.

private static void initializeState() {
    if (classInjectors != null) {
        return;
    }
    String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(INJECTOR_CODEHAUS_SCAN_PACKAGE) + "/**/*.class";
    String pattern2 = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(INJECTOR_SCAN_PACKAGE) + "/**/*.class";
    ClassLoader classLoader = GrailsAwareInjectionOperation.class.getClassLoader();
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classLoader);
    Resource[] resources;
    try {
        resources = scanForPatterns(resolver, pattern2, pattern);
        if (resources.length == 0) {
            classLoader = Thread.currentThread().getContextClassLoader();
            resolver = new PathMatchingResourcePatternResolver(classLoader);
            resources = scanForPatterns(resolver, pattern2, pattern);
        }
        final List<ClassInjector> injectors = new ArrayList<ClassInjector>();
        final List<ClassInjector> globalInjectors = new ArrayList<ClassInjector>();
        final Set<Class> injectorClasses = new HashSet<Class>();
        for (Resource resource : resources) {
            // ignore not readable classes and closures
            if (!resource.isReadable() || resource.getFilename().contains("$_"))
                continue;
            InputStream inputStream = resource.getInputStream();
            try {
                final ClassReader classReader = new ClassReader(inputStream);
                final String astTransformerClassName = AstTransformer.class.getSimpleName();
                final ClassLoader finalClassLoader = classLoader;
                classReader.accept(new ClassVisitor(Opcodes.ASM4) {

                    @Override
                    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                        try {
                            if (visible && desc.contains(astTransformerClassName)) {
                                Class<?> injectorClass = finalClassLoader.loadClass(classReader.getClassName().replace('/', '.'));
                                if (injectorClasses.contains(injectorClass))
                                    return super.visitAnnotation(desc, true);
                                if (ClassInjector.class.isAssignableFrom(injectorClass)) {
                                    injectorClasses.add(injectorClass);
                                    ClassInjector classInjector = (ClassInjector) injectorClass.newInstance();
                                    injectors.add(classInjector);
                                    if (GlobalClassInjector.class.isAssignableFrom(injectorClass)) {
                                        globalInjectors.add(classInjector);
                                    }
                                }
                            }
                        } catch (ClassNotFoundException e) {
                        // ignore
                        } catch (InstantiationException e) {
                        // ignore
                        } catch (IllegalAccessException e) {
                        // ignore
                        }
                        return super.visitAnnotation(desc, visible);
                    }
                }, ClassReader.SKIP_CODE);
            } catch (IOException e) {
            // ignore
            } catch (NoClassDefFoundError e) {
            // ignore
            } finally {
                inputStream.close();
            }
        }
        Collections.sort(injectors, new Comparator<ClassInjector>() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            public int compare(ClassInjector classInjectorA, ClassInjector classInjectorB) {
                if (classInjectorA instanceof Comparable) {
                    return ((Comparable) classInjectorA).compareTo(classInjectorB);
                }
                return 0;
            }
        });
        classInjectors = injectors.toArray(new ClassInjector[injectors.size()]);
        globalClassInjectors = globalInjectors.toArray(new ClassInjector[globalInjectors.size()]);
    } catch (IOException e) {
    // ignore
    }
}
Also used : ClassVisitor(org.springframework.asm.ClassVisitor) GlobalClassInjector(grails.compiler.ast.GlobalClassInjector) ClassInjector(grails.compiler.ast.ClassInjector) InputStream(java.io.InputStream) FileSystemResource(org.grails.io.support.FileSystemResource) Resource(org.grails.io.support.Resource) GlobalClassInjector(grails.compiler.ast.GlobalClassInjector) IOException(java.io.IOException) AnnotationVisitor(org.springframework.asm.AnnotationVisitor) ClassReader(org.springframework.asm.ClassReader) PathMatchingResourcePatternResolver(org.grails.io.support.PathMatchingResourcePatternResolver)

Aggregations

ClassInjector (grails.compiler.ast.ClassInjector)8 GlobalClassInjector (grails.compiler.ast.GlobalClassInjector)4 AllArtefactClassInjector (grails.compiler.ast.AllArtefactClassInjector)3 GrailsArtefactClassInjector (grails.compiler.ast.GrailsArtefactClassInjector)2 IOException (java.io.IOException)2 FileSystemResource (org.grails.io.support.FileSystemResource)2 Resource (org.grails.io.support.Resource)2 AnnotatedClassInjector (grails.compiler.ast.AnnotatedClassInjector)1 GrailsDomainClassInjector (grails.compiler.ast.GrailsDomainClassInjector)1 GroovyPageInjector (grails.compiler.ast.GroovyPageInjector)1 CompilationUnitAware (groovy.transform.CompilationUnitAware)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 AbstractGrailsArtefactTransformer (org.grails.compiler.injection.AbstractGrailsArtefactTransformer)1 PathMatchingResourcePatternResolver (org.grails.io.support.PathMatchingResourcePatternResolver)1 AnnotationVisitor (org.springframework.asm.AnnotationVisitor)1 ClassReader (org.springframework.asm.ClassReader)1 ClassVisitor (org.springframework.asm.ClassVisitor)1