我正在从java生成一些scala源代码。结果是这样的:
def monitor(value: => Builder[com.test.Monitor]): this.type = inject("monitor", value)
生成Builder中的类型。我这样做了:
private static String constructGenericScalaTypeName(Class type) {
String name = type.getName();
if (name == null || name.isEmpty()) {
name = "_";
}
return name;
}
但这并不是一直有效。例如,如果我有这样的事情:
public interface ExceptionCatcher<T>
我相信结果必须是:
def monitor(value: => Builder[com.test.ExceptionCatcher[_]]): this.type = inject("catcher", value)
有什么方法可以做到这一点吗?
答案 0 :(得分:0)
我有一个不太好的溶液:
private static String constructGenericScalaTypeName(Class type) {
String name = type.getName();
if (name == null || name.isEmpty()) {
name = "_";
} else {
TypeVariable<Class>[] types = type.getTypeParameters();
if (types.length != 0) {
StringBuilder builder = new StringBuilder(name);
builder.append("[");
for (int i = 0; i < types.length; i++) {
builder.append("_,");
}
builder.delete(builder.length() -1, builder.length());
builder.append("]");
name = builder.toString();
}
}
return name;
}