Patch from Mike T to support parameterized types in genrecord.
This commit is contained in:
@@ -12,7 +12,10 @@ import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -27,6 +30,8 @@ import org.apache.tools.ant.types.FileSet;
|
||||
import org.apache.tools.ant.types.Reference;
|
||||
import org.apache.tools.ant.util.ClasspathUtils;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
@@ -259,7 +264,7 @@ public class GenRecordTask extends Task
|
||||
|
||||
// create our substitution mappings
|
||||
Map<String, String> fsubs = Maps.newHashMap(subs);
|
||||
fsubs.put("type", getTypeName(f.getType()));
|
||||
fsubs.put("type", getTypeName(f.getGenericType()));
|
||||
fsubs.put("field", fname);
|
||||
fsubs.put("capfield", StringUtil.unStudlyName(fname).toUpperCase());
|
||||
|
||||
@@ -444,19 +449,37 @@ public class GenRecordTask extends Task
|
||||
return name;
|
||||
}
|
||||
|
||||
protected static String getTypeName (Class<?> clazz)
|
||||
protected static String getTypeName (Type type)
|
||||
{
|
||||
if (type instanceof Class) {
|
||||
Class<?> clazz = (Class<?>)type;
|
||||
if (clazz.isArray()) {
|
||||
Class<?> cclass = clazz.getComponentType();
|
||||
return (cclass.isPrimitive() ? cclass.getSimpleName() : getTypeName(cclass)) + "[]";
|
||||
} else {
|
||||
Class<?> nclass = clazz.isPrimitive() ? BOXES.get(clazz) : clazz;
|
||||
Class<?> eclass = nclass.getEnclosingClass();
|
||||
if (eclass != null) {
|
||||
return getTypeName(eclass) + "." + nclass.getSimpleName();
|
||||
} else {
|
||||
return nclass.getSimpleName();
|
||||
return (eclass == null) ? nclass.getSimpleName() :
|
||||
getTypeName(eclass) + "." + nclass.getSimpleName();
|
||||
}
|
||||
|
||||
} else if (type instanceof ParameterizedType) {
|
||||
ParameterizedType paramType = (ParameterizedType)type;
|
||||
Type[] typeArgs = paramType.getActualTypeArguments();
|
||||
String tparams = (typeArgs == null || typeArgs.length == 0) ? "" :
|
||||
"<" + Joiner.on(", ").join(
|
||||
Lists.transform(Lists.newArrayList(typeArgs), new Function<Type, String>() {
|
||||
@Override public String apply (Type input) {
|
||||
return getTypeName(input);
|
||||
}
|
||||
})) + ">";
|
||||
return getTypeName(paramType.getRawType()) + tparams;
|
||||
|
||||
} else if (type instanceof GenericArrayType) {
|
||||
return getTypeName(((GenericArrayType)type).getGenericComponentType()) + "[]";
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown kind of type '" + type + "'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.sql.Timestamp;
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import com.samskivert.jdbc.ConnectionProvider;
|
||||
import com.samskivert.jdbc.StaticConnectionProvider;
|
||||
import com.samskivert.util.Calendars;
|
||||
@@ -100,6 +102,7 @@ public abstract class TestBase
|
||||
rec.type = EnumKeyRecord.Type.A;
|
||||
rec.lastModified = tnow;
|
||||
rec.numbers = new int[] { 9, 0, 2, 1, 0 };
|
||||
rec.strList = Lists.newArrayList("foo", "bar", "Hello", "World");
|
||||
return rec;
|
||||
}
|
||||
|
||||
@@ -117,5 +120,6 @@ public abstract class TestBase
|
||||
assertEquals(expect.type, got.type);
|
||||
assertEquals(expect.lastModified, got.lastModified);
|
||||
assertTrue(Arrays.equals(expect.numbers, got.numbers));
|
||||
assertEquals(expect.strList, got.strList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,18 @@
|
||||
|
||||
package com.samskivert.depot;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.samskivert.depot.Transformers;
|
||||
import com.samskivert.depot.annotation.Entity;
|
||||
import com.samskivert.depot.annotation.Id;
|
||||
import com.samskivert.depot.annotation.Index;
|
||||
import com.samskivert.depot.annotation.Transform;
|
||||
import com.samskivert.depot.expression.ColumnExp;
|
||||
|
||||
/**
|
||||
@@ -31,6 +35,7 @@ public class TestRecord extends PersistentRecord
|
||||
public static final ColumnExp<Date> CREATED = colexp(_R, "created");
|
||||
public static final ColumnExp<Timestamp> LAST_MODIFIED = colexp(_R, "lastModified");
|
||||
public static final ColumnExp<int[]> NUMBERS = colexp(_R, "numbers");
|
||||
public static final ColumnExp<List<String>> STR_LIST = colexp(_R, "strList");
|
||||
// AUTO-GENERATED: FIELDS END
|
||||
|
||||
public static final int SCHEMA_VERSION = 3;
|
||||
@@ -55,6 +60,9 @@ public class TestRecord extends PersistentRecord
|
||||
|
||||
public int[] numbers;
|
||||
|
||||
@Transform(Transformers.StringIterable.class)
|
||||
public List<String> strList;
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user