I am writing this query service that is supposed to work with pairs of predefined classes. However, it seems redundant to me that when using this service class, I need to pass both the class type and the class object. For example, to request a Contact object, I need to provide both Contact.class and both, for example the following:
lookupService = new MyLookupServiceImpl<Contact>(Contact.class);
In this case, is there a way to initialize the class without passing it to "Contact.class"? The service class is as follows:
public class MyLookupServiceImpl<T> { private Class<T> cls; public MyLookupServiceImpl(Class<T> clz){ this.cls = clz; } public T queryObject(String sql) { try { QueryResult result = ConnectionFactory.getConnection().query(sql); if(result.getSize()>0){ T obj=null; MyObject sObj = result.getRecords()[0]; MyObjectBuilder<T> ob = new MyObjectBuilder<T>(cls); obj = ob.buildORMObject(sObj); return obj; } } catch (ConnectionException e) { logger.warn(e.toString()); } return null; } }
Any suggestion would be appreciated.
Thanks,
source share