From:

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

Relatd Projects

koalaxml

Extremely simple Java XML data type, very useful as a parameter/return type, as well as a class property type.
sqw-01b
SqW is a mathematical program for caculating and solving equations of type f(x)=0. It also builds simple graphs and has syntax analyser. Functions: sin(x), cos(x), tg(x), lg(x)... LEAVE YOUR RESPONSES ON FORUMS
monkey-manager
Monkey Manager 2007 is an ebay seller support software planned to be very usable and user-friendly (designed with HDCI method).The program manage: the seller list,the client list, the orders for clients e sellers, the catalogues, and they selling history
lineargs
LineArgs is the library contain a simple implementation of command line arguments parser based on annotations/metadata. The library has identical API for Java (1.5) and C# (.net 2.0) . Supports String,Integer,Boolean and List command line options.
autoproxy4j
Currently Java does not support the execution of autoproxy scripts. I wrote some Java code with the help of Mozilla's Rhino (implementation of JavaScript written entirely in Java) to get the result of an autoproxy script.
docsearch
This is a network based text search tool. The Network part consists of Client and Server side .class files. The text search algorithm has been implemented in C. On client side type SearchClient ip(server) string
expression4j
Expression4J (Math Expression Parser) is a java framework used to manage mathematic expression stored in String object like "f(x,b)=2*x-cos(b)" or "g(x,y)=f(y,x)*-2". Expression gramar can be define by user (add new operator or type ...)
monkey
Monkey is a scanner and a LR(1) parser generator for object oriented languages. The platform on wich it runs is Java.
jmac
It is a Java implementation of Monkey's Audio Compression decoder. JMAC is a Java library that decodes, converts and plays Monkey's Audio files (.MAC, .APL, .APE) in real time. JMAC doesn't need JMF. It runs under J2SE.
sqlrecordset
An implentation of Microsoft ActiveX Data Objects in Java, allowing disconnected recordset support, better NULL support and business object superclasses for rapid enterprise solution development.