diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index fcabdfa..7ee199b 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -56,10 +56,12 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { MemberNode memberNode = (MemberNode) visit(member); - if(memberNode instanceof ConstructorNode){ - hasConstructor = true; + if(memberNode != null) { + if(memberNode instanceof ConstructorNode){ + hasConstructor = true; + } + classNode.addMember(memberNode); } - classNode.addMember(memberNode); } if(!hasConstructor){ @@ -91,22 +93,43 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return mainMethod; } else { if(ctx.type() != null) { - MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); - if(ctx.parameterList() != null) { - for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { - methodNode.addParameter((ParameterNode) visit(parameter)); + if(ctx.AccessModifier() != null) { + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } } + return methodNode; + } else { + MethodNode methodNode = new MethodNode("public", createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } + } + return methodNode; } - return methodNode; } else { - MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); - if(ctx.parameterList() != null) { - for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { - methodNode.addParameter((ParameterNode) visit(parameter)); + if(ctx.AccessModifier() != null) { + MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } } + methodNode.block.addStatement(new ReturnNode(null)); + return methodNode; + } else { + MethodNode methodNode = new MethodNode("public", null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); + if(ctx.parameterList() != null) { + for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { + methodNode.addParameter((ParameterNode) visit(parameter)); + } + } + methodNode.block.addStatement(new ReturnNode(null)); + return methodNode; } - methodNode.block.addStatement(new ReturnNode(null)); - return methodNode; } } } @@ -116,7 +139,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { if(ctx.AccessModifier() != null) { return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); } else { - return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); + return new FieldNode(new AccessModifierNode("public"), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); } } @@ -289,7 +312,12 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) { - MethodCallNode methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText()); + MethodCallNode methodCallStatementExpressionNode; + if(ctx.target() != null) { + methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText()); + } else { + methodCallStatementExpressionNode = new MethodCallNode(null, ctx.Identifier().getText()); + } for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) { methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod)); } diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index f239a41..b163a2a 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -4,12 +4,27 @@ package parser; import ast.ASTNode; import ast.ClassNode; import ast.ProgramNode; +import ast.expressions.IExpressionNode; +import ast.expressions.unaryexpressions.MemberAccessNode; +import ast.expressions.unaryexpressions.UnaryNode; import ast.members.ConstructorNode; import ast.members.FieldNode; import ast.members.MemberNode; +import ast.members.MethodNode; +import ast.parameters.ParameterNode; +import ast.statementexpressions.AssignNode; +import ast.statementexpressions.AssignableNode; +import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; +import ast.statements.BlockNode; +import ast.statements.IStatementNode; +import ast.statements.ReturnNode; import ast.type.AccessModifierNode; +import ast.type.EnumValueNode; +import ast.type.ValueNode; import ast.type.type.BaseType; +import ast.type.type.ITypeNode; import ast.type.type.TypeEnum; +import com.fasterxml.jackson.annotation.JsonFormat; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; @@ -21,6 +36,7 @@ import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; import java.io.IOException; +import java.lang.reflect.Member; import static org.assertj.core.api.Assertions.assertThat; @@ -41,11 +57,11 @@ class AstBuilderTest { @Test @DisplayName("Multiple Empty Classes Test") public void multipleEmptyClassesTest() { - ClassNode classNode1 = Helper.generateEmptyClass("TestClass1"); - ClassNode classNode2 = Helper.generateEmptyClass("TestClass2"); + ClassNode class1 = Helper.generateEmptyClass("TestClass1"); + ClassNode class2 = Helper.generateEmptyClass("TestClass2"); ProgramNode expected = new ProgramNode(); - expected.addClass(classNode1); - expected.addClass(classNode2); + expected.addClass(class1); + expected.addClass(class2); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java"); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); @@ -54,9 +70,9 @@ class AstBuilderTest { @Test @DisplayName("Empty Class Test with Constructor") public void emptyClassWithConstructorTest() { - ClassNode classNode = Helper.generateEmptyClass("TestClass"); + ClassNode class1 = Helper.generateEmptyClass("TestClass"); ProgramNode expected = new ProgramNode(); - expected.addClass(classNode); + expected.addClass(class1); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java"); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); @@ -65,11 +81,11 @@ class AstBuilderTest { @Test @DisplayName("Field Test") public void fieldTest() { - ClassNode classNode = Helper.generateEmptyClass("TestClass"); - classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a")); + ClassNode class1 = Helper.generateEmptyClass("TestClass"); + class1.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a")); ProgramNode expected = new ProgramNode(); - expected.addClass(classNode); + expected.addClass(class1); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java"); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); @@ -78,11 +94,11 @@ class AstBuilderTest { @Test @DisplayName("Field Test with Accessmodifier") public void fieldTestWithModifier() { - ClassNode classNode = Helper.generateEmptyClass("TestClass"); - classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + ClassNode class1 = Helper.generateEmptyClass("TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); ProgramNode expected = new ProgramNode(); - expected.addClass(classNode); + expected.addClass(class1); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java"); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); @@ -91,64 +107,236 @@ class AstBuilderTest { @Test @DisplayName("Commments Ignore Test") public void commmentsIgnoreTest(){ + ClassNode class1 = Helper.generateEmptyClass("TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a")); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Comments.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Paramerter Test") public void constructorParameterTest(){ + BlockNode block = new BlockNode(); + block.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", block); + constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a")); + constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b")); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(constructor); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorParameter.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("This Dot Test") public void thisDotTest(){ + BlockNode block = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + ValueNode value = new ValueNode(EnumValueNode.INT_VALUE, "1"); + IExpressionNode expression = new UnaryNode(value); + + block.addStatement(new AssignNode(assignable, expression)); + block.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", block); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + class1.addMember(constructor); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ThisDot.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor This Dot Test") public void constructorThisDotTest(){ + BlockNode block = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + IExpressionNode expression = new UnaryNode("a"); + + block.addStatement(new AssignNode(assignable, expression)); + block.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", block); + constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a")); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a")); + class1.addMember(constructor); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorThisDot.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Void Methoden Test") public void voidMethodenTest(){ + ClassNode class1 = Helper.generateEmptyClass("TestClass"); + BlockNode block = new BlockNode(); + block.addStatement(new ReturnNode(null)); + class1.addMember(new MethodNode("public", null, true, "test", block)); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/VoidMethod.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Method call Test") public void constructorMethodCallTest(){ + BlockNode blockCon = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + IExpressionNode expression = new UnaryNode(new MethodCallNode(null, "testMethod")); + + blockCon.addStatement(new AssignNode(assignable, expression)); + blockCon.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); + + BlockNode blockMethod = new BlockNode(); + blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1")))); + MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + class1.addMember(constructor); + class1.addMember(method); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCall.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Method call Parameters Test") public void constructorMethodCallParametersTest(){ + BlockNode blockCon = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + MethodCallNode methodCall = new MethodCallNode(null, "testMethod"); + methodCall.addExpression(new UnaryNode("a")); + IExpressionNode expression = new UnaryNode(methodCall); + + blockCon.addStatement(new AssignNode(assignable, expression)); + blockCon.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); + constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a")); + + BlockNode blockMethod = new BlockNode(); + blockMethod.addStatement(new ReturnNode(new UnaryNode("a"))); + MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod); + method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a")); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + class1.addMember(constructor); + class1.addMember(method); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCallParameters.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Char Test") public void charTest(){ + BlockNode blockCon = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + MethodCallNode methodCall = new MethodCallNode(null, "testMethod"); + methodCall.addExpression(new UnaryNode("a")); + IExpressionNode expression = new UnaryNode(methodCall); + + blockCon.addStatement(new AssignNode(assignable, expression)); + blockCon.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); + constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a")); + + BlockNode blockMethod = new BlockNode(); + blockMethod.addStatement(new ReturnNode(new UnaryNode("a"))); + MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod); + method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a")); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a")); + class1.addMember(constructor); + class1.addMember(method); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Char.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Null Test") public void nullTest(){ + BlockNode blockCon = new BlockNode(); + MemberAccessNode memberAccess = new MemberAccessNode(true); + memberAccess.addIdentifier("a"); - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + AssignableNode assignable = new AssignableNode(memberAccess); + + blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null")))); + blockCon.addStatement(new ReturnNode(null)); + ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); + + ClassNode class1 = new ClassNode("public", "TestClass"); + class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + class1.addMember(constructor); + + ProgramNode expected = new ProgramNode(); + expected.addClass(class1); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Null.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test diff --git a/src/test/resources/input/javaCases/ConstructorMehtodCallParameters.java b/src/test/resources/input/javaCases/ConstructorMethodCallParameters.java similarity index 100% rename from src/test/resources/input/javaCases/ConstructorMehtodCallParameters.java rename to src/test/resources/input/javaCases/ConstructorMethodCallParameters.java diff --git a/src/test/resources/input/javaCases/ThisDot.java b/src/test/resources/input/javaCases/ThisDot.java index ddff42c..866b39e 100644 --- a/src/test/resources/input/javaCases/ThisDot.java +++ b/src/test/resources/input/javaCases/ThisDot.java @@ -2,7 +2,7 @@ class TestClass{ public int a; - public TestClass{ + public TestClass() { this.a = 1; } } \ No newline at end of file