1 /* Copyright 2006-2007 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org
.codehaus
.groovy
.grails
.compiler
.injection
;
17 import groovy
.lang
.GroovyResourceLoader
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.codehaus
.groovy
.ast
.ClassNode
;
21 import org
.codehaus
.groovy
.classgen
.GeneratorContext
;
22 import org
.codehaus
.groovy
.control
.CompilationFailedException
;
23 import org
.codehaus
.groovy
.control
.CompilationUnit
;
24 import org
.codehaus
.groovy
.control
.Phases
;
25 import org
.codehaus
.groovy
.control
.SourceUnit
;
26 import org
.codehaus
.groovy
.grails
.commons
.GrailsResourceUtils
;
27 import org
.springframework
.util
.Assert
;
29 import java
.net
.MalformedURLException
;
33 * A Groovy compiler injection operation that uses a specified array of ClassInjector instances to
34 * attempt AST injection.
36 * @author Graeme Rocher
40 * Created: Jul 27, 2007
43 public class GrailsAwareInjectionOperation
extends CompilationUnit
.PrimaryClassNodeOperation
{
45 private static final Log LOG
= LogFactory
.getLog(GrailsAwareInjectionOperation
.class);
47 private GroovyResourceLoader grailsResourceLoader
;
48 private ClassInjector
[] classInjectors
= new ClassInjector
[0];
50 public GrailsAwareInjectionOperation(GroovyResourceLoader resourceLoader
, ClassInjector
[] injectors
) {
51 Assert
.notNull(resourceLoader
, "The argument [resourceLoader] is required!");
52 this.grailsResourceLoader
= resourceLoader
;
53 if(injectors
!= null) {
54 this.classInjectors
= injectors
;
58 public void call(SourceUnit source
, GeneratorContext context
, ClassNode classNode
) throws CompilationFailedException
{
59 for (int i
= 0; i
< classInjectors
.length
; i
++) {
60 ClassInjector classInjector
= classInjectors
[i
];
64 if(GrailsResourceUtils
.isGrailsPath(source
.getName())) {
65 url
= grailsResourceLoader
.loadGroovySource(GrailsResourceUtils
.getClassName(source
.getName()));
68 url
= grailsResourceLoader
.loadGroovySource(source
.getName());
71 if(classInjector
.shouldInject(url
)) {
72 classInjector
.performInjection(source
, context
, classNode
);
74 } catch (MalformedURLException e
) {
75 LOG
.error("Error loading URL during addition of compile time properties: " + e
.getMessage(),e
);
76 throw new CompilationFailedException(Phases
.CONVERSION
,source
,e
);