diff options
Diffstat (limited to 'src/main/java/org')
| -rw-r--r-- | src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java | 5 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/couscous/frontends/java/UniqueIdentifiers.java | 17 |
2 files changed, 19 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index fca7353..84e95e8 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -36,6 +36,7 @@ public class JavaReader { return cons(reader.readCompilationUnit(ast), reader.classes.build()); } + private final UniqueIdentifiers identifiers = new UniqueIdentifiers(); private final ImmutableList.Builder<ClassNode> classes; private int anonymousClassCount = 0; @@ -105,11 +106,9 @@ public class JavaReader { } private ConstructorNode buildConstructor(TypeName type, List<VariableDeclaration> freeVariables) { - // TODO: find a more reliable of generating IDs. For instance, this clashes if - // the same variable is captured more than once Map<String, VariableDeclaration> argumentDeclarationsById = Maps.transformValues( Maps.uniqueIndex(freeVariables, VariableDeclaration::getId), - freeVariable -> var(freeVariable.getId() + "__capture", freeVariable.getName(), freeVariable.getType())); + freeVariable -> var(identifiers.generate(freeVariable.getId() + "__capture"), freeVariable.getName(), freeVariable.getType())); List<FormalArgumentNode> arguments = eagerMap( freeVariables, freeVariable -> formalArg(argumentDeclarationsById.get(freeVariable.getId()))); diff --git a/src/main/java/org/zwobble/couscous/frontends/java/UniqueIdentifiers.java b/src/main/java/org/zwobble/couscous/frontends/java/UniqueIdentifiers.java new file mode 100644 index 0000000..1d28de0 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/frontends/java/UniqueIdentifiers.java @@ -0,0 +1,17 @@ +package org.zwobble.couscous.frontends.java; + +import java.util.HashMap; +import java.util.Map; + +public class UniqueIdentifiers { + private final Map<String, Integer> identifiers; + + public UniqueIdentifiers() { + identifiers = new HashMap<>(); + } + + public String generate(String identifier) { + int index = identifiers.computeIfAbsent(identifier, x -> 0); + return identifier + "__" + index; + } +} |
