Java Code Examples for org.eclipse.jdt.core.dom.IfStatement#ELSE_STATEMENT_PROPERTY

The following examples show how to use org.eclipse.jdt.core.dom.IfStatement#ELSE_STATEMENT_PROPERTY . 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: ControlStatementsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
	ASTRewrite rewrite= cuRewrite.getASTRewrite();
	String label;
	if (fBodyProperty == IfStatement.THEN_STATEMENT_PROPERTY) {
		label = FixMessages.CodeStyleFix_ChangeIfToBlock_desription;
	} else if (fBodyProperty == IfStatement.ELSE_STATEMENT_PROPERTY) {
		label = FixMessages.CodeStyleFix_ChangeElseToBlock_description;
	} else {
		label = FixMessages.CodeStyleFix_ChangeControlToBlock_description;
	}

	TextEditGroup group= createTextEditGroup(label, cuRewrite);
	ASTNode moveTarget= rewrite.createMoveTarget(fBody);
	Block replacingBody= cuRewrite.getRoot().getAST().newBlock();
	replacingBody.statements().add(moveTarget);
	rewrite.set(fControlStatement, fBodyProperty, replacingBody, group);
}
 
Example 2
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void insertAt(ASTNode target, Statement declaration) {
	ASTRewrite rewrite= fCURewrite.getASTRewrite();
	TextEditGroup groupDescription= fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);

	ASTNode parent= target.getParent();
	StructuralPropertyDescriptor locationInParent= target.getLocationInParent();
	while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
		if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY
				|| locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY
				|| locationInParent == ForStatement.BODY_PROPERTY
				|| locationInParent == EnhancedForStatement.BODY_PROPERTY
				|| locationInParent == DoStatement.BODY_PROPERTY
				|| locationInParent == WhileStatement.BODY_PROPERTY) {
			// create intermediate block if target was the body property of a control statement:
			Block replacement= rewrite.getAST().newBlock();
			ListRewrite replacementRewrite= rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
			replacementRewrite.insertFirst(declaration, null);
			replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
			rewrite.replace(target, replacement, groupDescription);
			return;
		}
		target= parent;
		parent= parent.getParent();
		locationInParent= target.getLocationInParent();
	}
	ListRewrite listRewrite= rewrite.getListRewrite(parent, (ChildListPropertyDescriptor)locationInParent);
	listRewrite.insertBefore(declaration, target, groupDescription);
}
 
Example 3
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns true if a node at a given location is a body of a control statement. Such body nodes are
 * interesting as when replacing them, it has to be evaluates if a Block is needed instead.
 * E.g. <code> if (x) do(); -> if (x) { do1(); do2() } </code>
 *
 * @param locationInParent Location of the body node
 * @return Returns true if the location is a body node location of a control statement.
 */
public static boolean isControlStatementBody(StructuralPropertyDescriptor locationInParent) {
	return locationInParent == IfStatement.THEN_STATEMENT_PROPERTY
		|| locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY
		|| locationInParent == ForStatement.BODY_PROPERTY
		|| locationInParent == EnhancedForStatement.BODY_PROPERTY
		|| locationInParent == WhileStatement.BODY_PROPERTY
		|| locationInParent == DoStatement.BODY_PROPERTY;
}