简体   繁体   中英

How to get object type in runtime?

I am trying to create a class that is responsable for managing all the database access of my application, controlling the API of my DAO objects that have all protected methods. Some indentifiers are in portuguese since I'm a brazilian, anyways I think you will be able to understand whats going on there.. Following is the code:

public class DatabaseManager {

private ItemExpansivelDataSource itemExpansivelDataSource;
private ConfiguracoesDashboardDataSource configuracoesDashboardDataSource;
private PranchaDataSource pranchaDataSource;
private PranchaRaizDataSource pranchaRaizDataSource;

private HashMap<Class<?>, DataSource<?>> dataSourcesMap;

public DatabaseManager() {
    super();
    this.dataSourcesMap = new HashMap<Class<?>, DataSource<?>>();
    dataSourcesMap.put(ItemExpansivel.class, itemExpansivelDataSource);
    dataSourcesMap.put(ConfiguracoesDashboard.class, itemExpansivelDataSource);
    dataSourcesMap.put(Prancha.class, itemExpansivelDataSource);
    dataSourcesMap.put(PranchaRaiz.class, itemExpansivelDataSource);
}

public void insert(Object objeto){
    if(dataSourcesMap.containsKey(objeto.getClass())) {
        return dataSourcesMap.get(objeto.getClass()).insert(objeto);
    } else {
        throw new RuntimeException(ErrorMessages.NAO_EXISTE_DATA_SOURCE.toString());
    }
}

and there is the declaration of my datasource

public interface DataSource<T> {

List<T> getAll();
T findById(Integer id);

void openConection();
void closeConection();

void update(T valor);
void update(List<T> valores);

T insert(T valor);
List<T> insert(List<T> valores);

void delete(T valor);
void delete(List<T> valores);

Integer getLastInsertedRowId();
T converter(Cursor cursor);
}

My problem is that java dont know what kind of object it will receive as parameter so I cant call the insert method as I am trying to do, I need to find a work around but I cant think on nothing.. I wish someone can bring me some light, because I really think this is a good aproach to control the API of my DAO objects.. Thanks a lot

The only way to make it type-safe is to add a method to your DataSource interface which returns the required type, ie:

Class<T> getDataType();

Then you can turn your Map operations into type-safe code by introducing a helper method per operation:

public Object insert(Object objeto){
  final DataSource<?> dataSource = dataSourcesMap.get(objeto.getClass());
  if(dataSource!=null) {
      assert dataSource.getDataType().isInstance(objeto);
      return doInsert(dataSource, objeto);
  } else {
     throw new RuntimeException(ErrorMessages.NAO_EXISTE_DATA_SOURCE.toString());
  }
}
private static <T> T doInsert(DataSource<T> ds, Object o) {
  return ds.insert(ds.getDataType().cast(o));
}

The only way without such a method is to work without type safety. If your Map is private and you maintain the mappings correctly so that you always have a valid Class -key → value's type parameter relationship this could be an option. In this case, use the helper method as above but replace the ds.getDataType().cast(o) by an unsafe type-cast: (T)o .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM