i am new to groovy language. I was wondering if there will be anyone who will be able to help me understand the below code.

static analyzeAPI(pkgname) {

    def refs = Yaml.load(new File(output_base + "/${pkgname}_PackageRefs.yml").text)
    refs.Classes.each { 
      analyzeClassRef(pkgname, it.name)


   static analyzeClassRef(pkgname, name) { 
    println "=== Analyze class ${pkgname} ${name}"
    def filename = pkgname ? "${pkgname}/${name}.xml" : "${name}.xml"
    def page = readXml(filename)

    assert page.head.title == "${name} | Android Developers"

Gratly appreciate a reply
thanks you

Recommended Answers

All 4 Replies


Sorry I only saw this now.

Let's take it line by line:

  1. create a static method called analyAPI that takes a single parameter, pkgname. In Groovy, you do not have to specify the variable type, just the name, very much like in JavaScript.

  2. Define a variable called refs that uses a static method load in the Yaml object to load and parses a file as text. This will load the Yaml script and populate the variable called refs with all the information from the script.

  3. The variable refs will contain a reference to all the Classes and it will be a collection. Using refs.Classes.each allows you to loop through all the classes in this collection.

  4. Using a static method caled analyzeClassRef, parse the package name and the class name. The "it" refers to the current class being served by the "each" iterator. The "it" is a convenient method for getting hold of whatever is being iterated over and is very handy in Groovy.

  5. Declare our static method and specify the parameters. Again, note the abscence of types.

  6. Println is a method / function in Groovy that will print something to the console. Of special note is the use of "" quotes, that allow you to specify placeholders in the string that will be replaced by the content of the specified variables when the line is printed. For example, this string will contain the String representation of pgkname and name when printed.

  7. Define a variable called filename, and, using standard ternary logic, choose to use either the package name + name combo or just the name combo. Again, note how ${...} is used to refer to the string representation of a variable's content.

  8. Define a variable called page that contains the result of calling the readXml method.

  9. Assert, just like in Java, the title of the head of the page variable. Groovy is object orientated, just like Java, so page, for example, contains an object called head which, in turn, contains a property called title.

I hope that helps :)

commented: Very helpful, thanks +15

thank you Ewald good explanation!

My pleasure, I hope it was of some help :)

Hi Ewald

what does the =~ mean in groovy.
below is the code

  static propertyPattern = ~/@property\s*\([\w, =]+\)\s*(\w+)\s*(\*?)\w*/

def match = decl =~ propertyPattern

And also the property Pattern what does ([\w, =]+) match too?

appreciate a reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.