Carl Higashionna | 1 Feb 2004 01:36

Re: importing groovy scripts?

James,

Does this mean that importing groovy scripts works from within groovlets? If
so, how do I go about importing other scripts from groovlets? Can I just
use:

import myGroovyScript

Assuming that myGroovyScript lives at the root level of my web app. Or do I
need to create a class within the myGroovyScript file and import it that
way:

import myGroovyScript.myGroovyClass

This is great if it is already working in groovlets. I was resigned to
having to write monolithic servlets for the time being. And slowly
refactoring them and compiling separate classes so that I could use some
nicer design patterns like the Command Design Pattern, etc.

Thanks,
Carl

ps. I've been using Jython to script Java for a while and I've been using
Groovy for about a week now and I really enjoy it. Great work!

-----Original Message-----
>From: James Strachan <james_strachan@...>
>Subject: Re: [groovy-user] importing groovy scripts?
>Date: Sat, 31 Jan 2004 08:30:54 +0000
>To: groovy-user@...
(Continue reading)

James Strachan | 1 Feb 2004 11:53
Picon
Favicon

Re: importing groovy scripts?

So far the import rules work just like Java; i.e. you only need to 
import classes that are in a different package. If you're groovlets & 
scripts are not in packages or are all in the same package you 
shouldn't have to import.

Right now groovlets manage to auto-compile dependent scripts/classes. 
We need to refactor this to make it available to any kind of script 
etc.

On 1 Feb 2004, at 00:36, Carl Higashionna wrote:

> James,
>
> Does this mean that importing groovy scripts works from within 
> groovlets? If
> so, how do I go about importing other scripts from groovlets? Can I 
> just
> use:
>
> import myGroovyScript
>
> Assuming that myGroovyScript lives at the root level of my web app. Or 
> do I
> need to create a class within the myGroovyScript file and import it 
> that
> way:
>
> import myGroovyScript.myGroovyClass
>
> This is great if it is already working in groovlets. I was resigned to
(Continue reading)

Stefan Matthias Aust | 1 Feb 2004 12:00
Picon

[OT] Scala from a Groovy point of view

Scala is a new JVM-based language developed by Martin Odersky (creator
of Pizza aka GJ) and colleagues.  I compiled this very subjective view
on Scala because I think there're a lot of good ideas that might also
influence the design of Groovy.

Actually, you can't really compare both languages because Scala is a 
statically type functional language and not a dynamically typed
scripting language. Scala is also fully objectoriented (and more than 
Java) but it's my impression that at least currently, the developers 
concentrated on the functional aspects - unfortunately.

Scala compiled to the JVM and there seems to be work in progress to 
compile Scala also to the .NET CLR.  However, I don't think that these 
two version will be compatible because each version of Scala can 
directly interact with the underlying framework and will use the 
specific APIs of that platform.

To start with, Scala closes the gap between primitives types and object 
type, providing the illusion that everything is an object (as any good 
OO language should do IMHO).  They don't provide arbitrary precission 
integers though, so you still suffer from the 32 bit limit of integers. 
There're also no fractional numbers that is you have only inexact 
floating number for non-integer calculations.

I like Scala's clean syntax.  This defines (unsurprisingly :) a class

  class Foo { ... }

The class is public which is the default if no modifier is present, if 
you need a private class, add "private".  For variables and methods, 
(Continue reading)

Volkmann, Mark | 2 Feb 2004 17:03

here-docs and newlines

I was surprised to see that the following examples output the same thing.

---

s = "  This string
  spans three lines
  and contains two newlines."
println "[${s}]"

s = <<<EOS
  This string
  spans three lines
  and contains two newlines.
EOS
println "[${s}]"

---

Here's the output.

[  This string
  spans three lines
  and contains two newlines.]
[  This string
  spans three lines
  and contains two newlines.]

Why does the newline character before the closing EOS in the here-doc get trimmed?  Is that the correct behavior or a bug?



-------------------------------------------------------------------------------------
A.G. Edwards & Sons' outgoing and incoming e-mails are electronically
archived and subject to review and/or disclosure to someone other
than the recipient.

-------------------------------------------------------------------------------------
Guillaume Laforge | 2 Feb 2004 17:47
Picon
Favicon

RE: here-docs and newlines

Why does the newline character before the closing EOS in the here-doc get trimmed?  Is that the correct behavior or a bug?
 
I think it's the correct behaviour.
At least, it's implemented that way.
If you want the newline to be there, add a blank line before the ending marker.
 
How is it done in other scripting languages such as Ruby ?
 
Guillaume Laforge 
Okke van 't Verlaat | 2 Feb 2004 22:37
Picon

import groovy scripts

I've read the latest discussion about importing groovy scripts from 
within other groovy scripts on this list, and took a look at the 'latest 
from head' source code to find a possible solution. What I've did was 
creating my own classloader that is capable of loading regular classes 
as well as parsing .groovy files. A Simple experiment with promising 
results. Using my own classloader its was possible to mix groovy files 
and java (class) files on the same classpath. And much nicer : The 
import from within groovy was also using my own class loader. A simple 
and elegant solution . Here's the code :

--- cut (maybe formatting got wasted by pasting from eclipse ---

package test.loader;

import groovy.lang.GroovyClassLoader;

import java.io.IOException;
import java.io.InputStream;

import org.codehaus.groovy.syntax.SyntaxException;

/**
  * GrownClassLoader loads normal classes but also parses groovy files
  * and load them as regular class files
  *
  *  <at> author okke
  *
  */
public class TestClassLoader extends GroovyClassLoader {

     /**
      * Create a new Groovy class loader.
      *
      *  <at> param parent Parent classloader to use
      */
     public TestClassLoader(ClassLoader parent) {
     	super(parent);
     }

	/**
      * load a normal java class or a groovy script.
      *
      *  <at> param clazz Class name to load
      *
      *  <at> return Class
      *
      *  <at> throws ClassNotFoundException when no groovy script or real class
      *         could be found or when the found groovy script could not 
get parsed.
      *
	 *  <at> see java.lang.ClassLoader#loadClass(java.lang.String)
	 */
	public Class loadClass(String clazz) throws ClassNotFoundException {

         // first check if there is a groovy resource available
         //
         String groovyName = this.classToGroovy(clazz);

         InputStream is = this.getResourceAsStream(groovyName);
         if(is == null) {
             return super.loadClass(clazz);
         } else {
             try {
             	Class parsed = this.parseClass(is,clazz);

                 return parsed;
             } catch (IOException ioex) {
             	throw new ClassNotFoundException("IO exception while 
parsing " + groovyName,ioex);
             } catch (SyntaxException stex) {
                 throw new ClassNotFoundException("Syntax exception 
while parsing " + groovyName,stex);
             } finally {
             	try {
			is.close();
		} catch (IOException ioex) {
			// ignore
		}
             }
         }
	}

     /**
      * get the .groovy name of a class file (using jdk1.4 regexp 
replacement)
      *
      *  <at> param clazz Classname
      *  <at> return groovy name
      */
     protected String classToGroovy(String clazz) {
         return clazz.replaceAll("\\.","\\/") + ".groovy";
     }
}

-- copy and paste --

and here's some testcode:

--- TestLoader.java : java main file ---

package test.loader;

/**
  *  <at> author okke
  *
  */
public class TestLoader {

	/**
	 *
	 */
	public TestGrownClassLoader() {
		super();
	}

	public static void main(String[] args) throws Exception {
		try {
			ClassLoader loader = new 
TestClassLoader(Thread.currentThread().getContextClassLoader());

			Class tc = loader.loadClass("test.loader.TestClass");
			tc.newInstance();
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
	}
}

--- TestClass.groovy (simply put next to your java test class) ---

package test.loader

import test.loader.TestImport

class TestClass {
	TestClass() {
		System.out.println("testclass instantiated")
		new TestImport()
	}
}

--- TestImport.groovy (simply put next to your java test class) ---

package test.loader

class TestImport {
	TestImport() {
		System.out.println("test import instantiated");
	}
}

---

Have fun,
grtjs,
Okke
Owen Densmore | 4 Feb 2004 00:27
Favicon
Gravatar

groovysh output seems to skip a line

When I type a printing line to groovysh, it seems to need me to give it
a blank line before it prints.  Here's what I mean:

owen|~[510]: groovysh
Lets get Groovy!
================
Version: 1.0-beta-3 JVM: 1.4.1_01-27
Hit carriage return twice to execute a command
The command 'quit' will terminate the shell
groovy> foo=1
groovy> println foo         << should print when I hit CR
groovy>                     << ..but no print until the next line
1
null
groovy> println "hi"        << again.
groovy>
hi
null
groovy>

Am I making a silly mistake?  This is on Mac OS X Panther, using
the bash shell in the terminal application.

Owen

Owen Densmore          908 Camino Santander       Santa Fe, NM 87505
owen@...    Cell: 505-570-0168         Home: 505-988-3787
AIM:owendensmore   http://complexityworkshop.com  http://backspaces.net
Mark Volkmann | 4 Feb 2004 02:27
Favicon

Re: groovysh output seems to skip a line

What you are seeing is the way it is supposed to work.
You can enter as many lines as you want, pressing the enter key after each.
They aren't evaluated (compiled to bytecode and then executed) until you
press the enter key on a blank line.
This wasn't immediately apparent to me either, but now I think it's a good
approach.

----- Original Message -----
From: "Owen Densmore" <owen@...>
To: <groovy-user@...>
Sent: Tuesday, February 03, 2004 5:27 PM
Subject: [groovy-user] groovysh output seems to skip a line

> When I type a printing line to groovysh, it seems to need me to give it
> a blank line before it prints.  Here's what I mean:
>
> owen|~[510]: groovysh
> Lets get Groovy!
> ================
> Version: 1.0-beta-3 JVM: 1.4.1_01-27
> Hit carriage return twice to execute a command
> The command 'quit' will terminate the shell
> groovy> foo=1
> groovy> println foo         << should print when I hit CR
> groovy>                     << ..but no print until the next line
> 1
> null
> groovy> println "hi"        << again.
> groovy>
> hi
> null
> groovy>
>
> Am I making a silly mistake?  This is on Mac OS X Panther, using
> the bash shell in the terminal application.
>
> Owen
>
> Owen Densmore          908 Camino Santander       Santa Fe, NM 87505
> owen@...    Cell: 505-570-0168         Home: 505-988-3787
> AIM:owendensmore   http://complexityworkshop.com  http://backspaces.net
>
> _______________________________________________
> groovy-user mailing list
> groovy-user@...
> http://lists.codehaus.org/mailman/listinfo/groovy-user
>
Owen Densmore | 4 Feb 2004 06:58
Favicon
Gravatar

Real Beans

Folks: I'd like to use Groovy to create Java Beans that are 
indistinguishable from those written in Java.  By this I mean:
	- Setters and getters work just like those in Java, and
	- Reflection will work properly so that bean containers can find
	  the setter/getters correctly.
I want to use this for creating models in the RePast modeling system, 
which in the past has tried Jython but had bad luck with bean access.

Here is the problem description from a RePast implementor:
	A couple of years ago, I rewrote the repast heatbugs demo in Jython
	(python in Java). It worked quite well except for one glaring
	exception. The various probes (mouse-based and the model properties)
	rely on java bean properties defined through accessor methods and
	jpython was unable to duplicate that. That may no longer be the case
	though. So, the model ran just fine, but there was no gui
	manipulation of the model properties.

Owen Densmore          908 Camino Santander       Santa Fe, NM 87505
owen@...    Cell: 505-570-0168         Home: 505-988-3787
AIM:owendensmore   http://complexityworkshop.com  http://backspaces.net
Peter Reilly | 4 Feb 2004 09:38
Favicon

Re: Real Beans

Groovy (and beanshell 2.0) does support real beans.
I am testing this at the moment for a small extension to ant.

groovy:
class X {
   Integer y;
}
is equivalent to:
java:
public class X {
   public Integer y;
   public void setY(Integer y) {
       this.y = y;
   }
   public Integer getY() { return y; }
}

One issue I found:

groovy:
class X {
   Integer y;
    public void setY(Integer y) {
      this.y = y;
    }
}

Using setY() will cause a stackoverflow,

Peter

Owen Densmore wrote:

> Folks: I'd like to use Groovy to create Java Beans that are 
> indistinguishable from those written in Java.  By this I mean:
>     - Setters and getters work just like those in Java, and
>     - Reflection will work properly so that bean containers can find
>       the setter/getters correctly.
> I want to use this for creating models in the RePast modeling system, 
> which in the past has tried Jython but had bad luck with bean access.
>
> Here is the problem description from a RePast implementor:
>     A couple of years ago, I rewrote the repast heatbugs demo in Jython
>     (python in Java). It worked quite well except for one glaring
>     exception. The various probes (mouse-based and the model properties)
>     rely on java bean properties defined through accessor methods and
>     jpython was unable to duplicate that. That may no longer be the case
>     though. So, the model ran just fine, but there was no gui
>     manipulation of the model properties.
>
> Owen Densmore          908 Camino Santander       Santa Fe, NM 87505
> owen@...    Cell: 505-570-0168         Home: 505-988-3787
> AIM:owendensmore   http://complexityworkshop.com  http://backspaces.net
>
> _______________________________________________
> groovy-user mailing list
> groovy-user@...
> http://lists.codehaus.org/mailman/listinfo/groovy-user
>
>

Gmane