diff options
Diffstat (limited to 'src')
3 files changed, 99 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNode.java b/src/main/java/org/zwobble/couscous/ast/ClassNode.java index d0598d8..d2c64f5 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNode.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNode.java @@ -4,18 +4,50 @@ import java.util.List; import org.zwobble.couscous.ast.visitors.NodeVisitor; -import lombok.Value; - -@Value public class ClassNode implements Node { public static ClassNodeBuilder builder(String name) { return new ClassNodeBuilder(name); } - TypeName name; - List<FieldDeclarationNode> fields; - ConstructorNode constructor; - List<MethodNode> methods; + public static ClassNode declareClass( + TypeName name, + List<FieldDeclarationNode> fields, + ConstructorNode constructor, + List<MethodNode> methods) { + return new ClassNode(name, fields, constructor, methods); + } + + private final TypeName name; + private final List<FieldDeclarationNode> fields; + private final ConstructorNode constructor; + private final List<MethodNode> methods; + + private ClassNode( + TypeName name, + List<FieldDeclarationNode> fields, + ConstructorNode constructor, + List<MethodNode> methodNodes) { + this.name = name; + this.fields = fields; + this.constructor = constructor; + methods = methodNodes; + } + + public TypeName getName() { + return name; + } + + public List<FieldDeclarationNode> getFields() { + return fields; + } + + public ConstructorNode getConstructor() { + return constructor; + } + + public List<MethodNode> getMethods() { + return methods; + } public String getSimpleName() { return name.getSimpleName(); @@ -25,4 +57,54 @@ public class ClassNode implements Node { public void accept(NodeVisitor visitor) { visitor.visit(this); } + + @Override + public String toString() { + return "ClassNode(name=" + name + ", fields=" + fields + + ", constructor=" + constructor + ", methods=" + methods + ")"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((constructor == null) ? 0 : constructor.hashCode()); + result = prime * result + ((fields == null) ? 0 : fields.hashCode()); + result = prime * result + ((methods == null) ? 0 : methods.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ClassNode other = (ClassNode) obj; + if (constructor == null) { + if (other.constructor != null) + return false; + } else if (!constructor.equals(other.constructor)) + return false; + if (fields == null) { + if (other.fields != null) + return false; + } else if (!fields.equals(other.fields)) + return false; + if (methods == null) { + if (other.methods != null) + return false; + } else if (!methods.equals(other.methods)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } } diff --git a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java index 1b8aea3..b4e3891 100644 --- a/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java +++ b/src/main/java/org/zwobble/couscous/ast/ClassNodeBuilder.java @@ -67,7 +67,7 @@ public class ClassNodeBuilder { } public ClassNode build() { - return new ClassNode( + return ClassNode.declareClass( name, fields.build(), constructor.orElse(constructorBuilder().build()), diff --git a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java index c7276b1..5fbd256 100644 --- a/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java +++ b/src/test/java/org/zwobble/couscous/tests/ValueObjectTests.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.tests; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.List; import java.util.function.Function; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -14,6 +15,7 @@ import org.junit.runners.Parameterized.Parameters; import org.zwobble.couscous.ast.AnnotationNode; import org.zwobble.couscous.ast.AssignableExpressionNode; import org.zwobble.couscous.ast.AssignmentNode; +import org.zwobble.couscous.ast.ClassNode; import org.zwobble.couscous.ast.ExpressionNode; import org.zwobble.couscous.ast.LiteralNode; import org.zwobble.couscous.ast.VariableDeclaration; @@ -42,6 +44,7 @@ public class ValueObjectTests { return asList(new Object[][] { {AnnotationNode.class}, {AssignmentNode.class}, + {ClassNode.class}, {VariableDeclaration.class} }); } @@ -108,6 +111,8 @@ public class ValueObjectTests { private static Object generateFirstInstance(Class<?> type) { if (type.equals(String.class)) { return "[string 1]"; + } else if (type.equals(List.class)) { + return asList("[list 1]"); } else if (type.equals(PrimitiveValue.class)) { return generateFirstInstance(StringValue.class); } else if (type.equals(ExpressionNode.class)) { @@ -122,6 +127,8 @@ public class ValueObjectTests { private static Object generateSecondInstance(Class<?> type) { if (type.equals(String.class)) { return "[string 2]"; + } else if (type.equals(List.class)) { + return asList("[list 2]"); } else if (type.equals(PrimitiveValue.class)) { return generateSecondInstance(StringValue.class); } else if (type.equals(ExpressionNode.class)) { @@ -136,6 +143,8 @@ public class ValueObjectTests { private static Object generateInstance(Class<?> type) { if (type.equals(String.class)) { return "[string]"; + } else if (type.equals(List.class)) { + return asList("[list]"); } else if (type.equals(PrimitiveValue.class)) { return generateInstance(StringValue.class); } else if (type.equals(ExpressionNode.class)) { |
