trouble finding name of sub-classes

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble finding name of sub-classes

aeischeid
I use some class inheritance in my domain model and use default table-per-hierarchy method.

I found this in the mailing list which pretty much describes my problem - http://markmail.org/thread/pxiepf6dnb77gyrv

I used to be able to detect the sub-class with [object].class.name == "[nameOfSubClass]"

However this is no longer working. but so far I haven't been able to get anything to work.


sample code:


      parties.each {
            
            println it.email
//            These blow up...
//            AnimalOwner ao = new AnimalOwner()
//            if(it.instanceOf(ao)) println "yes"
//            else println "no"
//            if(it.isInstance(ao)) println "is Instance"
//            else println "isn't instance'"
            if(it instanceof AnimalOwner) println "yes Owner"
            else println "not Owner"
            if(it instanceof Party) println "yes Party"
            else println "not Party"
            println it
            println it['class'].name
            if(it.class.name == 'AnimalOwner'){it.investigations.each{itemList << it.id}}
        }
        println itemList
        println itemList.size()


output is:


not Owner
yes Party
TestOwner
Party_$$_javassist_53
not Owner
yes Party
TestVet
Party_$$_javassist_53
[]
0

errors when trying to use instanceOf() and isInstance() methods:

<strong>Message:</strong> No such property: instanceOf for class: AnimalOwner 
<strong>Message:</strong> No such property: isInstance for class: AnimalOwner
 
Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Dave Klein-2
This may not be much help, but would it be possible, and perhaps beneficial in other ways, to redesign so that external code doesn't need to know what concrete type it is working with?  Perhaps some logic that is currently outside the class can be moved inside of it.

Just a thought...
Dave
Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

aeischeid
Not a bad thought Dave. I might go that route in the end.

A couple notes for anyone viewing this in the future:

if I change my previous sample code to:

            if(it.class.isInstance(AnimalOwner)) println "is Instance"
            else println "isn't instance"
or

            if(it.class.isInstance(AnimalOwner.class)) println "is Instance"
            else println "isn't instance"

I no longer get a MissingMethod error, but i still get "isn't instance" 

On Fri, Aug 14, 2009 at 1:39 PM, Dave Klein <[hidden email]> wrote:
This may not be much help, but would it be possible, and perhaps beneficial in other ways, to redesign so that external code doesn't need to know what concrete type it is working with?  Perhaps some logic that is currently outside the class can be moved inside of it.

Just a thought...
Dave

Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Jeff Scott Brown
In reply to this post by aeischeid
On Fri, Aug 14, 2009 at 1:05 PM, Aaron Eischeid<[hidden email]> wrote:

> I use some class inheritance in my domain model and use default
> table-per-hierarchy method.
> I found this in the mailing list which pretty much describes my problem
> - http://markmail.org/thread/pxiepf6dnb77gyrv
>
> I used to be able to detect the sub-class with [object].class.name ==
> "[nameOfSubClass]"
> However this is no longer working. but so far I haven't been able to get
> anything to work.
>
> sample code:
>
>       parties.each {
>
>             println it.email
> //            These blow up...
> //            AnimalOwner ao = new AnimalOwner()
> //            if(it.instanceOf(ao)) println "yes"
> //            else println "no"
> //            if(it.isInstance(ao)) println "is Instance"
> //            else println "isn't instance'"

I am not sure what you are really trying to do but if you are trying
to figure out if "it" is an AnimalOwner, you should do this...

if(it instanceof AnimalOwner) {
  println 'yes'
} else {
  println 'no'
}

Does that help?



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Jeff Scott Brown
On Fri, Aug 14, 2009 at 4:33 PM, Jeff Brown<[hidden email]> wrote:

> On Fri, Aug 14, 2009 at 1:05 PM, Aaron Eischeid<[hidden email]> wrote:
>> I use some class inheritance in my domain model and use default
>> table-per-hierarchy method.
>> I found this in the mailing list which pretty much describes my problem
>> - http://markmail.org/thread/pxiepf6dnb77gyrv
>>
>> I used to be able to detect the sub-class with [object].class.name ==
>> "[nameOfSubClass]"
>> However this is no longer working. but so far I haven't been able to get
>> anything to work.
>>
>> sample code:
>>
>>       parties.each {
>>
>>             println it.email
>> //            These blow up...
>> //            AnimalOwner ao = new AnimalOwner()
>> //            if(it.instanceOf(ao)) println "yes"
>> //            else println "no"
>> //            if(it.isInstance(ao)) println "is Instance"
>> //            else println "isn't instance'"
>
> I am not sure what you are really trying to do but if you are trying
> to figure out if "it" is an AnimalOwner, you should do this...
>
> if(it instanceof AnimalOwner) {
>  println 'yes'
> } else {
>  println 'no'
> }
>


Or if you want to rule out subclasses...

if(it.class == AnimalOwner) {
  println 'yes'
} else {
  println 'no'
}

Again, does that help?



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

aeischeid
Hey Jeff,

I am trying to determine the type of a subclass 

class Party
class AnimalOwner extends Party
class Veterinarian extends Party 
etc...

so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...

neither of your suggestions work unfortunately. Seems to be some weird Hibernate black magic going on...

On Fri, Aug 14, 2009 at 4:35 PM, Jeff Brown <[hidden email]> wrote:
On Fri, Aug 14, 2009 at 4:33 PM, Jeff Brown<[hidden email]> wrote:
> On Fri, Aug 14, 2009 at 1:05 PM, Aaron Eischeid<[hidden email]> wrote:
>> I use some class inheritance in my domain model and use default
>> table-per-hierarchy method.
>> I found this in the mailing list which pretty much describes my problem
>> - http://markmail.org/thread/pxiepf6dnb77gyrv
>>
>> I used to be able to detect the sub-class with [object].class.name ==
>> "[nameOfSubClass]"
>> However this is no longer working. but so far I haven't been able to get
>> anything to work.
>>
>> sample code:
>>
>>       parties.each {
>>
>>             println it.email
>> //            These blow up...
>> //            AnimalOwner ao = new AnimalOwner()
>> //            if(it.instanceOf(ao)) println "yes"
>> //            else println "no"
>> //            if(it.isInstance(ao)) println "is Instance"
>> //            else println "isn't instance'"
>
> I am not sure what you are really trying to do but if you are trying
> to figure out if "it" is an AnimalOwner, you should do this...
>
> if(it instanceof AnimalOwner) {
>  println 'yes'
> } else {
>  println 'no'
> }
>


Or if you want to rule out subclasses...

if(it.class == AnimalOwner) {
 println 'yes'
} else {
 println 'no'
}

Again, does that help?



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Fred Carter
Thus quoth Aaron Eischeid (~ 8/14/2009 3:12 PM ~)...
Hey Jeff,

I am trying to determine the type of a subclass�

class Party
class AnimalOwner extends Party
class Veterinarian extends Party�
etc...

so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...

neither of your suggestions work unfortunately. Seems to be some weird Hibernate black magic going on...
if (it instanceof Party) {
    if (it instanceof AnimalOwner) {
        println "It owns animals -- Party & AnimalOwner"
    } else if (it instanceof Veterinarian) {
        println "It fixes animals -- Party & Veterinarian"
    } // .. test for other animal related parties...
} else {
    println "It is not a Party"
}

On Fri, Aug 14, 2009 at 4:35 PM, Jeff Brown <[hidden email]> wrote:
On Fri, Aug 14, 2009 at 4:33 PM, Jeff Brown<[hidden email]> wrote:
> On Fri, Aug 14, 2009 at 1:05 PM, Aaron Eischeid<[hidden email]> wrote:
>> I use some class�inheritance�in my domain model and use default
>> table-per-hierarchy method.
>> I found this in the mailing list which pretty much describes my problem
>> -�http://markmail.org/thread/pxiepf6dnb77gyrv
>>
>> I used to be able to detect the sub-class with [object].class.name ==
>> "[nameOfSubClass]"
>> However this is no longer working. but so far I haven't been able to get
>> anything to work.
>>
>> sample code:
>>
>> �� � �parties.each {
>>
>> �� � � � � �println it.email
>> // � � � � � �These blow up...
>> // � � � � � �AnimalOwner ao = new AnimalOwner()
>> // � � � � � �if(it.instanceOf(ao)) println "yes"
>> // � � � � � �else println "no"
>> // � � � � � �if(it.isInstance(ao)) println "is Instance"
>> // � � � � � �else println "isn't instance'"
>
> I am not sure what you are really trying to do but if you are trying
> to figure out if "it" is an AnimalOwner, you should do this...
>
> if(it instanceof AnimalOwner) {
> �println 'yes'
> } else {
> �println 'no'
> }
>


Or if you want to rule out subclasses...

if(it.class == AnimalOwner) {
�println 'yes'
} else {
�println 'no'
}

Again, does that help?



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

� �http://xircles.codehaus.org/manage_email





-- 
Fred Carter / AmberPoint, Inc.

[hidden email]
tel:+1.510.433.6525 fax:+1.510.663.6301
Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Jeff Scott Brown
In reply to this post by aeischeid
On Fri, Aug 14, 2009 at 5:12 PM, Aaron Eischeid<[hidden email]> wrote:

> Hey Jeff,
> I am trying to determine the type of a subclass
> class Party
> class AnimalOwner extends Party
> class Veterinarian extends Party
> etc...
> so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...
> neither of your suggestions work unfortunately. Seems to be some weird
> Hibernate black magic going on...
>

That doesn't make any sense to me.  Maybe I am misunderstanding.  Look
at this...

class Party {
    String name
}

class AnimalOwner extends Party {
}

class Veterinarian extends Party {
}

With those domain classes, I can do something like this...

3.times {
    new Veterinarian(name: "Vet #${it}").save()
}

5.times {
    new AnimalOwner(name: "Animal Owner #${it}").save()
}

def parties = Party.list()

def vetCount = 0
def aoCount = 0
parties.each {
    if(it instanceof AnimalOwner) {
        aoCount++
    } else if(it instanceof Veterinarian){
        vetCount++
    }
}

println "Number of Vets: ${vetCount}"
println "Number Of Animal Owners: ${aoCount}"

That outputs the following...

Number of Vets: 3
Number Of Animal Owners: 5




jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Fred Carter
Thus quoth Jeff Brown (~ 8/14/2009 4:32 PM ~)...
On Fri, Aug 14, 2009 at 5:12 PM, Aaron Eischeid[hidden email] wrote:
  
Hey Jeff,
I am trying to determine the type of a subclass
class Party
class AnimalOwner extends Party
class Veterinarian extends Party
etc...
so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...
neither of your suggestions work unfortunately. Seems to be some weird
Hibernate black magic going on...

    

That doesn't make any sense to me.  Maybe I am misunderstanding.  Look
at this...

class Party {
    String name
}

class AnimalOwner extends Party {
}

class Veterinarian extends Party {
}

With those domain classes, I can do something like this...

3.times {
    new Veterinarian(name: "Vet #${it}").save()
}

5.times {
    new AnimalOwner(name: "Animal Owner #${it}").save()
}

def parties = Party.list()

def vetCount = 0
def aoCount = 0
parties.each {
    if(it instanceof AnimalOwner) {
        aoCount++
    } else if(it instanceof Veterinarian){
        vetCount++
    }
}

println "Number of Vets: ${vetCount}"
println "Number Of Animal Owners: ${aoCount}"

That outputs the following...

Number of Vets: 3
Number Of Animal Owners: 5
  
I guess I don't understand what's not understood.
    3.times ... create Vet --> Create 3 veterinarians --> Number of Vets: 3
    5.times ... create AO --> Create 5 animal owners -->  Number of AnimalOwners: 5
Seems like the numbers match.  If you'd printed it, Number of parties: ${parties.size()} you would have seen 8.  As one might expected.

Now, If you go look at the table structure in the database (assuming these are domain classes), you'll see a Party table which has 8 rows (assuming the default setting).  Each of the 8 rows will have either AnimalOwner bits or Veterinarian bits.  There's a subclass-ish setting to determine if there's one-table-per-hierarchy or not, which defaults to one-table-per-hierarchy.  (In this case, there are no real bits different, of course, given the code provided, but presumably, in real life, there's a distinction.)



jb

  


-- 
Fred Carter / AmberPoint, Inc.

[hidden email]
tel:+1.510.433.6525 fax:+1.510.663.6301
Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Fred Carter
In reply to this post by Jeff Scott Brown
Oops -- sorry -- misappropriated the response. You message was explanatory.  I misunderstood the context.  Again, apologies...

Thus quoth Jeff Brown (~ 8/14/2009 4:32 PM ~)...
On Fri, Aug 14, 2009 at 5:12 PM, Aaron Eischeid[hidden email] wrote:
  
Hey Jeff,
I am trying to determine the type of a subclass
class Party
class AnimalOwner extends Party
class Veterinarian extends Party
etc...
so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...
neither of your suggestions work unfortunately. Seems to be some weird
Hibernate black magic going on...

    

That doesn't make any sense to me.  Maybe I am misunderstanding.  Look
at this...

class Party {
    String name
}

class AnimalOwner extends Party {
}

class Veterinarian extends Party {
}

With those domain classes, I can do something like this...

3.times {
    new Veterinarian(name: "Vet #${it}").save()
}

5.times {
    new AnimalOwner(name: "Animal Owner #${it}").save()
}

def parties = Party.list()

def vetCount = 0
def aoCount = 0
parties.each {
    if(it instanceof AnimalOwner) {
        aoCount++
    } else if(it instanceof Veterinarian){
        vetCount++
    }
}

println "Number of Vets: ${vetCount}"
println "Number Of Animal Owners: ${aoCount}"

That outputs the following...

Number of Vets: 3
Number Of Animal Owners: 5




jb

  


-- 
Fred Carter / AmberPoint, Inc.

[hidden email]
tel:+1.510.433.6525 fax:+1.510.663.6301
Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

aeischeid
Jeff, 

That is totally the behavior I would expect, if you look at the sample code in my first entry of this thread you will see where I was doing if(it instanceof AnimalOwner) and that wasn't working. if(it instanceof Party) was working, but none of the subclasses could be detected. I don't get why but it was hinted at by Graeme in the older thread I linked to. something about Hibernate proxies...

I do have some eager fetching enabled on these classes, do you think that could that be effecting things? 

My next step may be to try to reproduce in a sample app.

-Aaron

On Fri, Aug 14, 2009 at 8:32 PM, Fred Carter <[hidden email]> wrote:
Oops -- sorry -- misappropriated the response. You message was explanatory.  I misunderstood the context.  Again, apologies...


Thus quoth Jeff Brown (~ 8/14/2009 4:32 PM ~)...
On Fri, Aug 14, 2009 at 5:12 PM, Aaron Eischeid[hidden email] wrote:
  
Hey Jeff,
I am trying to determine the type of a subclass
class Party
class AnimalOwner extends Party
class Veterinarian extends Party
etc...
so I get a Party and I want to tell if it is a Vet, AnimalOwner or other...
neither of your suggestions work unfortunately. Seems to be some weird
Hibernate black magic going on...

    
That doesn't make any sense to me.  Maybe I am misunderstanding.  Look
at this...

class Party {
    String name
}

class AnimalOwner extends Party {
}

class Veterinarian extends Party {
}

With those domain classes, I can do something like this...

3.times {
    new Veterinarian(name: "Vet #${it}").save()
}

5.times {
    new AnimalOwner(name: "Animal Owner #${it}").save()
}

def parties = Party.list()

def vetCount = 0
def aoCount = 0
parties.each {
    if(it instanceof AnimalOwner) {
        aoCount++
    } else if(it instanceof Veterinarian){
        vetCount++
    }
}

println "Number of Vets: ${vetCount}"
println "Number Of Animal Owners: ${aoCount}"

That outputs the following...

Number of Vets: 3
Number Of Animal Owners: 5




jb

  


-- 
Fred Carter / AmberPoint, Inc.

[hidden email] tel:+1.510.433.6525 fax:+1.510.663.6301

Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

Jeff Scott Brown
On Sat, Aug 15, 2009 at 10:57 AM, Aaron Eischeid<[hidden email]> wrote:

> Jeff,
> That is totally the behavior I would expect, if you look at the sample code
> in my first entry of this thread you will see where I was doing if(it
> instanceof AnimalOwner) and that wasn't working. if(it instanceof Party) was
> working, but none of the subclasses could be detected. I don't get why but
> it was hinted at by Graeme in the older thread I linked to. something about
> Hibernate proxies...
> I do have some eager fetching enabled on these classes, do you think that
> could that be effecting things?
> My next step may be to try to reproduce in a sample app.
> -Aaron
>

If you can reproduce it in a sample app make that app available
somewhere (github would be perfect), I will be happy to take a look.




jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: trouble finding name of sub-classes

houbie
In reply to this post by aeischeid

grailsOnUbuntu wrote
I do have some eager fetching enabled on these classes, do you think that
could that be effecting things?
instanceof doesn't work with Hibernate proxies (at the time the proxy is created, the actual class is still unknown). So you can only use instanceof with eager fetching.

Grtz,
Ivo