Skip to content

Instantly share code, notes, and snippets.

@flaviocdc
Created January 16, 2013 19:17
Show Gist options
  • Select an option

  • Save flaviocdc/4549893 to your computer and use it in GitHub Desktop.

Select an option

Save flaviocdc/4549893 to your computer and use it in GitHub Desktop.
package br.ufrj.labase.model;
import br.ufrj.labase.model.exception.UnexpectedException;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.log4j.Logger;
public class PlainClassMap
{
private static final Logger log = Logger.getLogger(PlainClassMap.class);
private static Map<Class, ToPlainClassDescriptor> toPlainClass = new HashMap();
private PlainClassMap()
{
throw new UnsupportedOperationException("static class!");
}
public static ToPlainClassDescriptor getPlainClassDescriptor(Class<? extends Object> type)
{
if (type == null) {
throw new IllegalArgumentException("illegal argument: ToPlainClassDescriptor.getPlainClassDescriptor(null)");
}
ToPlainClassDescriptor plainClassDescriptor = (ToPlainClassDescriptor)toPlainClass.get(type);
if (plainClassDescriptor != null) {
return plainClassDescriptor;
}
ToPlainClass toPlainClass = (ToPlainClass)type.getAnnotation(ToPlainClass.class);
if (toPlainClass == null) {
return null;
}
String toObject = null;
String constructor = toPlainClass.toObject();
if (!constructor.equals("constructor")) {
toObject = constructor;
constructor = null;
}
return getPlainClassDescriptor(type, toPlainClass.target(), toPlainClass.toPlainObject(), toObject, constructor);
}
private static ToPlainClassDescriptor getPlainClassDescriptor(Class sourceType, Class targetType, String toPlainObjectMethod, String toObjectMethod, String constructor) {
ToPlainClassDescriptor plainClassDescriptor = null;
try {
plainClassDescriptor = new ToPlainClassDescriptor(sourceType, targetType, constructor, toObjectMethod, toPlainObjectMethod, null);
} catch (NoSuchMethodException e) {
new UnexpectedException("não conseguiu construir ToPlainClassDescriptor", e);
} catch (SecurityException e) {
new UnexpectedException("não conseguiu construir ToPlainClassDescriptor", e);
} catch (ClassNotFoundException e) {
new UnexpectedException("não conseguiu construir ToPlainClassDescriptor", e);
}
if (plainClassDescriptor != null) {
toPlainClass.put(plainClassDescriptor.getType(), plainClassDescriptor);
}
return plainClassDescriptor;
}
private static void init()
{
Map primitiveTypes = new HashMap();
primitiveTypes.put(Boolean.TYPE.getName(), Boolean.TYPE);
primitiveTypes.put(Byte.TYPE.getName(), Byte.TYPE);
primitiveTypes.put(Character.TYPE.getName(), Character.TYPE);
primitiveTypes.put(Short.TYPE.getName(), Short.TYPE);
primitiveTypes.put(Integer.TYPE.getName(), Integer.TYPE);
primitiveTypes.put(Long.TYPE.getName(), Long.TYPE);
primitiveTypes.put(Float.TYPE.getName(), Float.TYPE);
primitiveTypes.put(Double.TYPE.getName(), Double.TYPE);
Properties properties = new Properties();
try
{
properties.load(PlainClassMap.class.getClassLoader().getResourceAsStream("plainclasses.config"));
} catch (IOException e) {
log.warn("erro ao carregar plainClasses.config", e);
} catch (IllegalArgumentException e) {
log.warn("erro ao carregar plainClasses.config", e);
} catch (NullPointerException e) {
log.warn("erro ao carregar plainclasses.config", e);
}
for (Iterator i$ = properties.keySet().iterator(); i$.hasNext(); ) { Object key = i$.next();
if (key.toString().indexOf("@") == -1)
{
try
{
Class sourceType = Class.forName(key.toString());
String targetName = properties.getProperty(key.toString());
Class targetType = (Class)primitiveTypes.get(targetName);
if (targetType == null) {
targetType = Class.forName(targetName);
}
String constructor = null;
String toObjectMethod = properties.getProperty(key.toString() + "@toObject");
String toPlainObjectMethod = properties.getProperty(key.toString() + "@toPlainObject");
if (toObjectMethod.equals("constructor")) {
constructor = toObjectMethod;
toObjectMethod = null;
}
getPlainClassDescriptor(sourceType, targetType, toPlainObjectMethod, toObjectMethod, constructor);
}
catch (ClassNotFoundException e) {
log.error("Classe não encontrada - " + e.getMessage());
} catch (UnexpectedException e) {
log.error(e.getMessage());
}
}
}
log.debug("mapeamentos encontrados: " + toPlainClass.size());
}
static
{
init();
}
public static final class ToPlainClassDescriptor
{
private Class plainType;
private Constructor constructor;
private Method toObject;
private Method toPlainObject;
private Class type;
private ToPlainClassDescriptor(Class type, Class plainType, String constructor, String toObjectMethodName, String toPlainObjectMethodName)
throws SecurityException, NoSuchMethodException, ClassNotFoundException
{
this.plainType = plainType;
this.type = type;
this.toPlainObject = type.getMethod(toPlainObjectMethodName, new Class[0]);
if ((constructor == null) || (constructor.trim().equals("")))
this.toObject = getToObjectStaticMethod(type, toObjectMethodName);
else
this.constructor = type.getConstructor(new Class[] { plainType });
}
private Method getToObjectStaticMethod(Class source, String method)
throws SecurityException, NoSuchMethodException, ClassNotFoundException
{
PlainClassMap.log.debug("getToObjectStaticMethod: " + method);
int index = method.lastIndexOf('.');
Class s = source;
String m = method;
if (index != -1) {
String className = method.substring(0, index);
m = method.substring(index + 1, method.length());
s = Class.forName(className);
}
return s.getMethod(m, new Class[] { this.plainType });
}
public Class getPlainType()
{
return this.plainType;
}
public Class getType()
{
return this.type;
}
public Object convertToObject(Object value) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("convertendo... " + value);
Object o = null;
if (this.constructor == null)
o = this.toObject.invoke(null, new Object[] { value });
else {
o = this.constructor.newInstance(new Object[] { value });
}
return o;
}
public Object convertToPlainObject(Object o) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
return this.toPlainObject.invoke(o, new Object[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment