More Monkey
I've written a couple moreEclipse Monkeyscripts that I hope might be useful to others. They aren't meant as examples of anything clever using Monkey - for that, you'd be better looking elsewhere.
There are some bits of Java code editing typing that I see often, so, although they don't take long, I've written a couple of monkey scripts to help.
To install the scripts, install eclipse monkey, for each script, copy the script (including the "---" lines at the beginning and end), then from the "Monkey" menu in eclipse, select "Paste new script".
Convert method parameter declaration into method call parameter list
Converts:
String string, int integer, int[] nums, List list
into:
string, integer, nums, list
The script:
--- Came wiffling through the eclipsey wood ---
/*
* Menu: Strips types out of parameter list
Converts:
String foo, int x
into:
foo, x
* Kudos: Ivan Moore
* License: EPL
*/
function main() {
systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
contents = systemClipboard.getContents(null);
msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
parts = msg.split(',');
result = "";
for(partNumber in parts){
part = parts[partNumber].trim();
if(partNumber > 0){
result += ", ";
}
result += part.split(" ")[1]
}
newContents = new Packages.java.awt.datatransfer.StringSelection(result);
systemClipboard.setContents(newContents, newContents);
}
--- And burbled as it ran! --- Convert method parameter declaration into local variable declarations.
Converts:
String string, int integer, int[] nums, List list
into:
String string = null; int integer = -1; int[] nums = null; List list = null;
The script:
--- Came wiffling through the eclipsey wood ---
/*
* Menu: Paramater list INTO declarations
Converts:
String foo, int x
into:
String foo = null;
int x = -1;
* Kudos: Ivan Moore
* License: EPL
*/
function main() {
systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
contents = systemClipboard.getContents(null);
msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
parts = msg.split(',');
declarations = "";
for(partNumber in parts){
if(partNumber > 0){
declarations += "\t\t";
}
part = parts[partNumber].trim();
declarations += part + " = " + suitableValue(part) + ";\n";
}
newContents = new Packages.java.awt.datatransfer.StringSelection(declarations);
systemClipboard.setContents(newContents, newContents);
}
function suitableValue(part){
type = part.split(" ")[0];
if(type == "long") return "-1L";
if(type == "boolean") return "false";
if(type == "int" || type == "short" || type == "byte") return "-1";
if(type == "double") return "-1.0";
if(type == "float") return "-1.0F";
return "null";
}
--- And burbled as it ran! --- How to run the scripts
Once monkey and the scripts are installed:
- Copy the method parameters (e.g.
String string, int integer, int[] nums, List list). - Select the appropriate monkey menu item (e.g. "Paramater list INTO declarations")
- Paste, and you get the results as described above
What I'd like
Can you associate a keyboard shortcut to each monkey script? That would be really useful. The point of any script as tiny as these shown is to eliminate the jarring flow-destroying few seconds of manual editing. Therefore, to be of most use, they might be better available from a keyboard shortcut. Maybe.
If you know of a better way of doing these, then please comment.
Related Articles
Jesper Møller: Monkey see, abe gør
Jeff Mesnil: JMX Scripts with Eclipse Monkey
Programming in the small - removing duplication.
Spicing Up Embedded JavaScript
JavaScript Based Code Prettification
Hibernate user type
Jonas Boner: The Power of Sockets and Dynamic Proxies
Birt World: BIRT Milestone 5 New and Notable




