blob: ff671cde03e5025ff8c448f2c4d6b0ccdd3b4ece (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package org.zwobble.couscous.backends.naming;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import org.zwobble.couscous.ast.MethodSignature;
import org.zwobble.couscous.types.*;
import static com.google.common.collect.Iterables.transform;
import static org.zwobble.couscous.util.ExtraIterables.lazyMap;
import static org.zwobble.couscous.util.ExtraLists.list;
public class Names {
public static String toUniqueName(MethodSignature signature) {
return Joiner.on("__").join(Iterables.concat(
list(signature.getName()),
transform(signature.getArguments(), Names::typeToString),
list(typeToString(signature.getReturnType()))));
}
private static String typeToString(Type argument) {
return argument.accept(new Type.Visitor<String>() {
@Override
public String visit(ScalarType type) {
return type.getSimpleName();
}
@Override
public String visit(TypeParameter parameter) {
// TODO: should we care about the type?
Iterable<String> parts = lazyMap(parameter.getDeclaringScope().getParts(), part -> part.getName().replace(".", "_"));
return String.join("_", parts) + "_" + parameter.getName();
}
@Override
public String visit(ParameterizedType type) {
return typeToString(type.getRawType());
}
@Override
public String visit(BoundTypeParameter type) {
return typeToString(type.getParameter());
}
});
}
}
|