Skip to content

Commit f5f0bbd

Browse files
committed
Enhance AstUtils.isDomainClass(ClassNode) to support Domain classes not under the app/domain
In this PR we also deprecate the `AstUtils.isDomainClass(URL)`, please don't use it anymore Closes gh-66
1 parent 1a28c6b commit f5f0bbd

File tree

1 file changed

+44
-16
lines changed
  • grace-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect

1 file changed

+44
-16
lines changed

grace-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/AstUtils.groovy

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2015 original authors
2+
* Copyright 2015-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -31,6 +31,7 @@ import org.codehaus.groovy.ast.ClassNode
3131
import org.codehaus.groovy.ast.CodeVisitorSupport
3232
import org.codehaus.groovy.ast.GenericsType
3333
import org.codehaus.groovy.ast.MethodNode
34+
import org.codehaus.groovy.ast.ModuleNode
3435
import org.codehaus.groovy.ast.Parameter
3536
import org.codehaus.groovy.ast.PropertyNode
3637
import org.codehaus.groovy.ast.VariableScope
@@ -61,6 +62,7 @@ import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpecR
6162
* Utility methods for dealing with Groovy ASTs
6263
*
6364
* @author Graeme Rocher
65+
* @author Michael Yan
6466
* @since 5.0
6567
*/
6668
@CompileStatic
@@ -70,6 +72,9 @@ class AstUtils {
7072
private static final Class<?>[] EMPTY_JAVA_CLASS_ARRAY = []
7173
private static final Class<?>[] OBJECT_CLASS_ARG = [Object.class]
7274

75+
public static final String META_DATA_KEY_GRAILS_APP_DIR = "GRAILS_APP_DIR"
76+
public static final String META_DATA_KEY_PROJECT_DIR = "PROJECT_DIR"
77+
7378
public static final ClassNode COMPILE_STATIC_TYPE = ClassHelper.make(CompileStatic)
7479
public static final ClassNode TYPE_CHECKED_TYPE = ClassHelper.make(TypeChecked)
7580
public static final Object TRANSFORM_APPLIED_MARKER = new Object()
@@ -119,7 +124,9 @@ class AstUtils {
119124
*
120125
* @param url The URL instance
121126
* @return true if it is a domain class
127+
* @deprecated since 2024.0, in favor of #isDomainClass(ClassNode)
122128
*/
129+
@Deprecated(since = "2024.0.0", forRemoval = true)
123130
static boolean isDomainClass(URL url) {
124131
if (url == null) return false
125132

@@ -419,22 +426,14 @@ class AstUtils {
419426

420427
@Memoized
421428
static boolean isDomainClass(ClassNode classNode) {
422-
if (classNode == null) return false
423-
if (classNode.isArray()) return false
424-
if (implementsInterface(classNode, "org.grails.datastore.gorm.GormEntity")) {
425-
return true
429+
if (classNode == null) {
430+
return false
426431
}
427-
String filePath = classNode.getModule() != null ? classNode.getModule().getDescription() : null
428-
if (filePath != null) {
429-
try {
430-
if (isDomainClass(new File(filePath).toURI().toURL())) {
431-
return true
432-
}
433-
}
434-
catch (MalformedURLException e) {
435-
// ignore
436-
}
432+
if (classNode.isArray()) {
433+
return false
437434
}
435+
436+
// First, check if the annotation @Entity has already been added
438437
List<AnnotationNode> annotations = classNode.getAnnotations()
439438
if (annotations != null && !annotations.isEmpty()) {
440439
for (AnnotationNode annotation : annotations) {
@@ -444,6 +443,35 @@ class AstUtils {
444443
}
445444
}
446445
}
446+
447+
// Second, check whether this class implements the GormEntity interface
448+
if (implementsInterface(classNode, "org.grails.datastore.gorm.GormEntity")) {
449+
return true
450+
}
451+
452+
// Last, check whether this class is under the /domain directory
453+
ModuleNode moduleNode = classNode.getModule()
454+
if (moduleNode == null) {
455+
return false
456+
}
457+
SourceUnit sourceNode = moduleNode.getContext()
458+
if (sourceNode == null) {
459+
return false
460+
}
461+
ModuleNode ast = sourceNode.getAST()
462+
if (ast == null) {
463+
return false
464+
}
465+
String filename = sourceNode.getName()
466+
String projectDir = ast.getNodeMetaData(META_DATA_KEY_PROJECT_DIR)
467+
String grailsAppDir = ast.getNodeMetaData(META_DATA_KEY_GRAILS_APP_DIR)
468+
if (filename == null || projectDir == null || grailsAppDir == null) {
469+
return false
470+
}
471+
if (filename.startsWith(grailsAppDir + File.separatorChar + "domain")) {
472+
return true
473+
}
474+
447475
return false
448476
}
449477

0 commit comments

Comments
 (0)