Java Code Examples for org.eclipse.jdt.core.dom.IfStatement#getElseStatement()

The following examples show how to use org.eclipse.jdt.core.dom.IfStatement#getElseStatement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
private IfStmt visit(IfStatement node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	IfStmt ifStmt = new IfStmt(startLine, endLine, node);
	
	Expr condition = (Expr)process(node.getExpression());
	condition.setParent(ifStmt);
	ifStmt.setCondition(condition);
	
	Stmt then = (Stmt)process(node.getThenStatement());
	then.setParent(ifStmt);
	ifStmt.setThen(then);
	
	if(node.getElseStatement() != null){
		Stmt els = (Stmt) process(node.getElseStatement());
		els.setParent(ifStmt);
		ifStmt.setElse(els);
	}
	
	return ifStmt;
}
 
Example 2
Source File: SequenceDiagramVisitor.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
private void checkSendInIfNode(IfStatement ifNode) {
	boolean showErrorHere = placeOfError == ifNode;
	Statement thenStatement = ifNode.getThenStatement();
	if (showErrorHere) {
		placeOfError = thenStatement;
	}
	if (thenStatement instanceof Block) {
		checkSendInBlock((Block) thenStatement, showErrorHere);
	} else {
		checkSendInStatement(thenStatement);
	}
	Statement elseStatement = ifNode.getElseStatement();
	if (showErrorHere) {
		placeOfError = elseStatement;
	}
	if (elseStatement == null) {
		return;
	}
	if (elseStatement instanceof IfStatement) {
		checkSendInIfNode((IfStatement) elseStatement);
	} else if (elseStatement instanceof Block) {
		checkSendInBlock((Block) elseStatement, showErrorHere);
	} else {
		checkSendInStatement(elseStatement);
	}
}
 
Example 3
Source File: OptAltFragmentExporter.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean preNext(IfStatement curElement) {
	Statement thenStatement = curElement.getThenStatement();
	Statement elseStatement = curElement.getElseStatement();
	Expression condition = curElement.getExpression();

	if (elseStatement == null) {
		compiler.println("opt " + condition.toString());
		return true;
	} else {
		compiler.println("alt " + condition.toString());
		thenStatement.accept(compiler);
		if (elseStatement instanceof IfStatement) {
			processAltStatement((IfStatement) elseStatement);
		} else {
			compiler.println("else");
			elseStatement.accept(compiler);
		}
		return false;
	}
}
 
Example 4
Source File: OptAltFragmentExporter.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
private void processAltStatement(IfStatement statement) {
	Statement thenStatement = statement.getThenStatement();
	Statement elseStatement = statement.getElseStatement();
	Expression condition = statement.getExpression();

	compiler.println("else " + condition.toString());
	thenStatement.accept(compiler);
	if (elseStatement != null) {
		if (elseStatement instanceof IfStatement) {
			processAltStatement((IfStatement) elseStatement);
		} else {
			compiler.println("else");
			elseStatement.accept(compiler);
		}
	}
}
 
Example 5
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isSingleControlStatementWithoutBlock() {
	List<Statement> statements= fDeclaration.getBody().statements();
	int size= statements.size();
	if (size != 1)
		return false;
	Statement statement= statements.get(size - 1);
	int nodeType= statement.getNodeType();
	if (nodeType == ASTNode.IF_STATEMENT) {
		IfStatement ifStatement= (IfStatement) statement;
		return !(ifStatement.getThenStatement() instanceof Block)
			&& !(ifStatement.getElseStatement() instanceof Block);
	} else if (nodeType == ASTNode.FOR_STATEMENT) {
		return !(((ForStatement)statement).getBody() instanceof Block);
	} else if (nodeType == ASTNode.WHILE_STATEMENT) {
		return !(((WhileStatement)statement).getBody() instanceof Block);
	}
	return false;
}
 
Example 6
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean getAddElseProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
	if (!(node instanceof IfStatement)) {
		return false;
	}
	IfStatement ifStatement= (IfStatement) node;
	if (ifStatement.getElseStatement() != null) {
		return false;
	}

	if (resultingCollections == null) {
		return true;
	}

	AST ast= node.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	Block body= ast.newBlock();

	rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, body, null);

	String label= CorrectionMessages.QuickAssistProcessor_addelseblock_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_ELSE_BLOCK, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example 7
Source File: CloneInstanceMapper.java    From JDeodorant with MIT License 6 votes vote down vote up
private boolean isNestedUnderElse(ASTNode astNode) {
	if(astNode.getParent() instanceof IfStatement) {
		IfStatement ifParent = (IfStatement)astNode.getParent();
		if(ifParent.getElseStatement()!=null && ifParent.getElseStatement().equals(astNode))
			return true;
	}
	if(astNode.getParent() instanceof Block) {
		Block blockParent = (Block)astNode.getParent();
		if(blockParent.getParent() instanceof IfStatement) {
			IfStatement ifGrandParent = (IfStatement)blockParent.getParent();
			if(ifGrandParent.getElseStatement()!=null && ifGrandParent.getElseStatement().equals(blockParent))
				return true;
		}
	}
	return false;
}
 
Example 8
Source File: InputFlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(IfStatement node) {
	if (skipNode(node)) {
		return;
	}
	Statement thenPart = node.getThenStatement();
	Statement elsePart = node.getElseStatement();
	if ((thenPart != null && fSelection.coveredBy(thenPart)) || (elsePart != null && fSelection.coveredBy(elsePart))) {
		GenericSequentialFlowInfo info = createSequential();
		setFlowInfo(node, info);
		endVisitConditional(info, node.getExpression(), new ASTNode[] { thenPart, elsePart });
	} else {
		super.endVisit(node);
	}
}
 
Example 9
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final IfStatement node) {
  this.appendToBuffer("if (");
  node.getExpression().accept(this);
  this.appendToBuffer(") ");
  node.getThenStatement().accept(this);
  Statement _elseStatement = node.getElseStatement();
  boolean _tripleNotEquals = (_elseStatement != null);
  if (_tripleNotEquals) {
    this.appendToBuffer(" else ");
    node.getElseStatement().accept(this);
  }
  return false;
}
 
Example 10
Source File: InputFlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(IfStatement node) {
	if (skipNode(node))
		return;
	Statement thenPart= node.getThenStatement();
	Statement elsePart= node.getElseStatement();
	if ((thenPart != null && fSelection.coveredBy(thenPart)) ||
			(elsePart != null && fSelection.coveredBy(elsePart))) {
		GenericSequentialFlowInfo info= createSequential();
		setFlowInfo(node, info);
		endVisitConditional(info, node.getExpression(), new ASTNode[] {thenPart, elsePart});
	} else {
		super.endVisit(node);
	}
}
 
Example 11
Source File: ControlStatementsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(IfStatement statement) {
	handle(statement.getThenStatement(), IfStatement.THEN_STATEMENT_PROPERTY);

	Statement elseStatement= statement.getElseStatement();
	if (elseStatement != null && !(elseStatement instanceof IfStatement)) {
		handle(elseStatement, IfStatement.ELSE_STATEMENT_PROPERTY);
	}

	return super.visit(statement);
}
 
Example 12
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getInverseIfProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
	if (!(covering instanceof IfStatement)) {
		return false;
	}
	IfStatement ifStatement= (IfStatement) covering;
	if (ifStatement.getElseStatement() == null) {
		return false;
	}
	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}
	//
	AST ast= covering.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	Statement thenStatement= ifStatement.getThenStatement();
	Statement elseStatement= ifStatement.getElseStatement();

	// prepare original nodes
	Expression inversedExpression= getInversedExpression(rewrite, ifStatement.getExpression());

	Statement newElseStatement= (Statement) rewrite.createMoveTarget(thenStatement);
	Statement newThenStatement= (Statement) rewrite.createMoveTarget(elseStatement);
	// set new nodes
	rewrite.set(ifStatement, IfStatement.EXPRESSION_PROPERTY, inversedExpression, null);

	if (elseStatement instanceof IfStatement) {// bug 79507 && bug 74580
		Block elseBlock= ast.newBlock();
		elseBlock.statements().add(newThenStatement);
		newThenStatement= elseBlock;
	}
	rewrite.set(ifStatement, IfStatement.THEN_STATEMENT_PROPERTY, newThenStatement, null);
	rewrite.set(ifStatement, IfStatement.ELSE_STATEMENT_PROPERTY, newElseStatement, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_inverseIf_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_STATEMENT, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example 13
Source File: ASTNodeDifference.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionOfIfStatementNestedAtLevelZero() {
	if(expression1.getExpression().getParent() instanceof IfStatement &&
			expression2.getExpression().getParent() instanceof IfStatement) {
		IfStatement if1 = (IfStatement)expression1.getExpression().getParent();
		IfStatement if2 = (IfStatement)expression2.getExpression().getParent();
		boolean noElsePart = if1.getElseStatement() == null && if2.getElseStatement() == null;
		ASTNode parent1 = if1.getParent();
		while(parent1 instanceof Block) {
			parent1 = parent1.getParent();
		}
		ASTNode parent2 = if2.getParent();
		while(parent2 instanceof Block) {
			parent2 = parent2.getParent();
		}
		if(parent1 instanceof MethodDeclaration && parent2 instanceof MethodDeclaration) {
			return noElsePart;
		}
		if(parent1 instanceof TryStatement && parent2 instanceof TryStatement) {
			TryStatement try1 = (TryStatement)parent1;
			TryStatement try2 = (TryStatement)parent2;
			parent1 = try1.getParent();
			while(parent1 instanceof Block) {
				parent1 = parent1.getParent();
			}
			parent2 = try2.getParent();
			while(parent2 instanceof Block) {
				parent2 = parent2.getParent();
			}
			if(parent1 instanceof MethodDeclaration && parent2 instanceof MethodDeclaration) {
				return noElsePart;
			}
		}
	}
	return false;
}
 
Example 14
Source File: ControlStatementsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean satisfiesPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows, boolean cleanUpCheck) {
	Object child= controlStatement.getStructuralProperty(childDescriptor);

	if (!(child instanceof Block))
		return false;

	Block block= (Block)child;
	List<Statement> list= block.statements();
	if (list.size() != 1)
		return false;

	ASTNode singleStatement= list.get(0);

	if (onlyReturnAndThrows)
		if (!(singleStatement instanceof ReturnStatement) && !(singleStatement instanceof ThrowStatement))
			return false;

	if (controlStatement instanceof IfStatement) {
		// if (true) {
		//  while (true)
		// 	 if (false)
		//    ;
		// } else
		//   ;

		if (((IfStatement)controlStatement).getThenStatement() != child)
			return true;//can always remove blocks in else part

		IfStatement ifStatement= (IfStatement)controlStatement;
		if (ifStatement.getElseStatement() == null)
			return true;//can always remove if no else part

		return !hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck);
	} else {
		//if (true)
		// while (true) {
		//  if (false)
		//   ;
		// }
		//else
		// ;
		if (!hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck))
			return true;

		ASTNode currentChild= controlStatement;
		ASTNode parent= currentChild.getParent();
		while (true) {
			Statement body= null;
			if (parent instanceof IfStatement) {
				body= ((IfStatement)parent).getThenStatement();
				if (body == currentChild && ((IfStatement)parent).getElseStatement() != null)//->currentChild is an unblocked then part
					return false;
			} else if (parent instanceof WhileStatement) {
				body= ((WhileStatement)parent).getBody();
			} else if (parent instanceof DoStatement) {
				body= ((DoStatement)parent).getBody();
			} else if (parent instanceof ForStatement) {
				body= ((ForStatement)parent).getBody();
			} else if (parent instanceof EnhancedForStatement) {
				body= ((EnhancedForStatement)parent).getBody();
			} else {
				return true;
			}
			if (body != currentChild)//->parents child is a block
				return true;

			currentChild= parent;
			parent= currentChild.getParent();
		}
	}
}