I am trying to generate the instance of typed class at run time to work with redis.
Here is my code, but its failing to create the instance of typed class T.
interface ObjectFactory<T>
{
T create() throws Exception;
}
Here is the class that create the instance of T
import java.lang.reflect.ParameterizedType;
public class RedisObjectConstructor<T> implements ObjectFactory<T>
{
@SuppressWarnings("unchecked")
@Override
public T create() throws Exception
{
try
{
return (T) ((Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).newInstance();
}
catch (Exception e)
{
throw new Exception();
}
}
}
Here is logic to actually set and get value of domain field:
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.stereotype.Component;
@Component
public class RedisBasicEntity<T>
{
private final ObjectFactory<T> factory;
public RedisBasicEntity(ObjectFactory<T> factory)
{
this.factory = factory;
}
public Map<String, String> getFields(T redisDomain, Class<? extends Annotation> ann)
{
Map<String, String> redisHash = new HashMap<String, String>();
Class<?> clazz = redisDomain.getClass();
while (clazz != null)
{
for (Field field : clazz.getDeclaredFields())
{
if (field.isAnnotationPresent(ann))
{
field.setAccessible(true);
try
{
Object value = field.get(redisDomain);
if (value != null)
{
redisHash.put(field.getName(), value.toString());
System.out.println(field.getName() + ":" + value.toString());
}
}
catch (IllegalArgumentException e)
{
}
catch (IllegalAccessException e)
{
}
}
}
c = c.getSuperclass();
}
return redisHash;
}
public T getRedisEntity(Map<String, String> redisHash, Class<? extends Annotation> ann) throws Exception
{
T redisDomain = factory.create();
Class<?> clazz = redisDomain.getClass();
while (clazz != null)
{
for (Field field : clazz.getDeclaredFields())
{
if (field.isAnnotationPresent(ann))
{
field.setAccessible(true);
try
{
FieldUtils.writeDeclaredField(field, field.getName(), redisHash.get(field.getName()));
}
catch (Exception e)
{
}
}
}
}
return redisDomain;
}
}
Here is my test class:
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.ranker.annotations.rediskey.RedisHashKey;
import com.ranker.api.dao.iListQualityDAO;
import com.ranker.app.redis.domain.list.ListQuality;
import com.ranker.app.redis.repository.support.RedisBasicEntity;
import com.ranker.app.redis.repository.support.RedisObjectConstructor;
public class ListQualityDAO implements iListQualityDAO
{
@Autowired
private RedisBasicEntity<ListQuality> redisBasicEntity;
public static ListQuality saveListQuality()
{
RedisObjectConstructor<ListQuality> redisObjectConstructor = new RedisObjectConstructor<ListQuality>();
RedisBasicEntity<ListQuality> redisBasicEntity = new RedisBasicEntity<ListQuality>(redisObjectConstructor);
ListQuality listQuality = new ListQuality();
listQuality.setBurialScore(10.0);
Map<String, String> redisHash = redisBasicEntity.getFields(listQuality, RedisHashKey.class);
return null;
}
public static ListQuality getListQuality() throws Exception
{
RedisObjectConstructor<ListQuality> redisObjectConstructor = new RedisObjectConstructor<ListQuality>();
RedisBasicEntity<ListQuality> redisBasicEntity = new RedisBasicEntity<ListQuality>(redisObjectConstructor);
Map<String, String> redisHash = new HashMap<String, String>();
redisHash.put("satisfactionScore", "10.00");
redisHash.put("burialScore", "5.00");
redisHash.put("burialState", "true");
ListQuality listQuality = redisBasicEntity.getRedisEntity(redisHash, RedisHashKey.class);
return listQuality;
}
public static void main(String args[]) throws Exception
{
ListQualityDAO.getListQuality();
}
}
My Annotation that used to identify the field:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RedisHashKey {
}
Please let me know what am doing wrong here:
Here is the exception that am getting when i do test run:
Exception in thread "main" java.lang.Exception at com.ranker.app.redis.repository.support.RedisObjectConstructor.create(RedisObjectConstructor.java:19) at com.ranker.app.redis.repository.support.RedisBasicEntity.getRedisEntity(RedisBasicEntity.java:61) at com.ranker.api.dao.impl.ListQualityDAO.getListQuality(ListQualityDAO.java:37) at com.ranker.api.dao.impl.ListQualityDAO.main(ListQualityDAO.java:45)
Aucun commentaire:
Enregistrer un commentaire