Skip to content

Commit 5cf6edb

Browse files
Allow importFrom under @GrailsCompileStatic
Fixes #10157
1 parent 17100b2 commit 5cf6edb

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

grails-core/src/main/groovy/org/grails/compiler/ValidateableTypeCheckingExtension.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ValidateableTypeCheckingExtension extends TypeCheckingDSL {
5757
methodNotFound { ClassNode receiver, String name, ArgumentListExpression argList, ClassNode[] argTypes, MethodCall call ->
5858
def dynamicCall
5959
if(currentScope.constraintsClosureCode && currentScope.checkingConstraintsClosure) {
60-
if(receiver.getField(name)) {
60+
if(receiver.getField(name) || 'importFrom' == name) {
6161
dynamicCall = makeDynamic (call)
6262
}
6363
}

grails-test-suite-uber/src/test/groovy/grails/compiler/GrailsCompileStaticCompilationErrorsSpec.groovy

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package grails.compiler
2-
import grails.persistence.Entity
32

3+
import grails.persistence.Entity
44
import org.codehaus.groovy.control.MultipleCompilationErrorsException
5-
65
import spock.lang.Ignore
76
import spock.lang.Issue
87
import spock.lang.Specification
98

10-
119
class GrailsCompileStaticCompilationErrorsSpec extends Specification {
1210

1311
@Issue('GRAILS-11056')
@@ -469,6 +467,62 @@ class SomeService {
469467
e.message.contains 'Cannot find matching method java.lang.String#lastName()'
470468

471469
}
470+
471+
@Issue('grails/grails-core#10157')
472+
void 'Test constraints block which imports from a non-existent class'() {
473+
given:
474+
def gcl = new GroovyClassLoader()
475+
476+
when: 'a class marked with @GrailsCompileStatic imports constraints from a non-existent class'
477+
gcl.parseClass('''
478+
package grails.compiler
479+
480+
import grails.validation.Validateable
481+
482+
@GrailsCompileStatic
483+
class SomeValidateableClassWithInvalidImport implements Validateable {
484+
String name
485+
486+
static constraints = {
487+
importFrom SomeNonExistentClass
488+
}
489+
}
490+
''')
491+
then: 'an error is thrown'
492+
thrown(MultipleCompilationErrorsException)
493+
}
494+
495+
@Issue('grails/grails-core#10157')
496+
void 'Test constraints block which imports constraints'() {
497+
given:
498+
def gcl = new GroovyClassLoader()
499+
500+
when: 'a class marked with @GrailsCompileStatic imports constraints from a non-existent class'
501+
def c = gcl.parseClass('''
502+
package grails.compiler
503+
504+
import grails.validation.Validateable
505+
506+
@GrailsCompileStatic
507+
class SomeValidateableClassWithValidImport implements Validateable {
508+
String name
509+
510+
static constraints = {
511+
importFrom SomeOtherValidateableClass
512+
}
513+
}
514+
515+
class SomeOtherValidateableClass implements Validateable {
516+
String name
517+
static constraints = {
518+
name size: 3..15
519+
}
520+
}
521+
''')
522+
523+
then: 'the constraints were properly imported'
524+
gcl.loadClass('grails.compiler.SomeValidateableClassWithValidImport').constraintsMap['name'].getAppliedConstraint('size').range == 3..15
525+
}
472526
}
473527

474528
@Entity

0 commit comments

Comments
 (0)