Skip to content

Arguments of type 'MyObject[]' are not properly handled #34

@beirtipol

Description

@beirtipol

If interacting with arrays of object references instead of primitives, the objects are not properly converted during reflection conversion. e.g.

@XLFunction(name = "randomdoubles", //
		help = "list of random doubles", //
		args = { //
				"size" //
		}, //
		argHelp = { //
				"size of array" //
		}, //
		category = CATEGORY)
public static DoubleWrapper[] randomdoubles(int size) {
	DoubleWrapper[] result = new DoubleWrapper[size];
	for (int i = 0; i < result.length; i++) {
		result[i] = new DoubleWrapper(Math.random());
	}
	return result;
}

@XLFunction(name = "sumdoubles", //
		help = "sum of an array of doubles", //
		args = { //
				"doubles" //
		}, //
		argHelp = { //
				"array of doubles" //
		}, //
		category = CATEGORY)
public static double sumdoubles(DoubleWrapper[] doubles) {
	double result = 0.0;
	for (DoubleWrapper d : doubles) {
		result += d.wrapped;
	}
	return result;
}

public static class DoubleWrapper {
	public double wrapped;

	public DoubleWrapper(double wrap) {
		this.wrapped = wrap;
	}
}

If you try and call 'sumdoubles' from Excel, passing in an array of objects created from 'randomdoubles', you get an IllegalArgumentException due to type mismatch. It's down to how the objects are converted in XLoperConverter

I've been able to fix by adding the following to 'convertVector' though I suspect it could just replace the other Object[] handling. Apologies for not cloning and doing a pull request, am blocked by our company proxy.

	....
	else if (clazz.isArray()) {
		Class underlyingType = clazz.getComponentType();
		Object array17 = Array.newInstance(underlyingType, xlArray.rows);
		for (int n17 = 0; n17 < xlArray.rows; ++n17) {
			Array.set(array17, n17, this.createFrom(xlArray.get(n17), underlyingType));
		}
		value = array17;
	}
	....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions