advantageous/qbit

Problem with generics

abullon opened this issue · 1 comments

Hi,

I'm implementing a generic CRUD service, when I try to initiate the server I have this error

17:28:18.689 [main] INFO i.a.qbit.queue.impl.BasicQueue - Queue done creating Send Queue /services/cupones limit 100 checkEvery 10 tryTransfer falsepollTimeWait/polltime 15 17:28:18.689 [main] INFO i.a.q.service.impl.ServiceQueueImpl - Starting service cuponService 17:28:18.689 [main] INFO i.a.qbit.queue.impl.BasicQueue - Starting queue listener for Send Queue /services/cupones io.advantageous.qbit.service.impl.BaseServiceQueueImpl$3@51931956 17:28:18.689 [QueueListener|Send Queue booneventmanager] INFO i.a.q.b.events.impl.BoonEventManager - EventManagerQBIT_SYSTEM::joinService:: cuponService joined QBIT_SYSTEM Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class at io.advantageous.qbit.meta.builder.ServiceMethodMetaBuilder.deduceReturnTypes(ServiceMethodMetaBuilder.java:164) at io.advantageous.qbit.meta.builder.ServiceMethodMetaBuilder.build(ServiceMethodMetaBuilder.java:139) at io.advantageous.qbit.meta.builder.ServiceMetaBuilder.addMethod(ServiceMetaBuilder.java:170) at io.advantageous.qbit.meta.builder.ServiceMetaBuilder.lambda$addMethods$97(ServiceMetaBuilder.java:116) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418) at io.advantageous.qbit.meta.builder.ServiceMetaBuilder.addMethods(ServiceMetaBuilder.java:116) at io.advantageous.qbit.meta.builder.ContextMetaBuilder.addService(ContextMetaBuilder.java:366) at io.advantageous.qbit.meta.builder.ContextMetaBuilder.addService(ContextMetaBuilder.java:377) at io.advantageous.qbit.server.HttpRequestServiceServerHandlerUsingMetaImpl.addRestSupportFor(HttpRequestServiceServerHandlerUsingMetaImpl.java:307) at io.advantageous.qbit.server.ServiceEndpointServerImpl.initServices(ServiceEndpointServerImpl.java:327) at io.advantageous.qbit.server.ServiceEndpointServerImpl.initServices(ServiceEndpointServerImpl.java:340) at com.fiveadvantages.chaos.server.Launcher.main(Launcher.java:23) 17:28:18.742 [QueueListener|Send Queue healthserviceimpl] INFO i.a.q.s.health.HealthServiceImpl - HealthService::register() CuponService 10000 MILLISECONDS

My generic service is :

`
import com.fiveadvantages.chaos.datalayer.dto.CuponDTO;
import com.fiveadvantages.chaos.datalayer.dto.DTO;
import com.fiveadvantages.chaos.datalayer.entity.Cupon;
import com.fiveadvantages.chaos.datalayer.entity.Entity;
import com.fiveadvantages.chaos.util.Util;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import io.advantageous.boon.core.reflection.BeanUtils;
import io.advantageous.qbit.annotation.PathVariable;
import io.advantageous.qbit.annotation.RequestMapping;
import io.advantageous.qbit.annotation.RequestMethod;

import java.util.List;
public class GenericCRUDService<K extends Entity,T extends DTO,ID> {
private final static String DATABASE_URL = "jdbc:postgresql://localhost:5432/bd";
private final static String DATABASE_USER = "postgres";
private final static String DATABASE_PASSWORD = "********";
private K entity;
final Class entityClass;
private T DTO;
final Class dtoClass;
private Dao<K, ID> genericDAO;

public GenericCRUDService(Class<K> entityClass, Class<T> dtoClass) {
    this.entityClass = entityClass;
    this.dtoClass = dtoClass;
}

@RequestMapping(value = "", method = RequestMethod.GET)
public List<T> listAll() {
    ConnectionSource connectionSource = null;
    List<T> lista = null;
    try {
        connectionSource = new JdbcPooledConnectionSource( DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
        genericDAO = DaoManager.createDao(connectionSource, entityClass);
        lista = Util.CopyCollectionProperties(genericDAO.queryForAll(),dtoClass);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connectionSource != null) {
            try {
                connectionSource.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return lista;
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public CuponDTO getById(@PathVariable("id") ID id) {
ConnectionSource connectionSource = null;
CuponDTO cupon = new CuponDTO();
try {
connectionSource = new JdbcPooledConnectionSource ( DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
genericDAO = DaoManager.createDao(connectionSource, entityClass);
BeanUtils.copyProperties(genericDAO.queryForId(id),cupon);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connectionSource != null) {
try {
connectionSource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return cupon;
}

@RequestMapping(value = "", method = RequestMethod.POST)
public T create(final T dto) {
    ConnectionSource connectionSource = null;

    try {
        K entity = entityClass.newInstance();
        connectionSource = new JdbcPooledConnectionSource (DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
        genericDAO = DaoManager.createDao(connectionSource, entityClass);
        BeanUtils.copyProperties(dto,entity);
        BeanUtils.copyProperties(genericDAO.createIfNotExists(entity),dto);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connectionSource != null) {
            try {
                connectionSource.close();
            } catch (Exception e) {
                e.printStackTrace();
        }
        }
    }
    return dto;
}

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public T updateById(@PathVariable("id") ID id,final T dto) {
    ConnectionSource connectionSource = null;
    System.out.println("ID : "+id);
    try {
        connectionSource = new JdbcPooledConnectionSource ( DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
        genericDAO = DaoManager.createDao(connectionSource, entityClass);
        K entity = genericDAO.queryForId(id);
        BeanUtils.copyProperties(dto,entity);
        genericDAO.update(entity);
        BeanUtils.copyProperties(entity,dto);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connectionSource != null) {
            try {
                connectionSource.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return dto;
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteById(@PathVariable("id") ID id) {
    ConnectionSource connectionSource = null;
    String response = "Elemento eliminado con exito";
    try {
        connectionSource = new JdbcPooledConnectionSource ( DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
        genericDAO = DaoManager.createDao(connectionSource, entityClass);
        genericDAO.deleteById(id);
    } catch (Exception e) {
        response = "Ocurrio un error al intentar eliminar";
        e.printStackTrace();
    } finally {
        if (connectionSource != null) {
            try {
                connectionSource.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}

}
`
The child Class is :

`import ch.qos.logback.classic.Logger;
import com.fiveadvantages.chaos.datalayer.dto.CuponDTO;
import com.fiveadvantages.chaos.datalayer.entity.Cupon;
import com.fiveadvantages.chaos.service.generic.GenericCRUDService;
import io.advantageous.qbit.annotation.RequestMapping;
import org.slf4j.LoggerFactory;

@RequestMapping("/cupones")
public class CuponService extends GenericCRUDService<Cupon,CuponDTO,Integer> {

private Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

public CuponService() {
     super(Cupon.class, CuponDTO.class);
}

}`

My pom.xml

`


4.0.0

<groupId>com.5advantages.incubator</groupId>
<artifactId>chaos</artifactId>
<version>1.0-STARTUP</version>
io.advantageous.qbit qbit-admin 1.10.0.RELEASE io.advantageous.qbit qbit-vertx 1.10.0.RELEASE com.j256.ormlite ormlite-core 4.48 com.j256.ormlite ormlite-jdbc 4.48 commons-beanutils commons-beanutils 1.9.3
<!--LOG-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.12</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
</dependency>

<!--POSTGRESQL-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.4</version>
</dependency>

`