Patch from Mike T to support parameterized types in genrecord.

This commit is contained in:
Michael Bayne
2012-07-25 16:38:43 +00:00
parent 42e8d6e75e
commit 7f839f8ddd
3 changed files with 46 additions and 11 deletions
@@ -12,7 +12,10 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; 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.types.Reference;
import org.apache.tools.ant.util.ClasspathUtils; 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.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@@ -259,7 +264,7 @@ public class GenRecordTask extends Task
// create our substitution mappings // create our substitution mappings
Map<String, String> fsubs = Maps.newHashMap(subs); 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("field", fname);
fsubs.put("capfield", StringUtil.unStudlyName(fname).toUpperCase()); fsubs.put("capfield", StringUtil.unStudlyName(fname).toUpperCase());
@@ -444,19 +449,37 @@ public class GenRecordTask extends Task
return name; return name;
} }
protected static String getTypeName (Class<?> clazz) protected static String getTypeName (Type type)
{ {
if (clazz.isArray()) { if (type instanceof Class) {
Class<?> cclass = clazz.getComponentType(); Class<?> clazz = (Class<?>)type;
return (cclass.isPrimitive() ? cclass.getSimpleName() : getTypeName(cclass)) + "[]"; if (clazz.isArray()) {
} else { Class<?> cclass = clazz.getComponentType();
Class<?> nclass = clazz.isPrimitive() ? BOXES.get(clazz) : clazz; return (cclass.isPrimitive() ? cclass.getSimpleName() : getTypeName(cclass)) + "[]";
Class<?> eclass = nclass.getEnclosingClass();
if (eclass != null) {
return getTypeName(eclass) + "." + nclass.getSimpleName();
} else { } else {
return nclass.getSimpleName(); Class<?> nclass = clazz.isPrimitive() ? BOXES.get(clazz) : clazz;
Class<?> eclass = nclass.getEnclosingClass();
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.Arrays;
import java.util.Properties; import java.util.Properties;
import com.google.common.collect.Lists;
import com.samskivert.jdbc.ConnectionProvider; import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.StaticConnectionProvider; import com.samskivert.jdbc.StaticConnectionProvider;
import com.samskivert.util.Calendars; import com.samskivert.util.Calendars;
@@ -100,6 +102,7 @@ public abstract class TestBase
rec.type = EnumKeyRecord.Type.A; rec.type = EnumKeyRecord.Type.A;
rec.lastModified = tnow; rec.lastModified = tnow;
rec.numbers = new int[] { 9, 0, 2, 1, 0 }; rec.numbers = new int[] { 9, 0, 2, 1, 0 };
rec.strList = Lists.newArrayList("foo", "bar", "Hello", "World");
return rec; return rec;
} }
@@ -117,5 +120,6 @@ public abstract class TestBase
assertEquals(expect.type, got.type); assertEquals(expect.type, got.type);
assertEquals(expect.lastModified, got.lastModified); assertEquals(expect.lastModified, got.lastModified);
assertTrue(Arrays.equals(expect.numbers, got.numbers)); assertTrue(Arrays.equals(expect.numbers, got.numbers));
assertEquals(expect.strList, got.strList);
} }
} }
@@ -4,14 +4,18 @@
package com.samskivert.depot; package com.samskivert.depot;
import java.util.List;
import java.sql.Date; import java.sql.Date;
import java.sql.Timestamp; import java.sql.Timestamp;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import com.samskivert.depot.Transformers;
import com.samskivert.depot.annotation.Entity; import com.samskivert.depot.annotation.Entity;
import com.samskivert.depot.annotation.Id; import com.samskivert.depot.annotation.Id;
import com.samskivert.depot.annotation.Index; import com.samskivert.depot.annotation.Index;
import com.samskivert.depot.annotation.Transform;
import com.samskivert.depot.expression.ColumnExp; 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<Date> CREATED = colexp(_R, "created");
public static final ColumnExp<Timestamp> LAST_MODIFIED = colexp(_R, "lastModified"); public static final ColumnExp<Timestamp> LAST_MODIFIED = colexp(_R, "lastModified");
public static final ColumnExp<int[]> NUMBERS = colexp(_R, "numbers"); public static final ColumnExp<int[]> NUMBERS = colexp(_R, "numbers");
public static final ColumnExp<List<String>> STR_LIST = colexp(_R, "strList");
// AUTO-GENERATED: FIELDS END // AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 3; public static final int SCHEMA_VERSION = 3;
@@ -55,6 +60,9 @@ public class TestRecord extends PersistentRecord
public int[] numbers; public int[] numbers;
@Transform(Transformers.StringIterable.class)
public List<String> strList;
@Override @Override
public String toString () public String toString ()
{ {