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

org.sfm.reflect.meta.TupleClassMeta 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.map.MapperBuildingException;
import org.sfm.reflect.ConstructorDefinition;
import org.sfm.reflect.ConstructorParameter;
import org.sfm.reflect.ReflectionService;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class TupleClassMeta implements ClassMeta {

	public static final String[] EMPTY_STRING_ARRAY = new String[0];
	private final ReflectionService reflectionService;
	private Type type;
	private List> constructorDefinitions;

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

		try {
			this.constructorDefinitions = reflectionService.extractConstructors(type);
		} catch(Exception e) {
			throw new MapperBuildingException(e.getMessage(), e);
		}
	}


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

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

	public Type getType() {
		return type;
	}

	@Override
	public String[] generateHeaders() {
		List strings = new ArrayList();

		int i = 0;
		for(ConstructorParameter cp : constructorDefinitions.get(0).getParameters()) {
			String prefix = "element" + i;

			ClassMeta classMeta = reflectionService.getClassMeta(cp.getResolvedType(), false);

			if (classMeta != null) {
				for(String prop : classMeta.generateHeaders()) {
					strings.add(prefix + "_" + prop);
				}
			} else {
				strings.add(prefix);
			}

			i++;
		}

		return strings.toArray(EMPTY_STRING_ARRAY);
	}


	public List> getConstructorDefinitions() {
		return constructorDefinitions;
	}

	public int getTupleSize() {
		return constructorDefinitions.get(0).getParameters().length;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy