org.eclipse.jdt.core.dom.IfStatement Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.IfStatement. 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: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
private Statement createArrayComparison(String name) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS));
	invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
	invoc.arguments().add(getThisAccessForEquals(name));
	invoc.arguments().add(getOtherAccess(name));

	PrefixExpression pe= fAst.newPrefixExpression();
	pe.setOperator(PrefixExpression.Operator.NOT);
	pe.setOperand(invoc);

	IfStatement ifSt= fAst.newIfStatement();
	ifSt.setExpression(pe);
	ifSt.setThenStatement(getThenStatement(getReturnFalse()));

	return ifSt;
}
 
Example #2
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 #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: 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 #5
Source File: SequenceDiagramVisitor.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
private boolean checkSendOrFragmentInBlock(Block block) {
	List<Statement> statements = (List<Statement>) block.statements();
	List<Statement> loops = Utils.getLoopNodes(statements);
	loops.forEach(loop -> {
		checkSendInLoopNode(loop);
	});
	List<IfStatement> ifNodes = Utils.getIfNodes(statements);
	ifNodes.forEach(ifNode -> {
		checkSendInIfNode(ifNode);
	});
	List<MethodInvocation> parFragments = Utils.getParFragments(statements);
	parFragments.forEach(parFragment -> {
		checkSendInPar(parFragment);
	});
	List<MethodInvocation> methodInvocations = Utils.getMethodInvocations(statements);
	final List<Boolean> containsSendOrFragment = new ArrayList<>();
	methodInvocations.forEach(methodInvocation -> {
		containsSendOrFragment.add(checkSendOrFragmentInMethodInvocation(methodInvocation));
	});
	boolean isLeaf = loops.isEmpty() && ifNodes.isEmpty() && parFragments.isEmpty();
	return !isLeaf || containsSendOrFragment.contains(true);
}
 
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: 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 #8
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Statement createMultiArrayComparison(String name) {
	MethodInvocation invoc= fAst.newMethodInvocation();
	invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_EQUALS));
	invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS));
	invoc.arguments().add(getThisAccessForEquals(name));
	invoc.arguments().add(getOtherAccess(name));

	PrefixExpression pe= fAst.newPrefixExpression();
	pe.setOperator(PrefixExpression.Operator.NOT);
	pe.setOperand(invoc);

	IfStatement ifSt= fAst.newIfStatement();
	ifSt.setExpression(pe);
	ifSt.setThenStatement(getThenStatement(getReturnFalse()));

	return ifSt;
}
 
Example #9
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 #10
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final ReturnStatement node) {
  this.appendToBuffer("return");
  Expression _expression = node.getExpression();
  boolean _tripleNotEquals = (_expression != null);
  if (_tripleNotEquals) {
    this.appendSpaceToBuffer();
    node.getExpression().accept(this);
    this.appendSpaceToBuffer();
  } else {
    final ASTNode parent = node.getParent();
    final boolean isIfElse = ((parent instanceof IfStatement) && (((IfStatement) parent).getElseStatement() != null));
    if (((!isIfElse) && (!(parent instanceof SwitchStatement)))) {
      this.appendToBuffer(";");
    }
  }
  return false;
}
 
Example #11
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Statement createOuterComparison() {
	MethodInvocation outer1= fAst.newMethodInvocation();
	outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));

	MethodInvocation outer2= fAst.newMethodInvocation();
	outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE));
	outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED));

	MethodInvocation outerEql= fAst.newMethodInvocation();
	outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS));
	outerEql.setExpression(outer1);
	outerEql.arguments().add(outer2);

	PrefixExpression not= fAst.newPrefixExpression();
	not.setOperand(outerEql);
	not.setOperator(PrefixExpression.Operator.NOT);

	IfStatement notEqNull= fAst.newIfStatement();
	notEqNull.setExpression(not);
	notEqNull.setThenStatement(getThenStatement(getReturnFalse()));
	return notEqNull;
}
 
Example #12
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 #13
Source File: CloneStructureNode.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean isElseIf() {
	if(this.getMapping() instanceof PDGNodeMapping) {
		PDGNodeMapping thisMapping = (PDGNodeMapping)this.getMapping();
		PDGNodeMapping symmetrical = thisMapping.getSymmetricalIfNodePair();
		if(symmetrical != null && symmetrical.equals(parent.getMapping()))
			return true;
		PDGNode childNodeG1 = thisMapping.getNodeG1();
		PDGNode childNodeG2 = thisMapping.getNodeG2();
		if(thisMapping.isFalseControlDependent() &&
				(childNodeG1.getASTStatement() instanceof IfStatement && childNodeG1.getASTStatement().getParent() instanceof IfStatement) &&
				(childNodeG2.getASTStatement() instanceof IfStatement && childNodeG2.getASTStatement().getParent() instanceof IfStatement))
			return true;
	}
	else if(this.getMapping() instanceof PDGNodeGap) {
		PDGNodeGap nodeGap = (PDGNodeGap)this.getMapping();
		PDGNode nodeG1 = nodeGap.getNodeG1();
		PDGNode nodeG2 = nodeGap.getNodeG2();
		if(nodeGap.isFalseControlDependent() &&
				((nodeG1 != null && nodeG1.getASTStatement() instanceof IfStatement && nodeG1.getASTStatement().getParent() instanceof IfStatement) ||
				(nodeG2 != null && nodeG2.getASTStatement() instanceof IfStatement && nodeG2.getASTStatement().getParent() instanceof IfStatement)))
			return true;
	}
	return false;
}
 
Example #14
Source File: StringBuilderChainGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	IfStatement ifStatement= fAst.newIfStatement();
	ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral()));
	Block thenBlock= fAst.newBlock();
	flushTemporaryExpression();
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), thenBlock);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), thenBlock);
	flushTemporaryExpression();

	if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) {
		ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0)));
	} else {
		ifStatement.setThenStatement(thenBlock);
	}
	toStringMethod.getBody().statements().add(ifStatement);
}
 
Example #15
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean needsBlockAroundDanglingIf() {
	/* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=169331
	 *
	 * Situation:
	 * boolean a, b;
	 * void toInline() {
	 *     if (a)
	 *         hashCode();
	 * }
	 * void m() {
	 *     if (b)
	 *         toInline();
	 *     else
	 *         toString();
	 * }
	 * => needs block around inlined "if (a)..." to avoid attaching else to wrong if.
	 */
	return fTargetNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY
			&& fTargetNode.getParent().getStructuralProperty(IfStatement.ELSE_STATEMENT_PROPERTY) != null
			&& fSourceProvider.isDangligIf();
}
 
Example #16
Source File: StringBuilderGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	IfStatement ifStatement= fAst.newIfStatement();
	ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral()));
	Block thenBlock= fAst.newBlock();
	flushBuffer(null);
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), thenBlock);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), thenBlock);
	flushBuffer(thenBlock);

	if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) {
		ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0)));
	} else {
		ifStatement.setThenStatement(thenBlock);
	}
	toStringMethod.getBody().statements().add(ifStatement);
}
 
Example #17
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
	if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
		// e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
		return false;
	}
	if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
			|| locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY
			|| locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
			|| locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY
			|| locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.MESSAGE_PROPERTY
			|| locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchCase.EXPRESSION_PROPERTY
			|| locationInParent == ArrayAccess.INDEX_PROPERTY
			|| locationInParent == ThrowStatement.EXPRESSION_PROPERTY
			|| locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
			|| locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		return false;
	}
	return true;
}
 
Example #18
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 #19
Source File: NodeUtils.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
public boolean visit(SingleVariableDeclaration node){
	ASTNode parent = node.getParent();
	while(parent != null){
		if(parent instanceof Block || parent instanceof ForStatement || parent instanceof IfStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement){
			break;
		}
		parent = parent.getParent();
	}
	if(parent != null) {
		int start = _unit.getLineNumber(node.getStartPosition());
		int end = _unit.getLineNumber(parent.getStartPosition() + parent.getLength());
		Pair<String, Type> pair = new Pair<String, Type>(node.getName().getFullyQualifiedName(), node.getType());
		Pair<Integer, Integer> range = new Pair<Integer, Integer>(start, end);
		_tmpVars.put(pair, range);
	}
	return true;
}
 
Example #20
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
protected boolean isInfixExpressionWithCompositeParent(ASTNode node) {
	if(node instanceof InfixExpression &&
			(node.getParent() instanceof IfStatement || node.getParent() instanceof InfixExpression ||
			node.getParent() instanceof WhileStatement || node.getParent() instanceof DoStatement ||
			node.getParent() instanceof ForStatement)) {
		return true;
	}
	return false;
}
 
Example #21
Source File: CodeSearch.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(IfStatement node) {
	int start = _unit.getLineNumber(node.getExpression().getStartPosition());
	if(start == _extendedLine){
		_extendedStatement = node;
		return false;
	}
	return true;
}
 
Example #22
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean match(ReturnStatement node, Object other) {
	if (node.getExpression() instanceof ConditionalExpression && other instanceof IfStatement)
	{
		TernaryControlStructure nodeTernaryControlStructure = new TernaryControlStructure(node);
		return ifMatch(nodeTernaryControlStructure, other);
	}
	return super.match(node, other);
}
 
Example #23
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 #24
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getConvertIfElseToSwitchProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) {
	if (!(coveringNode instanceof IfStatement)) {
		return false;
	}
	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}

	if(!getConvertIfElseToSwitchProposals(context, coveringNode, resultingCollections, true))
		return false;
	return getConvertIfElseToSwitchProposals(context, coveringNode, resultingCollections, false);
}
 
Example #25
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 #26
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private static AbstractControlStructure generateAbstractControlStructure(Object object)
{
	if (object instanceof IfStatement)
	{
		return new IfControlStructure((IfStatement) object);
	}
	else if (object instanceof SwitchStatement)
	{
		return new SwitchControlStructure((SwitchStatement) object);
	}
	else if (object instanceof ExpressionStatement)
	{
		ExpressionStatement expressionStatement = (ExpressionStatement) object;
		if (AbstractControlStructureUtilities.hasOneConditionalExpression(expressionStatement) != null)
		{
			return new TernaryControlStructure(expressionStatement);
		}
	}
	else if (object instanceof ReturnStatement)
	{
		ReturnStatement returnStatement = (ReturnStatement) object;
		if (returnStatement.getExpression() instanceof ConditionalExpression)
		{
			return new TernaryControlStructure(returnStatement);
		}
	}
	return null;
}
 
Example #27
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(IfStatement stmnt) {
	/*
	 * if ( Expression ) Statement [ else Statement]
	 */
	styledString.append("if", new StyledStringStyler(keywordStyle));
	appendSpace();
	appendOpenParenthesis();
	handleExpression((Expression) stmnt.getExpression());
	appendClosedParenthesis();
	return false;
}
 
Example #28
Source File: ExtractToNullCheckedLocalProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void insertIfStatement(IfStatement ifStmt, Block thenBlock) {
	// did not have a block: add if-statement to new block
	this.block.statements().add(ifStmt);
	// and replace the single statement with this block
	this.rewrite.replace(this.origStmt, this.block, this.group);			
}
 
Example #29
Source File: GenerateHashCodeEqualsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Statement createReturningIfStatement(boolean result, Expression condition) {
	IfStatement firstIf= fAst.newIfStatement();
	firstIf.setExpression(condition);

	ReturnStatement returner= fAst.newReturnStatement();
	returner.setExpression(fAst.newBooleanLiteral(result));
	firstIf.setThenStatement(getThenStatement(returner));
	return firstIf;
}
 
Example #30
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;
}