Marek’s Blog
Random thoughts and ideas of Marek Radonsky-
Calling a varargs method from another vararg method in Java 5+
Posted on February 15th, 2009 No commentsI’ve decided to blog about this, because I ran into this kind of problem and I couldn’t find the answer anywhere, until I finally figured out how it works.
Every Java developer knows that Java 5+ supports variable argument methods. You can find a plenty of tutorials and examples how to write a method with a variable number of arguments, so I’ll jump right at the problem that I had.
Imagine that you have the following methods in one or two classes:void callMethod(String name, Object... args) {...} void sendMessage(String message, Object... params) {...}Now imagine that the implementation of the callMethod has to call the sendMessage method. Note that both of these methods have a vararg parameter and therefore the callMethod can be called with an arbitrary number of actual parameters. The question is, how to pass the array of parameters args into the sendMessage method. The answer is easy enough if you want to pass the args as they are. Here is a sample code:
void callMethod(String name, Object... args) { //... sendMessage(name, args); //... }This code passes all of the parameters that were used to call the callMethod to the sendMessage method and therefore params parameter will be an array containing all of those arguments.
Now the problem I was trying to solve was, that I wanted to pass into the sendMessage method all of the args and in addition to it also one or more parameters. These parameters should have been the first of the passed arguments into the params variable. Let’s say that I wanted to pass the number of parameters and then the actual parameters. Here is a code that does that:void callMethod(String name, Object... args) { //... Object[] newArgs = new Object[args.length]; newArgs[0] = args.length; System.arraycopy(args, 0, newArgs, 1, args.length); sendMessage(name, newArgs); //... }This way I was able to pass a modified number of arguments into a vararg method. The array newArgs is not going to be put into the first element of the params array of objects, but it is passed “as it is” (no new array needs to be created). So for example, if you called the callMethod this way
callMethod("method1", "param1", 2, 3.0);then the sendMessage’s params vararg parameter would have the following values:
[3, "param1", 2, 3.0]
Even though this might seem easy to some people, I think that there should be a better way how to do this. I don’t really like that I have to create a new array, set the elements manually and then copy the rest of the arguments. I think it would be great if Java provided a special syntax for these cases, such as for example “^” prefix for the array/vararg variables whose elements should be copied into the vararg array of the called method. For example the following code would do the same thing like the code above:
void callMethod(String name, Object... args) { //... sendMessage(name, args.length, ^args); // this doesn't work in Java, but it would be nice //... }What do you think? Do you agree that Java language should have something like this in its syntax?
-
Tell me what you read and I will tell you who you are
Posted on February 20th, 2007 No commentsToday, my friend Brittany published an interesting post, which started with these instructions:
1.] grab the nearest book.
2.] open the book to page 23.
3.] find the fifth sentence.
4.] post the text of the next three sentences on your blog along with these instructions.
5.] don’t you dare dig for that “cool” or “intellectual” book in your closet! i know you were thinking about it! just pick up whatever is closest.
6.] tag five other people to do the same.Ok, then. Here is an excerpt from my closest book:
“What is ORM? In nutshell, object/relational mapping is the automated (and transparent) persistence of objects in Java application to the tables in a relational database, using metadata that describes the mapping between the objects and the database. ORM, in essence, works by (reversibly) transforming data from one representation to another.”
The excerpt is from Hibernate in Action by Christian Bauer and Gavin King. This book has been on my desk for a year and it will probably stay there for some time as I use it for my work. I have to look in it every once in a while to find some information regarding to a tool we use in our application. It is an excellent reads, but if you aren’t a software developer, you won’t enjoy it too much.
I like the excerpt from Brittany’s book, so take a look at her goethewannabe’s Xanga site. And of course, don’t forget to publish 3 sentences from your book!
-
XPath Explorer
Posted on July 12th, 2006 No commentsNot so long time ago I came across this small utility called XPath Explorer (XPE). It is Swing based Java application allowing you to create XPath queries by simply clicking on XML elements you want to get from an example file. Or you can paste your XPatch query and see all nodes returned by that query. They also provide something they call Eclipse plugin. It actually adds an action to one of your toolbars, which allows you to run the XPE from Eclipse but it will still start the XPE in separate (Swing) window, so don’t expect any better form of integration. This tool is a great help for everybody who doesn’t remember the XPath syntax and has to use XPath expressions once in a while. Give it a try or just remember that it exists and download it when you need it.
-
Google Web Toolkit
Posted on May 25th, 2006 No commentsThis week TSS published an article about the new web toolkit from Google called GWT (note the reference to AWT and SWT). It allows AJAX based UI to be developed using composition of various components (kind of like in SWT - panels, layouts, buttons etc.). You write code in Java and use special browser to test and debug the application, then a special compiler converts the code from Java to JavaScript and the app is ready for a deployment.
I can’t tell if it is good or bad, but it sounds interesting (like most of the Google’s work), so I think it is definitely worth a look and try. -
My pictures
Posted on February 1st, 2006 No commentsAs you can see here, I don’t blog for too long. But for some time I collect and maintain my photo albums online, something like my “photo diary”. You can find these albums on a server that hosts my domain - radonsky.com. It sometimes contains pictures that I don’t consider “good”, but they are included in my gallery just to give some additional point of view. As you know, nobody wants to delete a picture after it was taken, even if it’s bad! I’m just not strong enough.
Let me know what do you think about my galleries.
Thx, M.
-
Another blog - Changes Abroad
Posted on February 1st, 2006 No commentsLast time I wrote you about my friend J.R.’s and his blog. It looks like he has problems with connectivity in Ecuador, so his blog is getting old. But that’s understandable as he has probably a lot of other things to do than to write an “internet diary” for all of us, who just lazily yawn in front of our computer screens.
Today, I want to tell you about a blog of my other two friends - Brooke and Brett. They also left United States and decided to spend a year in New Zealand. You can read about their adventures here in the blog called Changes Abroad.
These young Americans decided to leave comfort of their home and spend some time in a foreign country. I think that it will be a great experience for them and they will return as different people, with much better understanding of the world. (I know what I’m saying as I’m experiencing something similar right now.) I think that such experience enriches everybody and so it should be mandatory for every young person, to go abroad and spend there at a couple of months (instead of going to a military service, for example). If everybody did that, there would be no wars. Don’t you agree? -
J.R.’s new blog
Posted on January 24th, 2006 1 commentIt has been a while since I wrote my first post to this blog - almost two years. Since then, many things changed. I’ve moved from Prague, Czech Republic to Raleigh, North Carolina, where I do something like a professional career training. My friend J.R. returned from South America, spent more than a year in Raleigh and now he is back again in Cuenca, Ecuador. He has a new blog and whole new web at http://www.gedanken-experiment.com/ so I recommend you to check it out.
That’s it for today as I’m really, really tired.
M. -
First thoughts…
Posted on June 10th, 2004 No commentsHello everybody! (or nobody?)
For those of you who know me it’s probably not surprising that I started to write something like this - personal log - published on the web. Although English is not my native language, I decided to use it to allow my friends from all over the world to read it. I hope that my knowledge of the language will be enough for this writing and for them (for you) to understand.
It was my friend J.R. who showed me that blogs can be useful and fun not just for their authors, but also for anybody who watches and reads them. He is now traveling in South America and I spent hours by reading the posts from his journey. So I recommend you to see his blog at http://club.telepolis.com/jrguitar21/blog/.
I think that blogs are great especially for travelers (and backpackers), because it allows them to share adventures and impressions with anybody, who’s interested. That’s why I recommended blogging to my other friend - Ales, who is now going to spend summer in US. I hope, that he will also create his own blog, ’cause I can’t wait to hear about his adventures.
I think this is enough for my first post, so please be patient, some smart thoughts will come next time (maybe).



