Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

15 March 2018

How to set groovysdk on mac in IntelliJ IDEA

The solution works for:
Groovy 2.4.12
IntelliJ IDEA 2017.2.6
macOS 10.12.6
Homebrew 1.5.10
It should work with other versions too.


STORY:
    I tried to Config groovy SDK to use in IntelliJ IDEA ... but you need specific path which is .. something like that: /usr/local/Cellar/groovy/x.y.z/ libexec but I couldn't see usr folder. 
As it turns out you need to use a magical shortcut to enable a view of hidden files/folder in Finder on Mac.
How?

SOLUTION:
  1. Go to Project Structure
  2. Go to Global Libraries
  3. Press +
  4. Select Java 
  5. In Finder, press  ⌘⇧. ( Command key, SHIFT key  dot key a the same time ) to enable 'view hidden files' mode
  6. Go to top level (MachintoshHD)
  7. go to /usr/local/Cellar/groovy/x.y.z/libexec and press open if you don't see usr folder... perform step 5 (and ensure you have all privileges required) 
  8. Press create
  9. Done :)

20 May 2014

How to setup documentation for groovy in Netbeans 8

Solution for Netbeans 8.x ,Groovy 2.x (It will likely works with other version too)
Are you trying setup groovy documentation for Netbeans but you see this message:
"No groovy documentation found"
Well, this is how to do it correctly:
How add groovy documentation to Netbeans ?
  1. Download Groovy documentation  from codehouse.org page (http://groovy.codehaus.org/Download) (if you download groovy as bundle,they you are ready have in installation 
  2. Unzip to place where you keep documentation offline
  3. Go to documentation folder and select groovy-2.x.x\html
Done. :)

14 December 2013

How to display number of lines in file in Groovy

This is code to display number of lines in file in Groovy
CODE:

package dms.pastor.lab.groovy

class Stats {
    static main(args) {
        def num = 0;
        new File("C:\\Dom\\workspaces\\sts\\lab\\LabGroovy\\src\\dms\\pastor\\lab\\groovy\\Stats.groovy").eachLine { num++ }
        println "File has " + num + " lines.";
    }
}

based partly on solution from:
http://robbyoconnor.blogspot.co.uk/2008/04/groovy-way-to-count-number-of-lines.html

10 November 2013

How to create groovy project (without grails) in Netbeans ?

Solution for NetBeans 7.4

STORY
You want create a groovy project and you go to New Project ,select Groovy and then .... you can create Grails project...but you can't create Groovy project.

Bit weird,but what to do ?

SOLUTION:
There is a funny walk-around to this problem.

  1. Press new project
  2. Go to Examples
  3. Select Groovy
  4. Select NB Project Generator and Press next 
  5. Give a name for project
  6. DONE :)

Remember! You still need GroovyConsole to run your application :(

7 November 2013

How to catch assert exception in Groovy?

I never used assert in Java,but I found assert can be useful is Groovy,but ... here is story:
Solution for Groovy 2.x (I guess ,it works on all version of Groovy.I just didn't test it.

STORY:

I started from catch assert with Exception,but ...as usual ... it was a bad ideas as,when I run this code:

try{
assert 3>4
}catch(Exception e){
println(e.getMessage())
}

then ,I was welcome with error:

Exception thrown
Nov 07, 2013 10:04:05 AM org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

Assertion failed:

assert 3>4
        |
        false

SOLUTION:
Quick look to Groovy docs and I found that I should use AssertionError instead:

try{
assert 3>4
}catch(
AssertionError  e){
println(e.getMessage())
}


CONCLUSION:
Due dynamic nature of Groovy it is good to check Exceptions chapter on Groovy:
http://groovy.codehaus.org/JN3035-Exceptions

as they handle things differently.
(Groovy 2.x has amazing multi catch option!

6 May 2011

How to parse a date in Groovy ?

Solution based on Groovy 1.8.0

def dateFormat = 'dd-MM-yyyy' // set format (you can do this when you parse as well)
Date dateExample = Date.parse(dateFormat,dateStart) // parse date
println(dateExample) // print on console ;)

More about how to format date you can find here: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

More about Date class in groovy you can find here :  http://groovy.codehaus.org/JN0545-Dates

dynamically add methods with parameters to a class in groovy

Solution based on Groovy 1.8.0

Groovy dynamic edition of Java.... scary thing, but can do crazy things.

If you are mess maker and you depend on your mother IDE who clean mess after you (using quickfix and other helpful stuff) , then Groovy you are orphan. Your mess, you problem,IDE don't care.

Back to crazyness of Groovy.One of them is adding methods dynamically to class.You can find some examples on net with one parameter,but some people would love to know how to add 2 and more parameters.

Let's say you have Date class which has lots of funky method,but you wish to add another one to check is specific date is in range of time (lets say between 01-06-2011 and 01-11-2011 ).
How to add dynamically add methods with parameters to a class in groovy ?

NAMEofClass.metaClass.nameOfMethord = { Type parameter1, Type parameter2, Type parameter3 ->
method that will do marvellous and cool stuff
}

Date.metaClass.between = { String dateStart,String dateEnd,Date date ->
 def dateFormat = 'dd-MM-yyyy'
Date start = Date.parse(dateFormat,dateStart)
Date end = Date.parse(dateFormat,dateEnd)

if( (start > delegate).equals(-1) && (end > delegate).equals(1)){
return true
}else {
return false
  }

}


and then in your class you just need ....

boolean isInRange(Date date){
  return date.between(" 01-06-2011 ","  01-11-2011  ",date))
}

What to do when i must add dynamically added method of one class in other ????

Put "dynamically added method of Source Class( Date)" intro costructor of class where you will use Source class method.