This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 219002 - Groovy script should have "Play" icon within Java project
Summary: Groovy script should have "Play" icon within Java project
Status: NEW
Alias: None
Product: groovy
Classification: Unclassified
Component: Code (show other bugs)
Version: 7.3
Hardware: Macintosh (x86) Mac OS X
: P3 normal (vote)
Assignee: Martin Janicek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-25 15:32 UTC by michal.owsiak
Modified: 2014-01-20 14:36 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Groovy vs. Java (7.42 KB, image/png)
2012-09-25 15:32 UTC, michal.owsiak
Details

Note You need to log in before you can comment on or make changes to this bug.
Description michal.owsiak 2012-09-25 15:32:51 UTC
Created attachment 124888 [details]
Groovy vs. Java

Groovy scripts doesn't have "Play" icon even though they can be executed the same way as Java classes.
Comment 1 Martin Janicek 2012-09-25 15:44:52 UTC
Wow, nice catch :]
Comment 2 Martin Janicek 2012-10-04 14:12:37 UTC
Btw: In which cases do you think we should have "Play" icon shown? I mean, it's hard to figure out if the code is just a script or whether it's a full class. 

In Java there is a Play icon only if corresponding public static main(String[] args) method exists, but I'm not sure what should be the entry point in groovy. Maybe I can look at the code and say if there is "something" outside of the class declaration, show the "Play" icon? What do you think?
Comment 3 michal.owsiak 2012-10-04 19:24:19 UTC
This is tricky question.

According to documentation:

http://groovy.codehaus.org/Running

Script must be:

"- Classes with a main method of course,
- Classes extending GroovyTestCase are run with JUnit's test runner,
- Classes implementing the Runnable interface are instanciated either with a constructor with String[] as argument, or with a no-args constructor, then their run() method is called."

Typically, when you write simple shell scripts, it is enough to have at least one command outside type definition.

e.g.

#!/usr/bin/env groovy
println("Hello world")

Or something like this:

class HelloWorld {
   def greet( name ){
      "Hello ${name}!"
   }
}

def hw = new HelloWorld();

But, there is a clue in file: src/main/groovy/lang/GroovyShell.java#200

(http://dist.groovy.codehaus.org/distributions/groovy-src-2.0.5.zip)

from Groovy source distribution.

Here, you can see what Groovy does to determine whether script can be executed or not.

try {
            // let's find a main method
            scriptClass.getMethod("main", new Class[]{String[].class});
            // if that main method exist, invoke it
            return InvokerHelper.invokeMethod(scriptClass, "main", new Object[]{args});
        } catch (NoSuchMethodException e) {
            // if it implements Runnable, try to instantiate it
            if (Runnable.class.isAssignableFrom(scriptClass)) {
                return runRunnable(scriptClass, args);
            }
            // if it's a JUnit 3.8.x test, run it with an appropriate runner
            if (isJUnit3Test(scriptClass)) {
                return runJUnit3Test(scriptClass);
            }
            // if it's a JUnit 3.8.x test suite, run it with an appropriate runner
            if (isJUnit3TestSuite(scriptClass)) {
                return runJUnit3TestSuite(scriptClass);
            }
            // if it's a JUnit 4.x test, run it with an appropriate runner
            if (isJUnit4Test(scriptClass)) {
                return runJUnit4Test(scriptClass);
            }
            for (Map.Entry<String, GroovyRunner> entry : GroovySystem.RUNNER_REGISTRY.entrySet()) {
                GroovyRunner runner = entry.getValue();
                if (runner != null && runner.canRun(scriptClass, this.loader)) {
                    return runner.run(scriptClass, this.loader);
                }
            }


Cheers
Comment 4 Martin Janicek 2012-10-08 09:01:00 UTC
Thanks a lot for a detailed explanation! I'll try to use GroovyShell for recognition..
Comment 5 Martin Janicek 2014-01-20 14:36:14 UTC
Changing to enhancement, setting TM = TBD