Handle service methods with forall types.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6582 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2011-04-02 00:47:50 +00:00
parent fc7706c79d
commit 248795ba57
3 changed files with 32 additions and 2 deletions
@@ -25,6 +25,7 @@ import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.io.File;
@@ -138,6 +139,15 @@ public abstract class InvocationTask extends GenTask
}
}
public String typeParams () {
List<String> params = Lists.newArrayList();
for (Type type : method.getGenericParameterTypes()) {
collectTypeParams(type, params);
}
// the trailing space in '> ' is needed
return params.isEmpty() ? "" : StringUtil.toString(params, "<", "> ");
}
public String getArgList () {
StringBuilder buf = new StringBuilder();
Type[] ptypes = method.getGenericParameterTypes();
@@ -319,6 +329,8 @@ public abstract class InvocationTask extends GenTask
}
} else if (type instanceof GenericArrayType) {
addImportsForType(((GenericArrayType)type).getGenericComponentType(), imports);
} else if (type instanceof TypeVariable) {
// nothing needed
} else {
System.err.println(Logger.format(
"Unhandled Type in adding imports for a service", "type", type, "typeClass",
@@ -326,6 +338,24 @@ public abstract class InvocationTask extends GenTask
}
}
protected void collectTypeParams (Type type, List<String> params) {
if (type instanceof TypeVariable) {
String tvar = ((TypeVariable)type).getName();
if (!params.contains(tvar)) params.add(tvar);
} else if (type instanceof ParameterizedType) {
for (Type pt : ((ParameterizedType)type).getActualTypeArguments()) {
collectTypeParams(pt, params);
}
} else if (type instanceof WildcardType) {
for (Type lb : ((WildcardType)type).getLowerBounds()) {
collectTypeParams(lb, params);
}
for (Type ub : ((WildcardType)type).getUpperBounds()) {
collectTypeParams(ub, params);
}
} // else nada
}
protected String boxArgument (Class<?> clazz, int index) {
if (_ilistener.isAssignableFrom(clazz)) {
return GenUtil.boxArgument(clazz, "listener" + index);
@@ -63,7 +63,7 @@ public class {{name}}Marshaller extends InvocationMarshaller
public static final int {{code}} = {{-index}};
// from interface {{name}}Service
public void {{method.name}} ({{getArgList}})
public {{typeParams}}void {{method.name}} ({{getArgList}})
{
{{#listenerArgs}}
{{marshaller}} listener{{index}} = new {{marshaller}}();
@@ -19,7 +19,7 @@ public interface {{name}}Provider extends InvocationProvider
/**
* Handles a {@link {{name}}Service#{{method.name}}} request.
*/
void {{method.name}} (ClientObject caller{{#hasArgs}}, {{/hasArgs}}{{getArgList}}){{^listenerArgs.isEmpty}}
{{typeParams}}void {{method.name}} (ClientObject caller{{#hasArgs}}, {{/hasArgs}}{{getArgList}}){{^listenerArgs.isEmpty}}
throws InvocationException{{/listenerArgs.isEmpty}};
{{/methods}}