From a82ecb60d6ee7ee46e5800088f1769f86872077b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 29 Aug 2016 17:13:03 +0100 Subject: Initial support for static methods/fields on interfaces --- .../couscous/frontends/java/JavaReader.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/main/java/org/zwobble/couscous/frontends') 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 1c4ac81..5202127 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -115,11 +115,18 @@ public class JavaReader { ScalarType name = erasure(typeOf(type.resolveBinding())); Scope scope = topScope.enterClass(name); List typeParameters = readTypeParameters(scope.getIdentifier(), type); - TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); + TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations(), type.isInterface()); Set superTypes = superTypes(type); if (type.isInterface()) { // TODO: throw exception if other parts of body are declared - return InterfaceNode.declareInterface(name, typeParameters, superTypes, body.getMethods()); + return InterfaceNode.declareInterface( + name, + typeParameters, + superTypes, + body.getFields(), + body.getStaticConstructor(), + body.getMethods() + ); } else { return ClassNode.declareClass( name, @@ -233,7 +240,7 @@ public class JavaReader { GeneratedClosure readAnonymousClass(Scope outerScope, AnonymousClassDeclaration declaration) { ScalarType className = generateAnonymousName(declaration); Scope scope = outerScope.enterClass(className); - TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations()); + TypeDeclarationBody bodyDeclarations = readTypeDeclarationBody(scope, className, declaration.bodyDeclarations(), false); AnonymousClass anonymousClass = new AnonymousClass( Optional.of((AnonymousType) typeOf(declaration.resolveBinding())), superTypes(declaration), @@ -357,7 +364,7 @@ public class JavaReader { return ((AbstractTypeDeclaration)node).resolveBinding(); } - private TypeDeclarationBody readTypeDeclarationBody(Scope scope, ScalarType type, List bodyDeclarations) { + private TypeDeclarationBody readTypeDeclarationBody(Scope scope, ScalarType type, List bodyDeclarations, boolean isInterface) { TypeDeclarationBody.Builder body = TypeDeclarationBody.builder(); for (Object declaration : bodyDeclarations) { @@ -370,7 +377,7 @@ public class JavaReader { readStatementBody(scope, initializer.getBody()))); tryCast(FieldDeclaration.class, declaration) - .ifPresent(field -> readField(body, scope, type, field)); + .ifPresent(field -> readField(body, scope, type, field, isInterface)); tryCast(TypeDeclaration.class, declaration) .ifPresent(typeDeclaration -> classes.add(readNestedTypeDeclaration(body, typeDeclaration))); @@ -406,22 +413,22 @@ public class JavaReader { .orElse(typeNode); } - private void readField(TypeDeclarationBody.Builder builder, Scope scope, ScalarType declaringType, FieldDeclaration field) { + private void readField(TypeDeclarationBody.Builder builder, Scope scope, ScalarType declaringType, FieldDeclaration field, boolean isInterface) { @SuppressWarnings("unchecked") List fragments = field.fragments(); + boolean isStatic = isInterface || Modifier.isStatic(field.getModifiers()); Type type = typeOf(field.getType()); fragments.forEach(fragment -> { - builder.field(field(Modifier.isStatic(field.getModifiers()), fragment.getName().getIdentifier(), type)); + builder.field(field(isStatic, fragment.getName().getIdentifier(), type)); if (fragment.getInitializer() != null) { ExpressionNode value = readExpression(scope, type, fragment.getInitializer()); String name = fragment.getName().getIdentifier(); - boolean isStatic = Modifier.isStatic(field.getModifiers()); Receiver receiver = isStatic ? staticReceiver(declaringType) : instanceReceiver(thisReference(declaringType)); StatementNode assignment = assignStatement(fieldAccess(receiver, name, type), value); - builder.addInitializer(isStatic,assignment); + builder.addInitializer(isStatic, assignment); } }); -- cgit v1.2.3