From 7556b9f5907f88e82e43e8183a94d3e4dcb72c0a Mon Sep 17 00:00:00 2001 From: luca9913 Date: Tue, 6 Jun 2023 19:43:56 +0200 Subject: [PATCH] Fixed parser bug that caused TestComplete to fail --- .../StatementGenerator.java | 767 +++++++++--------- 1 file changed, 374 insertions(+), 393 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index fd750afd..f156114b 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -22,11 +22,11 @@ import java.util.stream.Collectors; public class StatementGenerator { private JavaClassRegistry reg; - private Map fields; //PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen + private Map fields; // PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen private Map localVars; private GenericsRegistry generics; - public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics, Map fields, Map localVars){ + public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics, Map fields, Map localVars) { this.reg = reg; this.generics = generics; this.fields = fields; @@ -36,23 +36,23 @@ public class StatementGenerator { public ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) { List ret = new ArrayList<>(); List fps = new ArrayList<>(); - if(formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) - return new ParameterList(ret, new NullToken()); //Dann ist die Parameterliste leer + if (formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) + return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer - if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException(); + if (formalParameterListContext.lastFormalParameter().formalParameter() == null) + throw new NotImplementedException(); - if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null - && formalParameterListContext.formalParameters().formalParameter() != null){ + if (formalParameterListContext != null && formalParameterListContext.formalParameters() != null && formalParameterListContext.formalParameters().formalParameter() != null) { fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter()); } fps.add(formalParameterListContext.lastFormalParameter().formalParameter()); - for(Java8Parser.FormalParameterContext fp : fps){ + for (Java8Parser.FormalParameterContext fp : fps) { String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId()); RefTypeOrTPHOrWildcardOrGeneric type; - if(fp.unannType() != null){ + if (fp.unannType() != null) { type = TypeGenerator.convert(fp.unannType(), reg, generics); - }else{ + } else { type = TypePlaceholder.fresh(fp.getStart()); } ret.add(new FormalParameter(paramName, type, fp.getStart())); @@ -61,9 +61,9 @@ public class StatementGenerator { return new ParameterList(ret, ret.get(0).getOffset()); } - /* - * StatementGeneration: - */ + /* + * StatementGeneration: + */ private Statement convert(Java8Parser.StatementContext stmt) { if (stmt.statementWithoutTrailingSubstatement() != null) { @@ -77,69 +77,73 @@ public class StatementGenerator { } else if (stmt.ifThenStatement() != null) { return convert(stmt.ifThenStatement()); } else if (stmt.labeledStatement() != null) { - return convert(stmt.labeledStatement() ); - }else throw new NotImplementedException(); + return convert(stmt.labeledStatement()); + } else + throw new NotImplementedException(); } - private Statement convert(Java8Parser.StatementNoShortIfContext stmt){ - if(stmt.statementWithoutTrailingSubstatement() != null){ + private Statement convert(Java8Parser.StatementNoShortIfContext stmt) { + if (stmt.statementWithoutTrailingSubstatement() != null) { return convert(stmt.statementWithoutTrailingSubstatement()); - }else if(stmt.labeledStatementNoShortIf() != null){ + } else if (stmt.labeledStatementNoShortIf() != null) { return convert(stmt.labeledStatementNoShortIf()); - }else if(stmt.ifThenElseStatementNoShortIf() != null){ + } else if (stmt.ifThenElseStatementNoShortIf() != null) { return convert(stmt.ifThenElseStatementNoShortIf()); - }else if(stmt.whileStatementNoShortIf() != null){ + } else if (stmt.whileStatementNoShortIf() != null) { return convert(stmt.whileStatementNoShortIf()); - }else if(stmt.forStatementNoShortIf() != null){ + } else if (stmt.forStatementNoShortIf() != null) { return convert(stmt.forStatementNoShortIf()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { - if(stmt.block() != null){ + if (stmt.block() != null) { return convert(stmt.block(), false); - }else if(stmt.emptyStatement() != null){ + } else if (stmt.emptyStatement() != null) { return new EmptyStmt(stmt.getStart()); - }else if(stmt.expressionStatement() != null){ + } else if (stmt.expressionStatement() != null) { return convert(stmt.expressionStatement()); - }else if(stmt.assertStatement() != null){ + } else if (stmt.assertStatement() != null) { return convert(stmt.assertStatement()); - }else if(stmt.switchStatement() != null){ + } else if (stmt.switchStatement() != null) { return convert(stmt.switchStatement()); - }else if(stmt.doStatement() != null){ + } else if (stmt.doStatement() != null) { return convert(stmt.doStatement()); - }else if(stmt.breakStatement() != null){ + } else if (stmt.breakStatement() != null) { return convert(stmt.breakStatement()); - }else if(stmt.continueStatement() != null){ + } else if (stmt.continueStatement() != null) { return convert(stmt.continueStatement()); - }else if(stmt.returnStatement() != null){ + } else if (stmt.returnStatement() != null) { return convert(stmt.returnStatement()); - }else if(stmt.synchronizedStatement() != null){ + } else if (stmt.synchronizedStatement() != null) { return convert(stmt.synchronizedStatement()); - }else if(stmt.throwStatement() != null){ + } else if (stmt.throwStatement() != null) { return convert(stmt.throwStatement()); - }else if(stmt.tryStatement() != null){ + } else if (stmt.tryStatement() != null) { return convert(stmt.tryStatement()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } public Block convert(Java8Parser.BlockContext block, boolean addTrailingReturn) { List statements = new ArrayList<>(); - if(block.blockStatements() != null) - for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ + if (block.blockStatements() != null) + for (Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()) { List stmt = convert(statementContext); statements.addAll(stmt); } - if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements); + if (addTrailingReturn) + statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } private List convert(Java8Parser.BlockStatementContext statementContext) { - if(statementContext.localVariableDeclarationStatement() != null){ + if (statementContext.localVariableDeclarationStatement() != null) { return convert(statementContext.localVariableDeclarationStatement()); - }else if(statementContext.classDeclaration() != null){ + } else if (statementContext.classDeclaration() != null) { throw new NotImplementedException(); - }else{ + } else { return Arrays.asList(convert(statementContext.statement())); } } @@ -151,193 +155,190 @@ public class StatementGenerator { private Statement convert(Java8Parser.LabeledStatementContext labeledStatementContext) { throw new NotImplementedException(); - //return convert(labeledStatementContext.statement()); + // return convert(labeledStatementContext.statement()); } - private Statement convert(Java8Parser.LabeledStatementNoShortIfContext stmt){ + private Statement convert(Java8Parser.LabeledStatementNoShortIfContext stmt) { throw new NotImplementedException(); - //return convert(stmt.statementNoShortIf()); + // return convert(stmt.statementNoShortIf()); } - private Statement convert(Java8Parser.ExpressionStatementContext stmt){ + private Statement convert(Java8Parser.ExpressionStatementContext stmt) { return convert(stmt.statementExpression()); } private Statement convert(Java8Parser.StatementExpressionContext stmt) { - if(stmt.assignment() != null){ + if (stmt.assignment() != null) { return convert(stmt.assignment()); - }else if(stmt.preIncrementExpression() != null){ + } else if (stmt.preIncrementExpression() != null) { return convert(stmt.preIncrementExpression()); - }else if(stmt.preDecrementExpression() != null){ + } else if (stmt.preDecrementExpression() != null) { return convert(stmt.preDecrementExpression()); - }else if(stmt.postIncrementExpression() != null){ + } else if (stmt.postIncrementExpression() != null) { return convert(stmt.postIncrementExpression()); - }else if(stmt.postDecrementExpression() != null){ + } else if (stmt.postDecrementExpression() != null) { return convert(stmt.postDecrementExpression()); - }else if(stmt.methodInvocation() != null){ + } else if (stmt.methodInvocation() != null) { return convert(stmt.methodInvocation()); - }else if(stmt.classInstanceCreationExpression() != null){ + } else if (stmt.classInstanceCreationExpression() != null) { return convert(stmt.classInstanceCreationExpression()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - public Receiver getReceiver(Expression expr){ - if(expr instanceof StaticClassName){ + public Receiver getReceiver(Expression expr) { + if (expr instanceof StaticClassName) { return (Receiver) expr; - }else { + } else { return new ExpressionReceiver(expr); } } private Statement convert(Java8Parser.MethodInvocationContext methodInvocationContext) { String name; - if(methodInvocationContext.methodName()!=null){ + if (methodInvocationContext.methodName() != null) { name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ + } else {// if(methodInvocationContext.Identifier() != null){ name = methodInvocationContext.Identifier().getText(); } Expression receiver; - if(methodInvocationContext.typeName() != null){ + if (methodInvocationContext.typeName() != null) { receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ + } else if (methodInvocationContext.expressionName() != null) { receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.primary() != null){ + } else if (methodInvocationContext.primary() != null) { receiver = convert(methodInvocationContext.primary()); - }else if(methodInvocationContext.toString().startsWith("super")){ + } else if (methodInvocationContext.toString().startsWith("super")) { receiver = new Super(methodInvocationContext.getStart()); - }else if(methodInvocationContext.methodName() != null){ + } else if (methodInvocationContext.methodName() != null) { receiver = new This(methodInvocationContext.getStart()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); ArgumentList argumentList = convert(methodInvocationContext.argumentList()); - ArrayList signature = argumentList.getArguments().stream() - .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); //ReturnType - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), - getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), - signature, methodInvocationContext.getStart()); + ArrayList signature = argumentList.getArguments().stream().map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); // ReturnType + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), signature, methodInvocationContext.getStart()); ret.setStatement(); return ret; } private ArgumentList convert(Java8Parser.ArgumentListContext argumentListContext) { - if(argumentListContext == null)return new ArgumentList(new ArrayList<>(), new NullToken()); + if (argumentListContext == null) + return new ArgumentList(new ArrayList<>(), new NullToken()); List args = new ArrayList<>(); Token offset = new NullToken(); - for(Java8Parser.ExpressionContext expr : argumentListContext.expression()){ + for (Java8Parser.ExpressionContext expr : argumentListContext.expression()) { args.add(convert(expr)); } - if(args.size()>0)offset = args.get(0).getOffset(); + if (args.size() > 0) + offset = args.get(0).getOffset(); return new ArgumentList(args, offset); } /** - * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und - * einer Klassenangabe unterscheiden. + * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und einer Klassenangabe unterscheiden. + * * @param expression * @param offset * @return */ - private Expression generateLocalOrFieldVarOrClassName(String expression, Token offset){ + private Expression generateLocalOrFieldVarOrClassName(String expression, Token offset) { String[] parts = expression.split("\\."); - if(parts.length < 2){ - //Check for localVar: - if(localVars.get(expression) != null){ + if (parts.length < 2) { + // Check for localVar: + if (localVars.get(expression) != null) { return new LocalVar(expression, localVars.get(expression), offset); - }else{ - if(fields.get(expression) != null){//PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen - return new FieldVar(new This(offset), expression, fields.get(expression), offset); - } else { - //kann eigentlich nicht vorkommen - //Dann Muss es ein Feld sein! - return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); - }} + if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen + return new FieldVar(new This(offset), expression, fields.get(expression), offset); + + } else { + // kann eigentlich nicht vorkommen + // Dann Muss es ein Feld sein! + return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); + } + } } return generateFieldVarOrClassname(expression, offset); } - private Expression generateFieldVarOrClassname(String expression, Token offset){ + private Expression generateFieldVarOrClassname(String expression, Token offset) { String[] parts = expression.split("\\."); String whole = ""; Expression receiver = null; - for(String part : parts){ - whole+=part; - //Check for Classname: - if(reg.contains(whole)){ + for (String part : parts) { + whole += part; + // Check for Classname: + if (reg.contains(whole)) { receiver = new StaticClassName(reg.getName(whole), offset); } - whole+="."; + whole += "."; } - if(parts.length < 2 || parts[0].contentEquals("this") ){ + if (parts.length < 2 || parts[0].contentEquals("this")) { receiver = new This(offset); } else if (parts[0].contentEquals("super")) { - receiver = new Super(offset); - }else if(receiver == null){ //Handelt es sich um keinen Statischen Klassennamen: - String part = expression.substring(0,expression.length() - (1 + parts[parts.length-1].length())); + receiver = new Super(offset); + } else if (receiver == null) { // Handelt es sich um keinen Statischen Klassennamen: + String part = expression.substring(0, expression.length() - (1 + parts[parts.length - 1].length())); receiver = generateLocalOrFieldVarOrClassName(part, offset); } - return new FieldVar(receiver, parts[parts.length-1], TypePlaceholder.fresh(offset) ,offset); + return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset); } private Expression convert(Java8Parser.ExpressionNameContext expressionNameContext) { return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart()); } - private Statement convert(Java8Parser.ClassInstanceCreationExpressionContext newExpression) { Java8Parser.TypeArgumentsContext genericArgs = null; - if(newExpression.expressionName()!= null)throw new NotImplementedException(); - if(newExpression.typeArgumentsOrDiamond()!= null){ - if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){ + if (newExpression.expressionName() != null) + throw new NotImplementedException(); + if (newExpression.typeArgumentsOrDiamond() != null) { + if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) { genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); } } - if(newExpression.typeArguments()!= null)throw new NotImplementedException(); + if (newExpression.typeArguments() != null) + throw new NotImplementedException(); TerminalNode identifier = newExpression.Identifier(0); - RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics); + RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, identifier.getSymbol(), reg, generics); ArgumentList args = convert(newExpression.argumentList()); - ArrayList signature = args.getArguments().stream() - .map(x -> TypePlaceholder.fresh(newExpression.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - signature.add(TypePlaceholder.fresh(newExpression.getStart())); //ReturnType + ArrayList signature = args.getArguments().stream().map(x -> TypePlaceholder.fresh(newExpression.getStart())).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(newExpression.getStart())); // ReturnType Statement ret = new NewClass(newClass, args, null, signature, newExpression.getStart()); ret.setStatement(); return ret; } private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { - Expression argument = convert(stmt.unaryExpression()); - Token offset = stmt.getStart(); - Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } + Expression argument = convert(stmt.unaryExpression()); + Token offset = stmt.getStart(); + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; + } - private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); return ret; } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); return ret; } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), - TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); return ret; } @@ -350,284 +351,286 @@ public class StatementGenerator { private AssignLeftSide convert(Java8Parser.LeftHandSideContext leftHandSide) { Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart()); - if(leftSide instanceof FieldVar)return new AssignToField((FieldVar) leftSide); - else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide); - else throw new NotImplementedException(); + if (leftSide instanceof FieldVar) + return new AssignToField((FieldVar) leftSide); + else if (leftSide instanceof LocalVar) + return new AssignToLocal((LocalVar) leftSide); + else + throw new NotImplementedException(); } - private Statement convert(Java8Parser.IfThenStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.IfThenStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ + private Statement convert(Java8Parser.IfThenElseStatementContext stmt) { Expression expr = convert(stmt.parExpression().expression()); Statement thenBlock = convert(stmt.statementNoShortIf()); Statement elseBlock = convert(stmt.statement()); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); } - private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.AssertStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.AssertStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.SwitchStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.SwitchStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.SwitchBlockContext stmt){ - //TODO + private Statement convert(Java8Parser.SwitchBlockContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.SwitchBlockStatementGroupContext stmt){ - //TODO + private Statement convert(Java8Parser.SwitchBlockStatementGroupContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.WhileStatementContext stmt){ + private Statement convert(Java8Parser.WhileStatementContext stmt) { Expression expr = convert(stmt.parExpression().expression()); Statement block = convert(stmt.statement()); - return new WhileStmt(expr, block,stmt.getStart()); + return new WhileStmt(expr, block, stmt.getStart()); } - private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.DoStatementContext stmt){ + private Statement convert(Java8Parser.DoStatementContext stmt) { Statement block = convert(stmt.statement()); Expression expr = convert(stmt.parExpression().expression()); - return new DoStmt(expr,block,stmt.getStart()); + return new DoStmt(expr, block, stmt.getStart()); } - private Statement convert(Java8Parser.ForStatementContext stmt){ - if(stmt.basicForStatement() != null){ + private Statement convert(Java8Parser.ForStatementContext stmt) { + if (stmt.basicForStatement() != null) { return convert(stmt.basicForStatement()); - }else if(stmt.enhancedForStatement() != null){ + } else if (stmt.enhancedForStatement() != null) { return convert(stmt.enhancedForStatement()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - private Statement convert(Java8Parser.ForStatementNoShortIfContext stmt){ - if(stmt.basicForStatementNoShortIf() != null){ + private Statement convert(Java8Parser.ForStatementNoShortIfContext stmt) { + if (stmt.basicForStatementNoShortIf() != null) { return convert(stmt.basicForStatementNoShortIf()); - }else if(stmt.enhancedForStatementNoShortIf() != null){ + } else if (stmt.enhancedForStatementNoShortIf() != null) { return convert(stmt.enhancedForStatementNoShortIf()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - private Statement convert(Java8Parser.BasicForStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.BasicForStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private List convert(Java8Parser.ForInitContext stmt){ - if(stmt.statementExpressionList() != null){ + private List convert(Java8Parser.ForInitContext stmt) { + if (stmt.statementExpressionList() != null) { return Arrays.asList(convert(stmt.statementExpressionList())); - }else if(stmt.localVariableDeclaration() != null){ + } else if (stmt.localVariableDeclaration() != null) { return convert(stmt.localVariableDeclaration()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } private List convert(Java8Parser.LocalVariableDeclarationContext declaration) { List ret = new ArrayList<>(); - if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){ - //TODO + if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { + // TODO throw new NotImplementedException(); } RefTypeOrTPHOrWildcardOrGeneric type; - if(declaration.unannTypeOrAuto().unannType() == null){ + if (declaration.unannTypeOrAuto().unannType() == null) { type = TypePlaceholder.fresh(declaration.getStart()); - }else{ + } else { type = TypeGenerator.convert(declaration.unannTypeOrAuto().unannType(), reg, generics); } ret.addAll(generateLocalVariableAssignments(declaration.variableDeclaratorList().variableDeclarator(), type)); return ret; } - private List generateLocalVariableAssignments(List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type){ + private List generateLocalVariableAssignments(List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type) { List ret = new ArrayList<>(); - for(Java8Parser.VariableDeclaratorContext varDecl : varDeclarators){ + for (Java8Parser.VariableDeclaratorContext varDecl : varDeclarators) { TerminalNode name = varDecl.variableDeclaratorId().Identifier(); ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); this.localVars.put(name.getText(), type); - if(varDecl.variableInitializer() != null){ + if (varDecl.variableInitializer() != null) { Expression initValue; - if(varDecl.variableInitializer().arrayInitializer() != null){ + if (varDecl.variableInitializer().arrayInitializer() != null) { throw new NotImplementedException(); - }else{ + } else { initValue = convert(varDecl.variableInitializer().expression()); } - ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())) - , initValue, name.getSymbol())); + ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())), initValue, name.getSymbol())); } } return ret; } - public Statement generateFieldAssignment(Java8Parser.VariableDeclaratorContext varDecl, RefTypeOrTPHOrWildcardOrGeneric type){ + public Statement generateFieldAssignment(Java8Parser.VariableDeclaratorContext varDecl, RefTypeOrTPHOrWildcardOrGeneric type) { TerminalNode name = varDecl.variableDeclaratorId().Identifier(); Expression initValue; - if(varDecl.variableInitializer().arrayInitializer() != null){ + if (varDecl.variableInitializer().arrayInitializer() != null) { throw new NotImplementedException(); - }else{ + } else { initValue = convert(varDecl.variableInitializer().expression()); } - return (new Assign(new AssignToField( - new FieldVar(new This(varDecl.getStart()), name.getText(), - type, varDecl.getStart())), - initValue, name.getSymbol())); + return (new Assign(new AssignToField(new FieldVar(new This(varDecl.getStart()), name.getText(), type, varDecl.getStart())), initValue, name.getSymbol())); } - private Statement convert(Java8Parser.ForUpdateContext stmt){ + private Statement convert(Java8Parser.ForUpdateContext stmt) { return convert(stmt.statementExpressionList()); } - private Statement convert(Java8Parser.StatementExpressionListContext stmt){ - //TODO + private Statement convert(Java8Parser.StatementExpressionListContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.EnhancedForStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.EnhancedForStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.EnhancedForStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java8Parser.EnhancedForStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.BreakStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.BreakStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.ContinueStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.ContinueStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.ReturnStatementContext stmt){ - if(stmt.expression() != null){ - return new Return( convert(stmt.expression()),stmt.getStart() ); - }else{ + private Statement convert(Java8Parser.ReturnStatementContext stmt) { + if (stmt.expression() != null) { + return new Return(convert(stmt.expression()), stmt.getStart()); + } else { return new ReturnVoid(stmt.getStart()); } } - private Statement convert(Java8Parser.ThrowStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.ThrowStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.SynchronizedStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.SynchronizedStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.TryStatementContext stmt){ - //TODO + private Statement convert(Java8Parser.TryStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.CatchesContext stmt){ - //TODO + private Statement convert(Java8Parser.CatchesContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java8Parser.CatchClauseContext stmt){ - //TODO + private Statement convert(Java8Parser.CatchClauseContext stmt) { + // TODO throw new NotImplementedException(); } -/* -***************+ Expression Conversions: - */ + /* + *************** + Expression Conversions: + */ private Expression convert(Java8Parser.ExpressionContext expression) { - if(expression.lambdaExpression()!=null){ + if (expression.lambdaExpression() != null) { return convert(expression.lambdaExpression()); - }else{ + } else { return convert(expression.assignmentExpression()); } } private Expression convert(Java8Parser.AssignmentExpressionContext expression) { - if(expression.conditionalExpression() != null){ + if (expression.conditionalExpression() != null) { return convert(expression.conditionalExpression()); - }else{ + } else { return convert(expression.assignment()); } } private Expression convert(Java8Parser.ConditionalExpressionContext expression) { - if(expression.conditionalOrExpression() != null){ + if (expression.conditionalOrExpression() != null) { return convert(expression.conditionalOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.ConditionalOrExpressionContext expression) { - if(expression.conditionalOrExpression() == null){ + if (expression.conditionalOrExpression() == null) { return convert(expression.conditionalAndExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.ConditionalAndExpressionContext expression) { - if(expression.conditionalAndExpression() == null){ + if (expression.conditionalAndExpression() == null) { return convert(expression.inclusiveOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.InclusiveOrExpressionContext expression) { - if(expression.inclusiveOrExpression() == null){ + if (expression.inclusiveOrExpression() == null) { return convert(expression.exclusiveOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.ExclusiveOrExpressionContext expression) { - if(expression.exclusiveOrExpression() == null){ + if (expression.exclusiveOrExpression() == null) { return convert(expression.andExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.AndExpressionContext expression) { - if(expression.andExpression() == null){ + if (expression.andExpression() == null) { return convert(expression.equalityExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.EqualityExpressionContext expression) { - if(expression.equalityExpression() == null){ + if (expression.equalityExpression() == null) { return convert(expression.relationalExpression()); - }else{ + } else { String operator = expression.getChild(1).getText(); Expression leftSide = convert(expression.equalityExpression()); Expression rightSide = convert(expression.relationalExpression()); @@ -636,62 +639,60 @@ public class StatementGenerator { } private Expression convert(Java8Parser.RelationalExpressionContext expression) { - if(expression.relationalExpression() == null) { + if (expression.relationalExpression() == null) { return convert(expression.shiftExpression()); - }else { + } else { String operator = expression.getChild(1).getText(); - return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), - convert(expression.relationalExpression()), - convert(expression.shiftExpression()), expression.getStart()); + return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), convert(expression.relationalExpression()), convert(expression.shiftExpression()), expression.getStart()); } - //}else throw new NotImplementedException(); + // }else throw new NotImplementedException(); } private BinaryExpr.Operator convertBinaryOperator(String operator) { - //return BinaryExpr.Operator.ADD; - if(operator.equals("+")) { - return BinaryExpr.Operator.ADD; - }else if(operator.equals("-")) { - return BinaryExpr.Operator.SUB; - }else if(operator.equals("*")) { - return BinaryExpr.Operator.MUL; - }else if(operator.equals("&")) { - return BinaryExpr.Operator.AND; - }else if(operator.equals("|")) { - return BinaryExpr.Operator.OR; - }else if(operator.equals("/")) { - return BinaryExpr.Operator.DIV; - }else if(operator.equals("<")) { - return BinaryExpr.Operator.LESSTHAN; - }else if(operator.equals(">")) { - return BinaryExpr.Operator.BIGGERTHAN; - }else if(operator.equals(">=")) { - return BinaryExpr.Operator.BIGGEREQUAL; - } else if(operator.equals("<=")) { - return BinaryExpr.Operator.LESSEQUAL; - } else if(operator.equals("==")) { - return BinaryExpr.Operator.EQUAL; - } else if(operator.equals("!=")) { - return BinaryExpr.Operator.NOTEQUAL; - } else { - throw new NotImplementedException(); - } -// throw new NotImplementedException(); + // return BinaryExpr.Operator.ADD; + if (operator.equals("+")) { + return BinaryExpr.Operator.ADD; + } else if (operator.equals("-")) { + return BinaryExpr.Operator.SUB; + } else if (operator.equals("*")) { + return BinaryExpr.Operator.MUL; + } else if (operator.equals("&")) { + return BinaryExpr.Operator.AND; + } else if (operator.equals("|")) { + return BinaryExpr.Operator.OR; + } else if (operator.equals("/")) { + return BinaryExpr.Operator.DIV; + } else if (operator.equals("<")) { + return BinaryExpr.Operator.LESSTHAN; + } else if (operator.equals(">")) { + return BinaryExpr.Operator.BIGGERTHAN; + } else if (operator.equals(">=")) { + return BinaryExpr.Operator.BIGGEREQUAL; + } else if (operator.equals("<=")) { + return BinaryExpr.Operator.LESSEQUAL; + } else if (operator.equals("==")) { + return BinaryExpr.Operator.EQUAL; + } else if (operator.equals("!=")) { + return BinaryExpr.Operator.NOTEQUAL; + } else { + throw new NotImplementedException(); + } + // throw new NotImplementedException(); } private Expression convert(Java8Parser.ShiftExpressionContext expression) { - if(expression.shiftExpression() == null){ + if (expression.shiftExpression() == null) { return convert(expression.additiveExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ + if (expression.additiveExpression() == null) { return convert(expression.multiplicativeExpression()); - }else { + } else { Expression leftSide = convert(expression.additiveExpression()); Expression rightSide = convert(expression.multiplicativeExpression()); BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); @@ -701,75 +702,72 @@ public class StatementGenerator { } private Expression convert(Java8Parser.MultiplicativeExpressionContext expression) { - if(expression.multiplicativeExpression() == null){ + if (expression.multiplicativeExpression() == null) { return convert(expression.unaryExpression()); - }else{ - Expression leftSide = convert(expression.multiplicativeExpression()); - Expression rightSide = convert(expression.unaryExpression()); - BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); - Token offset = expression.getStart(); - return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); + } else { + Expression leftSide = convert(expression.multiplicativeExpression()); + Expression rightSide = convert(expression.unaryExpression()); + BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); } } private Expression convert(Java8Parser.UnaryExpressionContext expression) { - if(expression.preIncrementExpression() != null){ + if (expression.preIncrementExpression() != null) { return convert(expression.preIncrementExpression()); - }else if(expression.preDecrementExpression() != null){ + } else if (expression.preDecrementExpression() != null) { return convert(expression.preDecrementExpression()); - }else if(expression.unaryExpressionNotPlusMinus() != null){ + } else if (expression.unaryExpressionNotPlusMinus() != null) { return convert(expression.unaryExpressionNotPlusMinus()); - }else if(expression.getText().startsWith("+")){ - return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), - TypePlaceholder.fresh(expression.getStart()), expression.getStart()); - }else if(expression.getText().startsWith("-")){ - return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), - TypePlaceholder.fresh(expression.getStart()), expression.getStart()); - }else{ - //Diese Exceptions sollte nie geworfen werden. - //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. + } else if (expression.getText().startsWith("+")) { + return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart()); + } else if (expression.getText().startsWith("-")) { + return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart()); + } else { + // Diese Exceptions sollte nie geworfen werden. + // Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. throw new NotImplementedException(); } } private Expression convert(Java8Parser.UnaryExpressionNotPlusMinusContext expression) { - if(expression.postfixExpression() != null){ + if (expression.postfixExpression() != null) { return convert(expression.postfixExpression()); - }else if(expression.castExpression() != null){ + } else if (expression.castExpression() != null) { return convert(expression.castExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.CastExpressionContext expressionContext) { Expression expr = null; - if(expressionContext.unaryExpression() != null){ + if (expressionContext.unaryExpression() != null) { throw new NotImplementedException(); - }else if(expressionContext.unaryExpressionNotPlusMinus() != null){ + } else if (expressionContext.unaryExpressionNotPlusMinus() != null) { expr = convert(expressionContext.unaryExpressionNotPlusMinus()); - }else if(expressionContext.lambdaExpression() != null){ + } else if (expressionContext.lambdaExpression() != null) { expr = convert(expressionContext.lambdaExpression()); } - return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics),expr, expressionContext.getStart()); + return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics), expr, expressionContext.getStart()); } private Expression convert(Java8Parser.PostfixExpressionContext expression) { Expression expr; - if(expression.primary() != null){ + if (expression.primary() != null) { expr = convert(expression.primary()); - }else{ + } else { expr = convert(expression.expressionName()); } - if(expression.postDecrementExpression_lf_postfixExpression() == null && - expression.postIncrementExpression_lf_postfixExpression() == null){ + if (expression.postDecrementExpression_lf_postfixExpression() == null && expression.postIncrementExpression_lf_postfixExpression() == null) { return expr; } - for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ + for (Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()) { expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart()); } - for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ + for (Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()) { expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart()); } @@ -778,14 +776,14 @@ public class StatementGenerator { private Expression convert(Java8Parser.PrimaryContext primary) { Expression expr; - if(primary.primaryNoNewArray_lfno_primary()!=null){ + if (primary.primaryNoNewArray_lfno_primary() != null) { expr = convert(primary.primaryNoNewArray_lfno_primary()); - }else{ + } else { expr = convert(primary.arrayCreationExpression()); } - if(primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size()>0){ - for(Java8Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()){ + if (primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size() > 0) { + for (Java8Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()) { expr = convert(expr, e); } } @@ -793,55 +791,52 @@ public class StatementGenerator { } private Expression convert(Expression expr, Java8Parser.PrimaryNoNewArray_lf_primaryContext e) { - if(e.classInstanceCreationExpression_lf_primary() != null){ + if (e.classInstanceCreationExpression_lf_primary() != null) { throw new NotImplementedException(); - }else if(e.fieldAccess_lf_primary() != null){ - + } else if (e.fieldAccess_lf_primary() != null) { + throw new NotImplementedException(); - }else if(e.arrayAccess_lf_primary() != null){ + } else if (e.arrayAccess_lf_primary() != null) { throw new NotImplementedException(); - }else if(e.methodReference_lf_primary() != null){ + } else if (e.methodReference_lf_primary() != null) { throw new NotImplementedException(); - }else { + } else { Java8Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary(); String methodName = ctxt.Identifier().toString(); - ArrayList signature = ctxt.argumentList().expression().stream() - .map(x -> TypePlaceholder.fresh(e.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - signature.add(TypePlaceholder.fresh(e.getStart())); //ReturnType - return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, - convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), signature, e.getStart()); + ArrayList signature = ctxt.argumentList().expression().stream().map(x -> TypePlaceholder.fresh(e.getStart())).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(e.getStart())); // ReturnType + return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), signature, e.getStart()); } } - + private Expression convert(Java8Parser.ArrayCreationExpressionContext expression) { throw new NotImplementedException(); } private Expression convert(Java8Parser.PrimaryNoNewArray_lfno_primaryContext expression) { - if(expression.literal() != null){ + if (expression.literal() != null) { return convert(expression.literal()); - }else if(expression.parExpression().expression()!=null){ + } else if (expression.parExpression() != null) { return convert(expression.parExpression().expression()); - }else if(expression.methodInvocation_lfno_primary() != null){ + } else if (expression.methodInvocation_lfno_primary() != null) { return convert(expression.methodInvocation_lfno_primary()); - }else if(expression.classInstanceCreationExpression_lfno_primary() != null) { + } else if (expression.classInstanceCreationExpression_lfno_primary() != null) { return convert(expression.classInstanceCreationExpression_lfno_primary()); - }else if(expression.getText().equals("this")) { + } else if (expression.getText().equals("this")) { return new This(expression.getStart()); - }else if(expression.fieldAccess_lfno_primary() != null){ + } else if (expression.fieldAccess_lfno_primary() != null) { return convert(expression.fieldAccess_lfno_primary()); - }else if(expression.methodReference_lfno_primary() != null){ + } else if (expression.methodReference_lfno_primary() != null) { throw new NotImplementedException(); - }else if(expression.typeName() != null){ + } else if (expression.typeName() != null) { throw new NotImplementedException(); - }else if(expression.unannPrimitiveType() != null){ + } else if (expression.unannPrimitiveType() != null) { throw new NotImplementedException(); - }else if(expression.arrayAccess_lfno_primary() != null){ + } else if (expression.arrayAccess_lfno_primary() != null) { throw new NotImplementedException(); - }else if(expression.fieldAccess_lfno_primary() != null){ + } else if (expression.fieldAccess_lfno_primary() != null) { throw new NotImplementedException(); - }else{ + } else { throw new NotImplementedException(); } } @@ -852,128 +847,114 @@ public class StatementGenerator { private Expression convert(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) { Java8Parser.TypeArgumentsContext genericArgs = null; - if(newExpression.expressionName()!= null)throw new NotImplementedException(); - if(newExpression.typeArgumentsOrDiamond()!= null){ - if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){ + if (newExpression.expressionName() != null) + throw new NotImplementedException(); + if (newExpression.typeArgumentsOrDiamond() != null) { + if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) { genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); } } - if(newExpression.typeArguments()!= null)throw new NotImplementedException(); + if (newExpression.typeArguments() != null) + throw new NotImplementedException(); TerminalNode identifier = newExpression.Identifier(0); - RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics); + RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, identifier.getSymbol(), reg, generics); ArgumentList args = convert(newExpression.argumentList()); - ArrayList signature = args.getArguments().stream() - .map(x -> TypePlaceholder.fresh(newExpression.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - signature.add(TypePlaceholder.fresh(newExpression.getStart())); //ReturnType + ArrayList signature = args.getArguments().stream().map(x -> TypePlaceholder.fresh(newExpression.getStart())).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(newExpression.getStart())); // ReturnType return new NewClass(newClass, args, null, signature, newExpression.getStart()); } private Expression convert(Java8Parser.LiteralContext literal) { - if(literal.IntegerLiteral() != null){ + if (literal.IntegerLiteral() != null) { Number value = Integer.parseInt(literal.IntegerLiteral().getText()); - return new Literal(TypePlaceholder.fresh(literal.getStart()), - value, literal.getStart()); - } else if(literal.FloatingPointLiteral()!= null){ + return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); + } else if (literal.FloatingPointLiteral() != null) { Number value = Double.parseDouble(literal.FloatingPointLiteral().getText()); - return new Literal(TypePlaceholder.fresh(literal.getStart()), - value, literal.getStart()); - }else if(literal.BooleanLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); + return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); + } else if (literal.BooleanLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.Boolean"), literal.getStart()); + return new Literal(type, Boolean.parseBoolean(literal.BooleanLiteral().getText()), literal.getStart()); + } else if (literal.CharacterLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.Character"), literal.getStart()); return new Literal(type, - Boolean.parseBoolean(literal.BooleanLiteral().getText()), - literal.getStart()); - }else if(literal.CharacterLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); - return new Literal(type, - // das gibt immer ' zurück, der Char befindet sich in Position 1 - //literal.CharacterLiteral().getText().charAt(0), - literal.CharacterLiteral().getText().charAt(1), - literal.getStart()); - }else if(literal.StringLiteral()!=null){ - RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); - return new Literal(type, - literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length()-1), - literal.getStart()); - }else if(literal.NullLiteral() != null){ - return new Literal(TypePlaceholder.fresh(literal.getStart()), null, - literal.getStart()); - }else { + // das gibt immer ' zurück, der Char befindet sich in Position 1 + // literal.CharacterLiteral().getText().charAt(0), + literal.CharacterLiteral().getText().charAt(1), literal.getStart()); + } else if (literal.StringLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.String"), literal.getStart()); + return new Literal(type, literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length() - 1), literal.getStart()); + } else if (literal.NullLiteral() != null) { + return new Literal(TypePlaceholder.fresh(literal.getStart()), null, literal.getStart()); + } else { throw new NotImplementedException(); } } private Expression convert(Java8Parser.MethodInvocation_lfno_primaryContext methodInvocationContext) { String name; - if(methodInvocationContext.methodName()!=null){ + if (methodInvocationContext.methodName() != null) { name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ + } else {// if(methodInvocationContext.Identifier() != null){ name = methodInvocationContext.Identifier().getText(); } Expression receiver; - if(methodInvocationContext.typeName() != null){ + if (methodInvocationContext.typeName() != null) { receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ + } else if (methodInvocationContext.expressionName() != null) { receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.toString().startsWith("super")) { + } else if (methodInvocationContext.toString().startsWith("super")) { receiver = new Super(methodInvocationContext.getStart()); - }else if(methodInvocationContext.methodName() != null){ + } else if (methodInvocationContext.methodName() != null) { receiver = new This(methodInvocationContext.methodName().getStart()); - }else { + } else { throw new NotImplementedException(); } ArgumentList argumentList = convert(methodInvocationContext.argumentList()); - ArrayList signature = argumentList.getArguments().stream() - .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); //ReturnType - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), - getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), - signature, methodInvocationContext.getStart()); + ArrayList signature = argumentList.getArguments().stream().map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(methodInvocationContext.getStart())); // ReturnType + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), signature, methodInvocationContext.getStart()); return ret; } private Expression convert(Java8Parser.LambdaExpressionContext expression) { Java8Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters(); ParameterList params; - if(lambdaParams.Identifier() != null){ + if (lambdaParams.Identifier() != null) { List parameterList = new ArrayList<>(); - parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(), - TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart())); + parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(), TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart())); params = new ParameterList(parameterList, lambdaParams.getStart()); - }else if(lambdaParams.formalParameterList() != null){ + } else if (lambdaParams.formalParameterList() != null) { params = convert(lambdaParams.formalParameterList()); - //}else if( lambdaParams.inferredFormalParameterList != null){ - }else { + // }else if( lambdaParams.inferredFormalParameterList != null){ + } else { params = new ParameterList(new ArrayList<>(), expression.getStart()); } - HashMap lambdaLocals = new HashMap<>(); + HashMap lambdaLocals = new HashMap<>(); lambdaLocals.putAll(localVars); - for(FormalParameter param : params.getFormalparalist()){ + for (FormalParameter param : params.getFormalparalist()) { lambdaLocals.put(param.getName(), param.getType()); } StatementGenerator lambdaGenerator = new StatementGenerator(reg, generics, fields, lambdaLocals); Block block; - if(expression.lambdaBody().expression() != null){ + if (expression.lambdaBody().expression() != null) { List statements = new ArrayList<>(); - statements.add(new Return(lambdaGenerator.convert(expression.lambdaBody().expression()), - expression.lambdaBody().expression().getStart())); + statements.add(new Return(lambdaGenerator.convert(expression.lambdaBody().expression()), expression.lambdaBody().expression().getStart())); block = new Block(statements, expression.lambdaBody().getStart()); - }else{ + } else { block = lambdaGenerator.convert(expression.lambdaBody().block(), true); } List funNParams = new ArrayList<>(); - funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type - params.getFormalparalist().forEach(formalParameter -> //Für jeden Parameter einen TPH anfügen: - funNParams.add(TypePlaceholder.fresh(expression.getStart()))); + funNParams.add(TypePlaceholder.fresh(expression.getStart()));// ret-Type + params.getFormalparalist().forEach(formalParameter -> // Für jeden Parameter einen TPH anfügen: + funNParams.add(TypePlaceholder.fresh(expression.getStart()))); RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart()); - //RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()), - //funNParams, name.getStart()); + // RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()), + // funNParams, name.getStart()); return new LambdaExpression(lambdaType, params, block, expression.getStart()); } }