From f31e1bc8ecd4a2323da72e7e78819d0f733bb27d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 11 Feb 2016 22:46:09 +0000 Subject: Read static field initialisers in Java frontend --- .../couscous/frontends/java/JavaReader.java | 44 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'src/main') 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 90723b0..1bf666f 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -65,7 +65,7 @@ public class JavaReader { TypeName name = generateClassName(ast); Scope scope = Scope.create().enterClass(name); TypeDeclaration type = (TypeDeclaration)ast.types().get(0); - TypeDeclarationBody body = readTypeDeclarationBody(scope, type.bodyDeclarations()); + TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); return ClassNode.declareClass( name, superTypes(type), @@ -146,7 +146,7 @@ public class JavaReader { GeneratedClosure readAnonymousClass(Scope outerScope, AnonymousClassDeclaration declaration) { TypeName className = generateAnonymousName(declaration); Scope scope = outerScope.enterClass(className); - TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, declaration.bodyDeclarations()); + TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations()); AnonymousClass anonymousClass = new AnonymousClass( superTypes(declaration), bodyDeclarations.getFields(), @@ -263,7 +263,7 @@ public class JavaReader { } } - private TypeDeclarationBody readTypeDeclarationBody(Scope scope, List bodyDeclarations) { + private TypeDeclarationBody readTypeDeclarationBody(Scope scope, TypeName type, List bodyDeclarations) { ImmutableList.Builder methods = ImmutableList.builder(); ConstructorNode constructor = ConstructorNode.DEFAULT; @@ -275,10 +275,7 @@ public class JavaReader { } } // TODO: handle instance initializers - List initializers = ofType(bodyDeclarations, Initializer.class); - List staticConstructor = eagerFlatMap( - initializers, - initializer -> readStatement(scope, initializer.getBody())); + List staticConstructor = readStaticConstructor(scope, type, bodyDeclarations); return new TypeDeclarationBody( readFields(ofType(bodyDeclarations, FieldDeclaration.class)), staticConstructor, @@ -286,6 +283,39 @@ public class JavaReader { methods.build()); } + private List readStaticConstructor(Scope scope, TypeName type, List bodyDeclarations) { + return eagerFlatMap( + bodyDeclarations, + declaration -> readStaticConstructorStatements(scope, type, declaration)); + } + + private Iterable readStaticConstructorStatements(Scope scope, TypeName type, Object declaration) { + if (declaration instanceof Initializer) { + return readStatement(scope, ((Initializer) declaration).getBody()); + } else if (declaration instanceof FieldDeclaration) { + FieldDeclaration field = (FieldDeclaration) declaration; + if (Modifier.isStatic(field.getModifiers())) { + // TODO: remove duplication with readField + @SuppressWarnings("unchecked") + List fragments = field.fragments(); + TypeName fieldType = typeOf(field.getType()); + return eagerFlatMap(fragments, fragment -> { + if (fragment.getInitializer() == null) { + return list(); + } else { + ExpressionNode value = readExpression(scope, fieldType, fragment.getInitializer()); + String name = fragment.getName().getIdentifier(); + return list(assignStatement(fieldAccess(type, name, fieldType), value)); + } + }); + } else { + return list(); + } + } else { + return list(); + } + } + private List readFields(List fields) { return eagerFlatMap(fields, this::readField); } -- cgit v1.2.3