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

org.simpleflatmapper.jdbc.impl.JpaAliasProvider 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: 9.0.2
Show newest version
package org.simpleflatmapper.jdbc.impl;

import org.simpleflatmapper.reflect.meta.AliasProvider;
import org.simpleflatmapper.reflect.meta.Table;
import org.simpleflatmapper.util.ErrorHelper;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class JpaAliasProvider implements AliasProvider {

	private final Class columnClass;
	private final Method columnName;
	private final Class tableClass;
	private final Method tableName;
	private final Method tableSchema;
	private final Method tableCatalog;

	@SuppressWarnings("unchecked")
	public JpaAliasProvider() {
		try {
			columnClass = (Class) Class.forName("javax.persistence.Column");
			columnName = columnClass.getDeclaredMethod("name");
			tableClass = (Class) Class.forName("javax.persistence.Table");
			tableName = tableClass.getDeclaredMethod("name");
			tableSchema = tableClass.getDeclaredMethod("schema");
			tableCatalog = tableClass.getDeclaredMethod("catalog");
		} catch(Exception e) {
			ErrorHelper.rethrow(e);
			throw new Error();
		}
	}


	@Override
	public String getAliasForMethod(Method method) {
		String alias = null;
		Object col = method.getAnnotation(columnClass);
		if (col != null) {
			alias = getColumnName(col);
		}
		return alias;
	}

	private String getColumnName(Object col) {
		return getString(col, columnName);
	}

	private String getTableName(Object col) {
		return getString(col, tableName);
	}

	private String getTableSchema(Object col) {
		return getString(col, tableSchema);
	}

	private String getTableCatalog(Object col) {
		return getString(col, tableCatalog);
	}

	private String getString(Object col, Method method) {
		try {
			return (String) method.invoke(col);
        } catch (Exception e) {
			return ErrorHelper.rethrow(e);
        }
	}

	@Override
	public String getAliasForField(Field field) {
		String alias = null;
		Object col = field.getAnnotation(columnClass);
		if (col != null) {
			alias = getColumnName(col);
		}
		return alias;
	}

	@Override
	public Table getTable(Class target) {
		Table table = Table.NULL;
		Object annotation = target.getAnnotation(tableClass);
		if (annotation != null) {
			table = new Table(getTableCatalog(annotation), getTableSchema(annotation), getTableName(annotation));
		}
		return table;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy