All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.lib.json.ArrayHandler Maven / Gradle / Ivy

Go to download

A main program (executable JAR) that will listen to port 29998. At first, it can only answer that it is an Envoy (a limited agent). The only function it supports is installing a -runpath. It will then create a framework + agent and transfer the connection to the just installed agent who will then install the bundles. This JAR is a main command for JPM called bndremote. In JPM, it will start up with debug enabled. This JAR does some highly complicated class loading wizardy to ensure that it does not enforce any constraints on the -runpath.

The newest version!
package aQute.lib.json;

import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;

import aQute.lib.hex.Hex;

public class ArrayHandler extends Handler {
	Type componentType;

	ArrayHandler(@SuppressWarnings("unused") Class rawClass, Type componentType) {
		this.componentType = componentType;
	}

	@Override
	public void encode(Encoder app, Object object, Map visited) throws IOException, Exception {

		// Byte arrays should not be treated as arrays. We treat them
		// as hex strings

		if (object instanceof byte[] array) {
			StringHandler.string(app, Hex.toHexString(array));
			return;
		}
		app.append("[");
		app.indent();
		String del = "";
		int l = Array.getLength(object);
		for (int i = 0; i < l; i++)
			try {
				app.append(del);
				if (!del.isEmpty()) {
					app.linebreak();
				}
				app.encode(Array.get(object, i), componentType, visited);
				del = ",";
			} catch (Exception e) {
				throw new IllegalArgumentException("[" + i + "]", e);
			}
		app.undent();
		app.append("]");
	}

	@Override
	public Object decodeArray(Decoder r) throws Exception {
		ArrayList list = new ArrayList<>();
		r.codec.parseArray(list, componentType, r);
		Object array = Array.newInstance(r.codec.getRawClass(componentType), list.size());
		int n = 0;
		for (Object o : list)
			Array.set(array, n++, o);

		return array;
	}
}