简体   繁体   English

Groovy抽象控制器<Domain Class>

[英]Groovy abstract controller <Domain Class>

Embarrassed, but I can't find the errors in my ways. 不好意思,但是我找不到我所遇到的错误。 No matter what I try M is always object (not the actual generic type). 无论我尝试什么, M始终是对象(不是实际的泛型类型)。 I have a simple setup( M is a grails domain): 我有一个简单的设置( M是grails域):

class NewsController extends AbstractController<News> {

}

class AbstractController<M> {

    def show(Long id){
        log.info(M)
        // prints: INFO  common.AbstractController  - class java.lang.Object
        def entity = M.get(id)  //errors
        // .. other code
    }
}

Please be gentle, this feels like a brain fart/something stupid. 请保持温柔,这感觉就像是大脑放屁/愚蠢的东西。

I would say Type Erasure(or its groovy equivalent). 我会说Type Erasure(或它的普通版)。

So the M is there just for compile time checking(At least in java). 因此, M仅用于编译时检查(至少在Java中)。 Now how this works in an interpreted language is a little more interesting, but I think your are mostly likely a victim of related circumstances. 现在,它如何以解释语言工作更有趣,但是我认为您很可能是相关情况的受害者。

It appears that groovy has even stricter requirements throwing away the generic information from source completely. 看来groovy甚至有更严格的要求,要完全从源头上丢弃通用信息。 Which probably leads to some interesting behavioral stuff at runtime. 这可能会在运行时导致一些有趣的行为。

http://groovy.codehaus.org/Generics http://groovy.codehaus.org/Generics

Well simple as it may be, the alternative I came up with (because of type erasures , IMHO a major flaw rendering class generics useless) was: 可能很简单,我想出了另一种选择(由于type erasures ,恕我直言,主要的缺陷渲染类泛型无用):

class AbstractController{
    abstract getDomainClass();   
    def show(Long id){
       log.info(getDomainClass())
    }
    def entity = getDomainClass().get(id) 
}

class NewsController extends AbstractController {
    def getDomainClass(){
        return News
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM