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

org.sfm.reflect.meta.OptionalClassMeta Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.reflect.meta;

import org.sfm.reflect.InstantiatorDefinition;
import org.sfm.reflect.Parameter;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.TypeHelper;
import org.sfm.utils.ErrorHelper;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

public class OptionalClassMeta implements ClassMeta {

    public static final String[] EMPTY_STRING_ARRAY = new String[0];
    private final ReflectionService reflectionService;
	private final Type type;
	private final InstantiatorDefinition instantiatorDefinition;
    private final ConstructorPropertyMeta propertyMeta;
	private final ClassMeta innerMeta;


	public OptionalClassMeta(Type type, ReflectionService reflectionService) {
		this.type = type;
		this.reflectionService = reflectionService;

		try {
            this.instantiatorDefinition = getInstantiatorDefinition(type);
            this.propertyMeta = new ConstructorPropertyMeta<>("value", reflectionService, instantiatorDefinition.getParameters()[0], TypeHelper.toClass(type));
			this.innerMeta = reflectionService.getClassMeta(instantiatorDefinition.getParameters()[0].getGenericType());
		} catch(Exception e) {
            ErrorHelper.rethrow(e);
            throw new IllegalStateException();
		}
	}

    private InstantiatorDefinition getInstantiatorDefinition(Type type) throws NoSuchMethodException {
        ParameterizedType pt = (ParameterizedType) type;
        InstantiatorDefinition id = new InstantiatorDefinition(Optional.class.getMethod("ofNullable", Object.class),
                new Parameter("value", Object.class, pt.getActualTypeArguments()[0]));
        return id;
    }

    public InstantiatorDefinition getInstantiatorDefinition() {
        return instantiatorDefinition;
    }

    @Override
	public ReflectionService getReflectionService() {
		return reflectionService;
	}

	@Override
	public PropertyFinder newPropertyFinder() {
		return new OptionalPropertyFinder(this);
	}

	public Type getType() {
		return type;
	}

	@Override
	public String[] generateHeaders() {
		List strings = new ArrayList();
		int i = 0;
		for(Parameter cp : instantiatorDefinition.getParameters()) {
			ClassMeta classMeta = reflectionService.getClassMeta(cp.getGenericType());

			for(String prop : classMeta.generateHeaders()) {
				strings.add(prop);
			}

			i++;
		}
		return strings.toArray(EMPTY_STRING_ARRAY);
	}

	public ClassMeta getInnerMeta() {
		return innerMeta;
	}

    public PropertyMeta getProperty() {
        return propertyMeta;
    }

	@Override
	public boolean isLeaf() {
		return false;
	}

	@Override
	public List getInstantiatorDefinitions() {
		return Arrays.asList(instantiatorDefinition);
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy