Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java-Groovy : passing arrays

 
Paul Ristevian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
From a previous post someone helped me with getting a Java class to interact with Groovy class.
I was able to pass string literals and regular variables, but having difficulties with passing arrays.

SO this is the situation: I wish to call groovy class from Java.
But when I call the groovy class in the main{} of Java, I wish to pass a Java Array.
But what is the equivalent in to Java Array in Groovy.
I saw somewhere, that you may need to do some kind of casting on a Groovy List structure.

I am using Netbeans IDE by the way.

Thanks.

P
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since everything in Groovy is an object, I don't think it has a concept of array. So, you probably need to change your Java signature to take in java.util.List rather than array. In java, it's quite easy to convert from a List to an array.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Freddy:

Groovy has arrays: JN1025-Arrays.

John.
 
Paul Ristevian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks for the reply.
But let me get into some code to get into specifics.

Does that mean I can do the following:

In Java we have this declaration :
**************************************
String[] aas = { "array", "of", "String", };
**************************************

In Groovy we have this class :

**************************************
class HelloWorld {
def greet( name ){
return "Hello ${name[0]} with your new ${name[1]} and your second ${name[2]}, and that is it!"
}
}
**************************************


SO in Java :: Main we have this java passing array to groovy instantiation :
HelloWorld helloArrayMethodpass = new HelloWorld();

helloArrayMethodpass.setgreet(aas);
System.out.println(helloArrayMethodpass.getgreet());

*****************************************************

Or do I have to declare the parameter 'name' in the groovy snippet of code first to some groovy array, like this :

def name= new Object[4] //we must specify the size of the fixed-size array
assert name.size() == 4

*******************************************************

Can you please let me know if any of the above is correct.


Paul


 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul:

You can probably just do this:



John.
 
Paul Ristevian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks I will give that a try.
it is good if it this simple.

By the way, I am not that familiar with the 'assert' statement in Groovy.
And have difficulty finding the definition of this 'assert'. People just use it
and hardly explain , i guess, this is due to the fact that it is considered very basic,
and due to this it is not explained much out on the web sites/groovy tutorial sites.

If you can give me the explanation it will be much appreciated.

Sincerely,

Paul
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul:

You can check out the Java assertions tutorial here. Alternatively, you can use the JUnit or TestNG assert classes to do the same thing.

John.
 
Paul Ristevian
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

When I gave it a try it did not work.
But then I found out I made an array go out of bounds with this statement : ${name[3]},
so this went to the 4th element when I passed a 3-element array.

Once I fixed it to : ${name[2]}, then it worked.
See the code below.

So the KEY thing was to declare array in Groovy class : String[] name ;
But I find that strange because this is exactly Java syntax, and in the link to Arrays with Groovy seem to be different than this.

BUT it does work! So thanks a lot!!!



Here it is below:

*********************************************
Java Code (Java has the MAIN):

public static void main(String args[]) {


String[] ArrayStr = { "Google", "Yahoo", "MSN" };

String wordMultiLine;
ProcessG process = new ProcessG();

wordMultiLine = process.getMessage(ArrayStr);

System.out.println(wordMultiLine);


**********************************************
The Groovy code is this::

public class ProcessG {

String[] name


public String getMessage(name) {
assert(name.size() >= 3)
return """
<html>
<body>
Hello, my name is ${name[0]}
and I am returning this from personPlace : ${name[1]}
a multi line GString called : ${name[2]}
</body>
</html>
"""
}

****************************************************************




 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul:

It works that way because under the hood, Groovy really is Java. The 'def' keyword basically tells Groovy that you want it to do the dirty work of type specification. You can specify an array, or you can let Groovy handle it, and it will create ArrayLists.

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic