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

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

There is a newer version: 7.0.0
Show newest version
package aQute.lib.json;

import static java.lang.invoke.MethodHandles.publicLookup;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.regex.Pattern;

public class SpecialHandler extends Handler {
	@SuppressWarnings("rawtypes")
	final Class				type;
	final Method			valueOf;
	final Constructor	constructor;

	public SpecialHandler(Class type, Constructor constructor, Method valueOf) {
		this.type = type;
		this.constructor = constructor;
		this.valueOf = valueOf;
	}

	@Override
	public void encode(Encoder app, Object object, Map visited) throws IOException, Exception {
		StringHandler.string(app, object.toString());
	}

	@Override
	public Object decode(Decoder dec, String s) throws Exception {
		if (type == Pattern.class)
			return Pattern.compile(s);

		if (constructor != null) {
			try {
				return publicLookup().unreflectConstructor(constructor)
					.invoke(s);
			} catch (Error | Exception e) {
				throw e;
			} catch (Throwable e) {
				throw new RuntimeException(e);
			}
		}

		if (valueOf != null) {
			try {
				return publicLookup().unreflect(valueOf)
					.invoke(s);
			} catch (Error | Exception e) {
				throw e;
			} catch (Throwable e) {
				throw new RuntimeException(e);
			}
		}

		throw new IllegalArgumentException("Do not know how to convert a " + type + " from a string");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy