You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Guy Matz <gu...@gmail.com> on 2016/04/07 17:33:48 UTC

Calling a static method by packagename.method

Hello!  I have some static methods defined and am able to access them after
I import, e,.g.
import static org.matz.utils.jira.*

With that import I can call the methods, e.g. createTicket, but I would
like to be able to call it as *jira*.createTicket, as is possible with the
Math package, e.g.

import static java.lang.Math.*
println "PI is ${PI} (${Math.PI})"

my method declaration in jira.groovy is (including the namespace):
package org.matz.jenkins

def static void createTicket(String changelog, String summary, Map config)
{ ... }

Does anyone know what I need to do to either my definitions, or import, or
something else to be able to refer to the createTicket method as
jira.createTicket?  I would like to be able to do this to avoid name
collisions as well as to be able to make explicit which "class" I'm calling
the method on.

Thanks!
Guy

Re: Calling a static method by packagename.method

Posted by Jochen Theodorou <bl...@gmx.org>.

On 07.04.2016 19:15, emmanuel r wrote:
> You can do this:
>
> import static org.matz.utils.Jira.*
> import org.matz.utils.Jira
>
> Jira.createTicket()
> createTicket()

or if a small case jira is wanted:

import org.matz.utils.Jira as jira

> Note: Methods are /always/ in a class. Most likely Groovy is creating a
> Script class for you.

yepp, always! In case of a script, the name is generated from the script 
name.

bye Jochen


Re: Calling a static method by packagename.method

Posted by emmanuel r <go...@gmail.com>.
You can do this:

import static org.matz.utils.Jira.*
import org.matz.utils.Jira

Jira.createTicket()
createTicket()

Note: Methods are *always* in a class. Most likely Groovy is creating a
Script class for you.
On Apr 7, 2016 12:16 PM, "Guy Matz" <gu...@gmail.com> wrote:

OK!  Putting the static methods in classes as described above allows me to
call Jira,createTicket, but not createTicket.  Should I be able to do
that?  A point to a relevant doc would be fine!  :-)

Thanks again, all!

On Thu, Apr 7, 2016 at 12:01 PM, Guy Matz <gu...@gmail.com> wrote:

> Thanks for the reply!  The thing is, though, that my static methods are
> not within a class . . .  does that make a difference?
> One other thing: When I try to call createTicket() as jira.createTicket()
> I get:
> groovy:000> jira.createTicket()
> Unknown property: jira
>
> Any other thoughts would be greatly appreciated!
>
> On Thu, Apr 7, 2016 at 11:47 AM, Winnebeck, Jason <
> Jason.Winnebeck@windstream.com> wrote:
>
>> Math is a class, not a package. So if you create a class jira (it really
>> should at least start with a capital in Groovy, so JIRA or Jira) with
>> static methods, you can access it like you do with Math. Note in your
>> example, “import static java.lang.Math.*” actually is allowing you to use
>> constants like PI without putting Math.PI in front – your import actually
>> does nothing because you use Math.PI and Math is part of java.lang, which
>> is a default import. Below you can see a proper way:
>>
>>
>>
>> package org.matz.utils
>>
>>
>>
>> class Jira{
>>
>>   static void createTicket() {}
>>
>> }
>>
>>
>>
>> And in your script:
>>
>>
>>
>> import org.matz.utils.* //Now you can reference any class in this package
>> by its name alone, including Jira
>>
>>
>>
>> Jira.createTicket()
>>
>>
>>
>> Jason
>>
>>
>>
>> *From:* Guy Matz [mailto:guymatz@gmail.com]
>> *Sent:* Thursday, April 07, 2016 11:34 AM
>> *To:* users@groovy.apache.org
>> *Subject:* Calling a static method by packagename.method
>>
>>
>>
>> Hello!  I have some static methods defined and am able to access them
>> after I import, e,.g.
>>
>> import static org.matz.utils.jira.*
>>
>>
>>
>> With that import I can call the methods, e.g. createTicket, but I would
>> like to be able to call it as *jira*.createTicket, as is possible with
>> the Math package, e.g.
>>
>>
>>
>> import static java.lang.Math.*
>>
>> println "PI is ${PI} (${Math.PI})"
>>
>>
>>
>> my method declaration in jira.groovy is (including the namespace):
>>
>> package org.matz.jenkins
>>
>>
>>
>> def static void createTicket(String changelog, String summary, Map
>> config) { ... }
>>
>>
>>
>> Does anyone know what I need to do to either my definitions, or import,
>> or something else to be able to refer to the createTicket method as
>> jira.createTicket?  I would like to be able to do this to avoid name
>> collisions as well as to be able to make explicit which "class" I'm calling
>> the method on.
>>
>>
>>
>> Thanks!
>>
>> Guy
>>
>>
>> ------------------------------
>> This email message and any attachments are for the sole use of the
>> intended recipient(s). Any unauthorized review, use, disclosure or
>> distribution is prohibited. If you are not the intended recipient, please
>> contact the sender by reply email and destroy all copies of the original
>> message and any attachments.
>>
>
>

Re: Calling a static method by packagename.method

Posted by Guy Matz <gu...@gmail.com>.
OK!  Putting the static methods in classes as described above allows me to
call Jira,createTicket, but not createTicket.  Should I be able to do
that?  A point to a relevant doc would be fine!  :-)

Thanks again, all!

On Thu, Apr 7, 2016 at 12:01 PM, Guy Matz <gu...@gmail.com> wrote:

> Thanks for the reply!  The thing is, though, that my static methods are
> not within a class . . .  does that make a difference?
> One other thing: When I try to call createTicket() as jira.createTicket()
> I get:
> groovy:000> jira.createTicket()
> Unknown property: jira
>
> Any other thoughts would be greatly appreciated!
>
> On Thu, Apr 7, 2016 at 11:47 AM, Winnebeck, Jason <
> Jason.Winnebeck@windstream.com> wrote:
>
>> Math is a class, not a package. So if you create a class jira (it really
>> should at least start with a capital in Groovy, so JIRA or Jira) with
>> static methods, you can access it like you do with Math. Note in your
>> example, “import static java.lang.Math.*” actually is allowing you to use
>> constants like PI without putting Math.PI in front – your import actually
>> does nothing because you use Math.PI and Math is part of java.lang, which
>> is a default import. Below you can see a proper way:
>>
>>
>>
>> package org.matz.utils
>>
>>
>>
>> class Jira{
>>
>>   static void createTicket() {}
>>
>> }
>>
>>
>>
>> And in your script:
>>
>>
>>
>> import org.matz.utils.* //Now you can reference any class in this package
>> by its name alone, including Jira
>>
>>
>>
>> Jira.createTicket()
>>
>>
>>
>> Jason
>>
>>
>>
>> *From:* Guy Matz [mailto:guymatz@gmail.com]
>> *Sent:* Thursday, April 07, 2016 11:34 AM
>> *To:* users@groovy.apache.org
>> *Subject:* Calling a static method by packagename.method
>>
>>
>>
>> Hello!  I have some static methods defined and am able to access them
>> after I import, e,.g.
>>
>> import static org.matz.utils.jira.*
>>
>>
>>
>> With that import I can call the methods, e.g. createTicket, but I would
>> like to be able to call it as *jira*.createTicket, as is possible with
>> the Math package, e.g.
>>
>>
>>
>> import static java.lang.Math.*
>>
>> println "PI is ${PI} (${Math.PI})"
>>
>>
>>
>> my method declaration in jira.groovy is (including the namespace):
>>
>> package org.matz.jenkins
>>
>>
>>
>> def static void createTicket(String changelog, String summary, Map
>> config) { ... }
>>
>>
>>
>> Does anyone know what I need to do to either my definitions, or import,
>> or something else to be able to refer to the createTicket method as
>> jira.createTicket?  I would like to be able to do this to avoid name
>> collisions as well as to be able to make explicit which "class" I'm calling
>> the method on.
>>
>>
>>
>> Thanks!
>>
>> Guy
>>
>>
>> ------------------------------
>> This email message and any attachments are for the sole use of the
>> intended recipient(s). Any unauthorized review, use, disclosure or
>> distribution is prohibited. If you are not the intended recipient, please
>> contact the sender by reply email and destroy all copies of the original
>> message and any attachments.
>>
>
>

Re: Calling a static method by packagename.method

Posted by Guy Matz <gu...@gmail.com>.
Thanks for the reply!  The thing is, though, that my static methods are not
within a class . . .  does that make a difference?
One other thing: When I try to call createTicket() as jira.createTicket() I
get:
groovy:000> jira.createTicket()
Unknown property: jira

Any other thoughts would be greatly appreciated!

On Thu, Apr 7, 2016 at 11:47 AM, Winnebeck, Jason <
Jason.Winnebeck@windstream.com> wrote:

> Math is a class, not a package. So if you create a class jira (it really
> should at least start with a capital in Groovy, so JIRA or Jira) with
> static methods, you can access it like you do with Math. Note in your
> example, “import static java.lang.Math.*” actually is allowing you to use
> constants like PI without putting Math.PI in front – your import actually
> does nothing because you use Math.PI and Math is part of java.lang, which
> is a default import. Below you can see a proper way:
>
>
>
> package org.matz.utils
>
>
>
> class Jira{
>
>   static void createTicket() {}
>
> }
>
>
>
> And in your script:
>
>
>
> import org.matz.utils.* //Now you can reference any class in this package
> by its name alone, including Jira
>
>
>
> Jira.createTicket()
>
>
>
> Jason
>
>
>
> *From:* Guy Matz [mailto:guymatz@gmail.com]
> *Sent:* Thursday, April 07, 2016 11:34 AM
> *To:* users@groovy.apache.org
> *Subject:* Calling a static method by packagename.method
>
>
>
> Hello!  I have some static methods defined and am able to access them
> after I import, e,.g.
>
> import static org.matz.utils.jira.*
>
>
>
> With that import I can call the methods, e.g. createTicket, but I would
> like to be able to call it as *jira*.createTicket, as is possible with
> the Math package, e.g.
>
>
>
> import static java.lang.Math.*
>
> println "PI is ${PI} (${Math.PI})"
>
>
>
> my method declaration in jira.groovy is (including the namespace):
>
> package org.matz.jenkins
>
>
>
> def static void createTicket(String changelog, String summary, Map config)
> { ... }
>
>
>
> Does anyone know what I need to do to either my definitions, or import, or
> something else to be able to refer to the createTicket method as
> jira.createTicket?  I would like to be able to do this to avoid name
> collisions as well as to be able to make explicit which "class" I'm calling
> the method on.
>
>
>
> Thanks!
>
> Guy
>
>
> ------------------------------
> This email message and any attachments are for the sole use of the
> intended recipient(s). Any unauthorized review, use, disclosure or
> distribution is prohibited. If you are not the intended recipient, please
> contact the sender by reply email and destroy all copies of the original
> message and any attachments.
>

RE: Calling a static method by packagename.method

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
Math is a class, not a package. So if you create a class jira (it really should at least start with a capital in Groovy, so JIRA or Jira) with static methods, you can access it like you do with Math. Note in your example, “import static java.lang.Math.*” actually is allowing you to use constants like PI without putting Math.PI in front – your import actually does nothing because you use Math.PI and Math is part of java.lang, which is a default import. Below you can see a proper way:

package org.matz.utils

class Jira{
  static void createTicket() {}
}

And in your script:

import org.matz.utils.* //Now you can reference any class in this package by its name alone, including Jira

Jira.createTicket()

Jason

From: Guy Matz [mailto:guymatz@gmail.com]
Sent: Thursday, April 07, 2016 11:34 AM
To: users@groovy.apache.org
Subject: Calling a static method by packagename.method

Hello!  I have some static methods defined and am able to access them after I import, e,.g.
import static org.matz.utils.jira.*

With that import I can call the methods, e.g. createTicket, but I would like to be able to call it as jira.createTicket, as is possible with the Math package, e.g.

import static java.lang.Math.*
println "PI is ${PI} (${Math.PI})"

my method declaration in jira.groovy is (including the namespace):
package org.matz.jenkins

def static void createTicket(String changelog, String summary, Map config) { ... }

Does anyone know what I need to do to either my definitions, or import, or something else to be able to refer to the createTicket method as jira.createTicket?  I would like to be able to do this to avoid name collisions as well as to be able to make explicit which "class" I'm calling the method on.

Thanks!
Guy


----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.