java / groovy / grails instanceof失败 [英] java/groovy/grails instanceof fails

查看:164
本文介绍了java / groovy / grails instanceof失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Video的类和这个类的一个对象(这里叫做b)

这是测试用例:

  log.debug b 
log.debug b.class
log.debug(b.class ==视频)
log.debug(b instanceof Video)
log.debug(b.instanceOf(Video))
log.debug(b in Video)

输出结果是:
$ b $ pre $ DEBUG NavLinkHelperTagLib - product.content.Video :4352
DEBUG NavLinkHelperTagLib - 类product.content.Video
DEBUG NavLinkHelperTagLib - 真正的
DEBUG NavLinkHelperTagLib - 假
DEBUG NavLinkHelperTagLib - 真正的
DEBUG NavLinkHelperTagLib - 假

有人可以告诉我为什么instanceof失败吗?!

编辑:



我觉得这与GORM有关。这里是我的donain类:

 抽象类Product实现Serializable {

static hasMany = [additionalContents: AdditionalContent]
}

抽象类AdditionalContent实现Serializable {
static belongsTo = [product:Product]

}

class Video extends AdditionalContent {
}

我认为这里的问题是延迟加载?但是当这是代理/懒惰问题时,通常我会在调用.getClass()时看到诸如Video $$ javaassist之类的东西,但我没有看到这一点......

解决方案

这与Hibernate / Gorm相关。你的域对象很可能是由Hibernate代理的。因此,您不能使用Java instanceOf

Grails团队知道这个问题,因此他们在每个Grails域对象中引入了 instanceOf()方法。以下引用来自 Grails参考资料


通过使用GORM的
instanceOf方法,您可以保护自己不受这个问题的影响:



def person = Person.get(1)



断言Pet.list()[0] .instanceOf(Dog)


因此,只需使用 instanceOf()方法比较域类。



为了了解 instanceOf 是如何实现的,你需要反编译你的 Video 使用 JD-GUI



Grails 3应用程序由特性 GormEntity 实现。

  @ Traits.TraitBridge(traitClass = GormEntity.class,DESC = (Ljava /郎/类;)Z)
公共布尔}这种(类ARG1)
{
//字节代码:
// 0:invokestatic 88 ch / silviowangler / zscsupporter / Game:$ getCallSiteArray()[Lorg / codehaus / groovy / runtime / callsite / CallSite;
// 3:astore_2
// 4:aload_2
// 5:ldc_w 548
// 8:aaload
// 9:ldc -104
// 11:aload_0
// 12:aload_1
// 13:invokeinterface 209 4 0
// 18:invokestatic 182组织/ Codehaus的/常规/运行/ typehandling / DefaultTypeTransformation: booleanUnbox(Ljava / lang / Object;)Z
// 21:ireturn
// 22:nop
// 23:nop
// 24:nop
// 25:nop
// 26:nop
// 27:nop
// 28:nop
// 29:nop
// 30:athrow
//局部变量表:
//开始时间槽名签名
// 0 22 0此游戏
// 0 22 1 arg1类
}

这是了解 instanceOf 方法的行为。


I have a class called Video and an object of this class (here called "b")

This is the test case:

log.debug b
log.debug b.class
log.debug (b.class == Video)
log.debug (b instanceof Video)
log.debug (b.instanceOf(Video))
log.debug (b in Video)

the output is:

DEBUG NavLinkHelperTagLib  - product.content.Video : 4352
DEBUG NavLinkHelperTagLib  - class product.content.Video
DEBUG NavLinkHelperTagLib  - true
DEBUG NavLinkHelperTagLib  - false
DEBUG NavLinkHelperTagLib  - true
DEBUG NavLinkHelperTagLib  - false

Can somebody tell me why the instanceof fails?!

EDIT:

I have the feeling that this is GORM related. Here are my donain classes:

abstract class Product implements Serializable {

    static hasMany = [additionalContents:AdditionalContent]
}

abstract class AdditionalContent implements Serializable{
    static belongsTo = [product:Product]

}

class Video extends AdditionalContent {
}

I think the problem here is lazy loading? But when this is the proxy/lazy problem then normally I would see something like Video$$javaassist when calling .getClass() but I am not seeing this...

解决方案

This is Hibernate / Gorm related. Your domain object is most likely proxied by Hibernate. Therefore you cannot use Java instanceOf.

The Grails team is aware of that problem and therefore they introduce the instanceOf() method on every Grails domain object. The following quote is from the Grails reference.

You can protect yourself to a degree from this problem by using the instanceOf method by GORM:

def person = Person.get(1)

assert Pet.list()[0].instanceOf(Dog)

Therefore only compare domain classes by using instanceOf() method.

In order to see how instanceOf is implemented you need to decompile your Video domain class by using JD-GUI.

In a Grails 3 application this is implemented by the trait GormEntity.

@Traits.TraitBridge(traitClass=GormEntity.class, desc="(Ljava/lang/Class;)Z")
  public boolean instanceOf(Class arg1)
  {
    // Byte code:
    //   0: invokestatic 88 ch/silviowangler/zscsupporter/Game:$getCallSiteArray    ()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
    //   3: astore_2
    //   4: aload_2
    //   5: ldc_w 548
    //   8: aaload
    //   9: ldc -104
    //   11: aload_0
    //   12: aload_1
    //   13: invokeinterface 209 4 0
    //   18: invokestatic 182   org/codehaus/groovy/runtime/typehandling/DefaultTypeTransformation:booleanUnbox (Ljava/lang/Object;)Z
    //   21: ireturn
    //   22: nop
    //   23: nop
    //   24: nop
    //   25: nop
    //   26: nop
    //   27: nop
    //   28: nop
    //   29: nop
    //   30: athrow
    // Local variable table:
    //   start  length  slot    name    signature
    //   0  22  0   this    Game
    //   0  22  1   arg1    Class
  }

This is the only way to understand how the instanceOf method behaves.

这篇关于java / groovy / grails instanceof失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆