Parham Doustdar
Parham Doustdar

Reputation: 2039

What does the @sign do?

I have seen the at (@) sign in Groovy files and I don't know if it's a Groovy or Java thing. I have tried to search on Google, Bing, and DuckDuckGo for the mystery at sign, but I haven't found anything. Can anyone please give me a resource to know more about what this operator does?

Upvotes: 9

Views: 12970

Answers (4)

Rhysyngsun
Rhysyngsun

Reputation: 951

It can also be used to access attributes when parsing XML using Groovy's XmlSlurper:

def xml = '''<results><result index="1"/></results>'''
def results = new XmlSlurper().parseText(xml)
def index = results.result[0][email protected]() // prints "1"

http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper

Upvotes: 2

tim_yates
tim_yates

Reputation: 171054

As well as being a sign for an annotation, it's the Groovy Field operator

In Groovy, calling object.field calls the getField method (if one exists). If you actually want a direct reference to the field itself, you use @, ie:

class Test {
  String name = 'tim'

  String getName() {
    "Name: $name"
  }
}

def t = new Test()
println t.name   // prints "Name: tim"
println t.@name  // prints "tim"

Upvotes: 9

Hemant Metalia
Hemant Metalia

Reputation: 30638

'@' is an annotations in java/ Groovy look at the demo :Example with code

Java 5 and above supports the use of annotations to include metadata within programs. Groovy 1.1 and above also supports such annotations.

  • Annotations are used to provide information to tools and libraries.

  • They allow a declarative style of providing metadata information and allow it to be stored directly in the source code.

  • Such information would need to otherwise be provided using non-declarative means or using external files.

Upvotes: 2

Josh
Josh

Reputation: 12566

It's a Java annotation. Read more at that link.

Upvotes: 9

Related Questions