Added support for streaming List and ArrayList natively. A List will be
unmarshalled into an ArrayList on the receiver. Along the way, I improved support for generic types as arguments to invocation services (which required one unfortunate "sweeping" warning suppression, but since this is in generated code, I think we can be sure it won't be doing anything untoward). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4375 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -24,6 +24,9 @@ package com.threerings.io;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Code to read and write basic object types (like arrays of primitives,
|
||||
* {@link Integer} instances, {@link Double} instances, etc.).
|
||||
@@ -50,6 +53,8 @@ public class BasicStreamers
|
||||
float[].class,
|
||||
double[].class,
|
||||
Object[].class,
|
||||
List.class,
|
||||
ArrayList.class,
|
||||
};
|
||||
|
||||
/** An array of instances of all of the basic streamers. */
|
||||
@@ -72,6 +77,8 @@ public class BasicStreamers
|
||||
new FloatArrayStreamer(),
|
||||
new DoubleArrayStreamer(),
|
||||
new ObjectArrayStreamer(),
|
||||
new ListStreamer(),
|
||||
new ListStreamer(),
|
||||
};
|
||||
|
||||
/** Streams {@link Boolean} instances. */
|
||||
@@ -643,4 +650,42 @@ public class BasicStreamers
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Streams {@link List} instances. */
|
||||
public static class ListStreamer extends Streamer
|
||||
{
|
||||
// documentation inherited
|
||||
public Object createObject (ObjectInputStream in)
|
||||
throws IOException
|
||||
{
|
||||
return new ArrayList();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void writeObject (
|
||||
Object object, ObjectOutputStream out, boolean useWriter)
|
||||
throws IOException
|
||||
{
|
||||
List list = (List)object;
|
||||
int ecount = list.size();
|
||||
out.writeInt(ecount);
|
||||
for (int ii = 0; ii < ecount; ii++) {
|
||||
out.writeObject(list.get(ii));
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void readObject (
|
||||
Object object, ObjectInputStream in, boolean useReader)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
@SuppressWarnings("unchecked") ArrayList<Object> value =
|
||||
(ArrayList<Object>)object;
|
||||
int ecount = in.readInt();
|
||||
value.ensureCapacity(ecount);
|
||||
for (int ii = 0; ii < ecount; ii++) {
|
||||
value.add(in.readObject());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ public class GenDObjectTask extends Task
|
||||
// if this field is an array, we need its component types
|
||||
if (ftype.isArray()) {
|
||||
Class<?> etype = ftype.getComponentType();
|
||||
ctx.put("elemtype", GenUtil.simpleName(etype));
|
||||
ctx.put("elemtype", GenUtil.simpleName(etype, null));
|
||||
ctx.put("wrapelem", GenUtil.boxArgument(etype, "value"));
|
||||
ctx.put("wrapoelem", GenUtil.boxArgument(etype, "ovalue"));
|
||||
}
|
||||
@@ -283,7 +283,8 @@ public class GenDObjectTask extends Task
|
||||
ParameterizedType pt = (ParameterizedType)t;
|
||||
if (pt.getActualTypeArguments().length > 0) {
|
||||
ctx.put("etype", GenUtil.simpleName(
|
||||
(Class<?>)pt.getActualTypeArguments()[0]));
|
||||
(Class<?>)pt.getActualTypeArguments()[0],
|
||||
null));
|
||||
}
|
||||
} else {
|
||||
ctx.put("etype", "DSet.Entry");
|
||||
|
||||
@@ -74,7 +74,7 @@ public class GenServiceTask extends InvocationTask
|
||||
|
||||
public String getName ()
|
||||
{
|
||||
String name = GenUtil.simpleName(listener);
|
||||
String name = GenUtil.simpleName(listener, null);
|
||||
name = StringUtil.replace(name, "Listener", "");
|
||||
int didx = name.indexOf(".");
|
||||
return name.substring(didx+1);
|
||||
@@ -140,7 +140,8 @@ public class GenServiceTask extends InvocationTask
|
||||
Class[] args = m.getParameterTypes();
|
||||
for (int aa = 0; aa < args.length; aa++) {
|
||||
if (_ilistener.isAssignableFrom(args[aa]) &&
|
||||
GenUtil.simpleName(args[aa]).startsWith(sname + ".")) {
|
||||
GenUtil.simpleName(
|
||||
args[aa], null).startsWith(sname + ".")) {
|
||||
checkedAdd(listeners, new ServiceListener(
|
||||
service, args[aa], imports));
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -50,8 +51,8 @@ public class GenUtil
|
||||
Pattern.compile("^\\s*public\\s+(interface|class)\\s+(\\S+)(\\W|$)");
|
||||
|
||||
/**
|
||||
* Returns the name of the supplied class as it would likely appear in
|
||||
* code using the class (no package prefix, arrays specified as
|
||||
* Returns the name of the supplied class as it would likely appear in code
|
||||
* using the class (no package prefix, arrays specified as
|
||||
* <code>type[]</code>).
|
||||
*/
|
||||
public static String simpleName (Field field)
|
||||
@@ -64,7 +65,8 @@ public class GenUtil
|
||||
field.getGenericType();
|
||||
cname = atype.getGenericComponentType().toString();
|
||||
} else {
|
||||
return simpleName(clazz.getComponentType()) + "[]";
|
||||
return simpleName(clazz.getComponentType(),
|
||||
field.getGenericType()) + "[]";
|
||||
}
|
||||
} else if (field.getGenericType() instanceof ParameterizedType) {
|
||||
cname = field.getGenericType().toString();
|
||||
@@ -78,18 +80,22 @@ public class GenUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the supplied class as it would likely appear in
|
||||
* code using the class (no package prefix, arrays specified as
|
||||
* Returns the name of the supplied class as it would likely appear in code
|
||||
* using the class (no package prefix, arrays specified as
|
||||
* <code>type[]</code>).
|
||||
*/
|
||||
public static String simpleName (Class<?> clazz)
|
||||
public static String simpleName (Class<?> clazz, Type type)
|
||||
{
|
||||
if (clazz.isArray()) {
|
||||
return simpleName(clazz.getComponentType()) + "[]";
|
||||
return simpleName(clazz.getComponentType(), type) + "[]";
|
||||
} else {
|
||||
String cname = clazz.getName();
|
||||
if (type instanceof ParameterizedType) {
|
||||
cname = type.toString();
|
||||
}
|
||||
Package pkg = clazz.getPackage();
|
||||
int offset = (pkg == null) ? 0 : pkg.getName().length()+1;
|
||||
String name = clazz.getName().substring(offset);
|
||||
String name = cname.substring(offset);
|
||||
return StringUtil.replace(name, "$", ".");
|
||||
}
|
||||
}
|
||||
@@ -122,10 +128,10 @@ public class GenUtil
|
||||
}
|
||||
|
||||
/**
|
||||
* "Unboxes" the supplied argument, ie. turning an
|
||||
* <code>Integer</code> object into an <code>int</code>.
|
||||
* "Unboxes" the supplied argument, ie. turning an <code>Integer</code>
|
||||
* object into an <code>int</code>.
|
||||
*/
|
||||
public static String unboxArgument (Class<?> clazz, String name)
|
||||
public static String unboxArgument (Class<?> clazz, Type type, String name)
|
||||
{
|
||||
if (clazz == Boolean.TYPE) {
|
||||
return "((Boolean)" + name + ").booleanValue()";
|
||||
@@ -144,7 +150,7 @@ public class GenUtil
|
||||
} else if (clazz == Double.TYPE) {
|
||||
return "((Double)" + name + ").doubleValue()";
|
||||
} else {
|
||||
return "(" + simpleName(clazz) + ")" + name + "";
|
||||
return "(" + simpleName(clazz, type) + ")" + name + "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -54,7 +55,7 @@ public abstract class InvocationTask extends Task
|
||||
|
||||
public String getMarshaller ()
|
||||
{
|
||||
String name = GenUtil.simpleName(listener);
|
||||
String name = GenUtil.simpleName(listener, null);
|
||||
// handle ye olde special case
|
||||
if (name.equals("InvocationService.InvocationListener")) {
|
||||
return "ListenerMarshaller";
|
||||
@@ -106,7 +107,8 @@ public abstract class InvocationTask extends Task
|
||||
// InvocationService listeners, we need to import its
|
||||
// marshaller as well
|
||||
if (_ilistener.isAssignableFrom(arg) &&
|
||||
!GenUtil.simpleName(arg).startsWith("InvocationService")) {
|
||||
!GenUtil.simpleName(
|
||||
arg, null).startsWith("InvocationService")) {
|
||||
String mname = arg.getName();
|
||||
mname = StringUtil.replace(mname, "Service", "Marshaller");
|
||||
mname = StringUtil.replace(mname, "Listener", "Marshaller");
|
||||
@@ -135,11 +137,12 @@ public abstract class InvocationTask extends Task
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Class<?>[] args = method.getParameterTypes();
|
||||
Type[] ptypes = method.getGenericParameterTypes();
|
||||
for (int ii = skipFirst ? 1 : 0; ii < args.length; ii++) {
|
||||
if (buf.length() > 0) {
|
||||
buf.append(", ");
|
||||
}
|
||||
buf.append(GenUtil.simpleName(args[ii]));
|
||||
buf.append(GenUtil.simpleName(args[ii], ptypes[ii]));
|
||||
buf.append(" arg").append(skipFirst ? ii : ii+1);
|
||||
}
|
||||
return buf.toString();
|
||||
@@ -172,12 +175,13 @@ public abstract class InvocationTask extends Task
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Class<?>[] args = method.getParameterTypes();
|
||||
Type[] ptypes = method.getGenericParameterTypes();
|
||||
for (int ii = (listenerMode ? 0 : 1); ii < args.length; ii++) {
|
||||
if (buf.length() > 0) {
|
||||
buf.append(", ");
|
||||
}
|
||||
buf.append(unboxArgument(args[ii], listenerMode ? ii : ii-1,
|
||||
listenerMode));
|
||||
buf.append(unboxArgument(args[ii], ptypes[ii],
|
||||
listenerMode ? ii : ii-1, listenerMode));
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
@@ -192,12 +196,13 @@ public abstract class InvocationTask extends Task
|
||||
}
|
||||
|
||||
protected String unboxArgument (
|
||||
Class<?> clazz, int index, boolean listenerMode)
|
||||
Class<?> clazz, Type type, int index, boolean listenerMode)
|
||||
{
|
||||
if (listenerMode && _ilistener.isAssignableFrom(clazz)) {
|
||||
return "listener" + index;
|
||||
} else {
|
||||
return GenUtil.unboxArgument(clazz, "args[" + index + "]");
|
||||
return GenUtil.unboxArgument(
|
||||
clazz, type, "args[" + index + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ public class ${name}Dispatcher extends InvocationDispatcher
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
// from InvocationDispatcher
|
||||
public InvocationMarshaller createMarshaller ()
|
||||
{
|
||||
return new ${name}Marshaller();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
@SuppressWarnings("unchecked") // from InvocationDispatcher
|
||||
public void dispatchRequest (
|
||||
ClientObject source, int methodId, Object[] args)
|
||||
throws InvocationException
|
||||
|
||||
Reference in New Issue
Block a user