aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-01 11:38:06 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-01 11:38:06 +0000
commit0aae239d0f3226d25a3cd44a0151e8a8db35a3eb (patch)
tree016ad751a64e7d4b146905827b5e045ef41b392a /src/main/java/org/zwobble
parent6a369dbd094d825812454eb39bfaaf84c1ce2148 (diff)
Share code between lambda and anonymous class reading for captures
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java33
1 files changed, 19 insertions, 14 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 70dc160..9b5f561 100644
--- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
+++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java
@@ -9,7 +9,6 @@ import org.zwobble.couscous.ast.VariableDeclaration;
import java.io.IOException;
import java.nio.file.Path;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -17,6 +16,7 @@ import java.util.function.Function;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
import static org.zwobble.couscous.ast.AnnotationNode.annotation;
import static org.zwobble.couscous.ast.AssignmentNode.assignStatement;
import static org.zwobble.couscous.ast.ConstructorNode.constructor;
@@ -70,16 +70,13 @@ public class JavaReader {
concat(lambda.getFormalArguments(), lambda.getBody()));
MethodNode method = MethodNode.method(
- Collections.emptyList(),
+ emptyList(),
false,
functionalInterfaceMethod.getName(),
lambda.getFormalArguments(),
- replaceCaptureReferences(className, lambda.getBody(), freeVariables));
+ lambda.getBody());
- ClassNode classNode = new ClassNodeBuilder(className)
- .constructor(buildConstructor(className, freeVariables))
- .method(method)
- .build();
+ ClassNode classNode = classWithCapture(className, freeVariables, emptyList(), ImmutableList.of(method));
classes.add(classNode);
return new GeneratedClosure(classNode.getName(), freeVariables);
@@ -138,21 +135,29 @@ public class JavaReader {
TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(declaration.bodyDeclarations());
List<VariableDeclaration> freeVariables = findFreeVariables(bodyDeclarations.getNodes());
+ ClassNode classNode = classWithCapture(className, freeVariables, bodyDeclarations.getFields(), bodyDeclarations.getMethods());
+ classes.add(classNode);
+ return new GeneratedClosure(classNode.getName(), freeVariables);
+ }
+
+ private ClassNode classWithCapture(
+ TypeName className,
+ List<VariableDeclaration> freeVariables,
+ List<FieldDeclarationNode> declaredFields,
+ List<MethodNode> methods
+ ) {
Iterable<FieldDeclarationNode> captureFields = transform(
freeVariables,
freeVariable -> field(freeVariable.getName(), freeVariable.getType()));
- List<FieldDeclarationNode> fields = ImmutableList.copyOf(Iterables.concat(bodyDeclarations.getFields(), captureFields));
- List<MethodNode> methods = eagerMap(bodyDeclarations.getMethods(), method ->
- method.mapBody(body -> replaceCaptureReferences(className, body, freeVariables)));
+ List<FieldDeclarationNode> fields = ImmutableList.copyOf(Iterables.concat(declaredFields, captureFields));
- ClassNode classNode = ClassNode.declareClass(
+ return ClassNode.declareClass(
className,
fields,
buildConstructor(className, freeVariables),
- methods);
- classes.add(classNode);
- return new GeneratedClosure(classNode.getName(), freeVariables);
+ eagerMap(methods, method ->
+ method.mapBody(body -> replaceCaptureReferences(className, body, freeVariables))));
}
private TypeName generateClassName(AnonymousClassDeclaration declaration) {