如何获取Grails域对象的属性的类型(类)?

Pet*_*ker 14 grails groovy dynamic metaobject

我正在尝试在Grails中动态创建域对象,并遇到这样的问题:对于引用另一个域对象的任何属性,metaproperty告诉我它的类型是"java.lang.Object"而不是期望的类型.

例如:

class PhysicalSiteAssessment {
    // site info
    Site site
    Date sampleDate
    Boolean rainLastWeek
    String additionalNotes
    ...
Run Code Online (Sandbox Code Playgroud)

是域类的开头,它引用另一个域类"站点".

如果我尝试使用此代码(在服务中)动态查找此类的属性类型:

String entityName = "PhysicalSiteAssessment"
Class entityClass
try {
    entityClass = grailsApplication.getClassForName(entityName)
} catch (Exception e) {
    throw new RuntimeException("Failed to load class with name '${entityName}'", e)
}
entityClass.metaClass.getProperties().each() {
    println "Property '${it.name}' is of type '${it.type}'"
}
Run Code Online (Sandbox Code Playgroud)

然后结果是它识别Java类,但不识别Grails域类.输出包含以下行:

Property 'site' is of type 'class java.lang.Object'
Property 'siteId' is of type 'class java.lang.Object'
Property 'sampleDate' is of type 'class java.util.Date'
Property 'rainLastWeek' is of type 'class java.lang.Boolean'
Property 'additionalNotes' is of type 'class java.lang.String' 
Run Code Online (Sandbox Code Playgroud)

问题是我想使用动态查找来查找匹配的对象,例如a

def targetObjects = propertyClass."findBy${idName}"(idValue)
Run Code Online (Sandbox Code Playgroud)

通过内省检索propertyClass的地方,idName是要查找的属性的名称(不一定是数据库ID),idValue是要查找的值.

一切都以:

org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static java.lang.Object.findByCode() is applicable for argument types: (java.lang.String) values: [T04]
Run Code Online (Sandbox Code Playgroud)

有没有办法找到属性的实际域类?或者可能是一些其他解决方案来查找未给出类型的域类的实例(只有具有该类型的属性名称)?

如果我使用类型名称是大写的属性名称("site" - >"Site")以通过grailsApplication实例查找类的约定,它可以工作,但我想避免这种情况.

Sie*_*uer 15

Grails允许您通过GrailsApplication实例访问域模型的一些元信息.你可以这样查找:

import org.codehaus.groovy.grails.commons.ApplicationHolder
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler

def grailsApplication = ApplicationHolder.application
def domainDescriptor = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, "PhysicalSiteAssessment")

def property = domainDescriptor.getPropertyByName("site")
def type = property.getType()
assert type instanceof Class
Run Code Online (Sandbox Code Playgroud)

API: