Compare commits

..

No commits in common. "c135819ba086267d05475794c14acc805e1bccc5" and "093beb9ef1692e436855d71c33fcdc77b450cf53" have entirely different histories.

4 changed files with 38 additions and 254 deletions

View File

@ -56,13 +56,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
MemberNode memberNode = (MemberNode) visit(member);
if(memberNode != null) {
if(memberNode instanceof ConstructorNode){
hasConstructor = true;
}
classNode.addMember(memberNode);
}
}
if(!hasConstructor){
BlockNode blockNode = new BlockNode();
@ -93,7 +91,6 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return mainMethod;
} else {
if(ctx.type() != null) {
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()) {
@ -102,16 +99,6 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
}
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;
}
} else {
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()) {
@ -120,16 +107,6 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
}
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;
}
}
}
}
@ -139,7 +116,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
if(ctx.AccessModifier() != null) {
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} else {
return new FieldNode(new AccessModifierNode("public"), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
}
}
@ -312,12 +289,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
MethodCallNode methodCallStatementExpressionNode;
if(ctx.target() != null) {
methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText());
} else {
methodCallStatementExpressionNode = new MethodCallNode(null, ctx.Identifier().getText());
}
MethodCallNode methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText());
for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) {
methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod));
}

View File

@ -4,27 +4,12 @@ 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;
@ -36,7 +21,6 @@ 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;
@ -57,11 +41,11 @@ class AstBuilderTest {
@Test
@DisplayName("Multiple Empty Classes Test")
public void multipleEmptyClassesTest() {
ClassNode class1 = Helper.generateEmptyClass("TestClass1");
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
ClassNode classNode1 = Helper.generateEmptyClass("TestClass1");
ClassNode classNode2 = Helper.generateEmptyClass("TestClass2");
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
expected.addClass(class2);
expected.addClass(classNode1);
expected.addClass(classNode2);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -70,9 +54,9 @@ class AstBuilderTest {
@Test
@DisplayName("Empty Class Test with Constructor")
public void emptyClassWithConstructorTest() {
ClassNode class1 = Helper.generateEmptyClass("TestClass");
ClassNode classNode = Helper.generateEmptyClass("TestClass");
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -81,11 +65,11 @@ class AstBuilderTest {
@Test
@DisplayName("Field Test")
public void fieldTest() {
ClassNode class1 = Helper.generateEmptyClass("TestClass");
class1.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
ClassNode classNode = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -94,11 +78,11 @@ class AstBuilderTest {
@Test
@DisplayName("Field Test with Accessmodifier")
public void fieldTestWithModifier() {
ClassNode class1 = Helper.generateEmptyClass("TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
ClassNode classNode = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -107,236 +91,64 @@ 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"));
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Comments.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
//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"));
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);
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("This Dot Test")
public void thisDotTest(){
BlockNode block = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
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);
//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");
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);
//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));
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/VoidMethod.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
//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");
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);
//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");
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);
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Char Test")
public void charTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
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);
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Null Test")
public void nullTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
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);
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test

View File

@ -2,7 +2,7 @@ class TestClass{
public int a;
public TestClass() {
public TestClass{
this.a = 1;
}
}