[jira] [Comment Edited] (GROOVY-8350) Class expressions with and without ".class"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Comment Edited] (GROOVY-8350) Class expressions with and without ".class"

JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/GROOVY-8350?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16204657#comment-16204657 ]

Eric Milles edited comment on GROOVY-8350 at 3/12/18 7:33 PM:
--------------------------------------------------------------

I did some additional research on this one and answered a lot of my own questions. Would it be possible to update the language docs to reflect this (or point me to the section where it is described)?

1. Class literals (with and without ".class") and class instances are interchangeable (at least in dynamic Groovy); they all provide access to static members of the referent type
 {{System.getProperties()}}, {{System.class.getProperties()}}, and {{def systemClass = System _or any other source of class instance_; systemClass.getProperties()}} all execute fine.

2. Point (1) extends to method pointers as well
 {{System.&currentTimeMillis}}, {{System.class.&currentTimeMillis}}, and {{def systemClass = ...; systemClass.&currentTimeMillis}} all produce a usable closure.

3. Point (1) extends to property syntax as well
 {{System.properties}}, {{System.class.properties}}, and {{def systemClass = ...; systemClass.properties}} should all call {{System.getProperties()}}

4. Point (1) extends to Class members
 {{System.getCanonicalName()}}, {{System.class.getCanonicalName()}} and {{def systemClass = ...; systemClass.getCanonicalName()}} all return "java.lang.System"

5. Class static methods can be called directly from static initializers and methods
{code:java}
  class C {
    static {
      def name = getCanonicalName() // equivalent to C.getCanonicalName()
    }
  }
{code}
Therefore, I can conclude that {{System.getClass()}} is actually {{(System.class).getClass()}}, which is an instance of {{Class<Class>}}.

 

BONUS: {{this}} in a static scope is the same as using a class literal of the enclosing type.  Sometimes it is required; like the call to getCanonicalName() above – to use property notation, this is required:
{code:groovy}
class C {
  static {
    def name = this.canonicalName // equivalent to C.getCanonicalName()
  }
}
{code}


was (Author: emilles):
I did some additional research on this one and answered a lot of my own questions. Would it be possible to update the language docs to reflect this (or point me to the section where it is described)?

1. Class literals (with and without ".class") and class instances are interchangeable (at least in dynamic Groovy); they all provide access to static members of the referent type
 {{System.getProperties()}}, {{System.class.getProperties()}}, and {{def systemClass = System _or any other source of class instance_; systemClass.getProperties()}} all execute fine.

2. Point (1) extends to method pointers as well
 {{System.&currentTimeMillis}}, {{System.class.&currentTimeMillis}}, and {{def systemClass = ...; systemClass.&currentTimeMillis}} all produce a usable closure.

3. Point (1) extends to property syntax as well
 {{System.properties}}, {{System.class.properties}}, and {{def systemClass = ...; systemClass.properties}} should all call {{System.getProperties()}}

4. Point (1) extends to Class members
 {{System.getCanonicalName()}}, {{System.class.getCanonicalName()}} and {{def systemClass = ...; systemClass.getCanonicalName()}} all return "java.lang.System"

5. Class static methods can be called directly from static initializers and methods
{code:java}
  class C {
    static {
      def name = getCanonicalName() // equivalent to C.getCanonicalName()
    }
  }
{code}
Therefore, I can conclude that {{System.getClass()}} is actually {{(System.class).getClass()}}, which is an instance of {{Class<Class>}}.

 

BONUS: {{this}} in a static scope is the same as using a class literal of the enclosing type.  Sometimes it is required; like the call to getCanonicalName() above – to use property notation, this is required:
{code:groovy}
class C {
  static {
    def name = this.canonicalName
  }
}
{code}

> Class expressions with and without ".class"
> -------------------------------------------
>
>                 Key: GROOVY-8350
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8350
>             Project: Groovy
>          Issue Type: Documentation
>            Reporter: Eric Milles
>            Priority: Minor
>
> This seems like a bug (but it could just be a clarification in the documentation).  And is seems related to GROOVY-8152, GROOVY-8153, and GROOVY-8154.
> {{System.currentTimeMillis()}} is your typical static call.
> {{System.getClass().currentTimeMillis()}} results in a MissingMethodException, as it should.
> However, {{System.class.currentTimeMillis()}} and {{def sys = System.class; sys.currentTimeMillis()}} both compile and execute fine.  I would expect a MissingMethodException for these as well.
> Is there some trick where methodMissing on class instance can find the static method or something?  My Java experience says no {{currentTimeMillis}} method on {{Class}}.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)