Quantcast

class vs getClass()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

class vs getClass()

jay.pedersen

Hi,

Consider the following

def props = System.getProperties()
println props.getClass().name  // ==> "java.util.Properties'
println props.class                  // ==> null
for (prop in props) {
  println prop.getClass().name  // ==> 'java.util.Hashtable$Entry'
  println prop.class                  // ==> 'class java.util.Hashtable$Entry'
  break
}

My understanding, apparently faulty, was that getClass() and class
would be the same thing in all cases.   Basic JavaBeans property.

Why would I see null for props.class but not for props.getClass().name?

My environment is Groovy 1.6.3 on Windows 64 bit Vista machine.

I am not seeing the answer in the GroovyObject documentation,
don't see getClass() there at all.

Looks like getClass() comes from java.lang.Object (vs groovy.lang.GroovyObject),
but what of "class"?

-Jay



     

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class vs getClass()

Mike Dillon-4
begin Jay Pedersen quotation:
> Why would I see null for props.class but not for props.getClass().name?

If your object is a Map, then "class" gets looked up through
getProperty(). The safest way to get the class is to always call
getClass(), which should be safe unless something is delegating all
method calls to another object or hijacking invokeMethod() or something.

-md

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: class vs getClass()

Martin C. Martin-2
Another way to say it is:

In Java foo.bar and foo.bar() are field access and method call,
respectively.  They're simple operations with straightforward semantics.

In Groovy, they can be anything.  Any piece of code can modify the
meaning of any other object or class.  This is what allows builders,
GORM, etc.

Given a random object, you can use foo.@bar to access the "bar" field,
but AFAIK there's no syntax for bypassing the metaclass when calling a
method.  On the other hand, if someone overrides the getClass() method
on an object, it's probably their fault and not yours.  :)

Best,
Martin

Mike Dillon wrote:

> begin Jay Pedersen quotation:
>> Why would I see null for props.class but not for props.getClass().name?
>
> If your object is a Map, then "class" gets looked up through
> getProperty(). The safest way to get the class is to always call
> getClass(), which should be safe unless something is delegating all
> method calls to another object or hijacking invokeMethod() or something.
>
> -md
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

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

    http://xircles.codehaus.org/manage_email


Loading...