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

The following examples show how to use org.eclipse.jdt.core.dom.ConditionalExpression. 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 6 votes vote down vote up
private Statement createAddQualifiedHashCode(IVariableBinding binding) {

		MethodInvocation invoc= fAst.newMethodInvocation();
		invoc.setExpression(getThisAccessForHashCode(binding.getName()));
		invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE));

		InfixExpression expr= fAst.newInfixExpression();
		expr.setOperator(Operator.EQUALS);
		expr.setLeftOperand(getThisAccessForHashCode(binding.getName()));
		expr.setRightOperand(fAst.newNullLiteral());

		ConditionalExpression cexpr= fAst.newConditionalExpression();
		cexpr.setThenExpression(fAst.newNumberLiteral("0")); //$NON-NLS-1$
		cexpr.setElseExpression(invoc);
		cexpr.setExpression(parenthesize(expr));

		return prepareAssignment(parenthesize(cexpr));
	}
 
Example #2
Source File: ExpressionVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IBinding resolveBinding(Expression expression){
	if (expression instanceof Name)
		return ((Name)expression).resolveBinding();
	if (expression instanceof ParenthesizedExpression)
		return resolveBinding(((ParenthesizedExpression)expression).getExpression());
	else if (expression instanceof Assignment)
		return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ???
	else if (expression instanceof MethodInvocation)
		return ((MethodInvocation)expression).resolveMethodBinding();
	else if (expression instanceof SuperMethodInvocation)
		return ((SuperMethodInvocation)expression).resolveMethodBinding();
	else if (expression instanceof FieldAccess)
		return ((FieldAccess)expression).resolveFieldBinding();
	else if (expression instanceof SuperFieldAccess)
		return ((SuperFieldAccess)expression).resolveFieldBinding();
	else if (expression instanceof ConditionalExpression)
		return resolveBinding(((ConditionalExpression)expression).getThenExpression());
	return null;
}
 
Example #3
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ITypeConstraint[] create(ConditionalExpression node) {
	List<ITypeConstraint> result= new ArrayList<ITypeConstraint>();
	Expression thenExpression= node.getThenExpression();
	Expression elseExpression= node.getElseExpression();
	ConstraintVariable whole= fConstraintVariableFactory.makeExpressionOrTypeVariable(node, getContext());
	ConstraintVariable ev1= fConstraintVariableFactory.makeExpressionOrTypeVariable(thenExpression, getContext());
	ConstraintVariable ev2= fConstraintVariableFactory.makeExpressionOrTypeVariable(elseExpression, getContext());
	ITypeConstraint[] constraints1= fTypeConstraintFactory.createEqualsConstraint(ev1, ev2);
	ITypeConstraint[] constraints2= fTypeConstraintFactory.createSubtypeConstraint(ev1, whole);
	ITypeConstraint[] constraints3= fTypeConstraintFactory.createSubtypeConstraint(ev2, whole);
	result.addAll(Arrays.asList(constraints1));
	result.addAll(Arrays.asList(constraints2));
	result.addAll(Arrays.asList(constraints3));
	return result.toArray(new ITypeConstraint[result.size()]);
}
 
Example #4
Source File: TernaryControlStructure.java    From JDeodorant with MIT License 6 votes vote down vote up
private void initializeFields(Statement node)
{
	ConditionalExpression conditionalExpression = AbstractControlStructureUtilities.hasOneConditionalExpression(node);
	if (conditionalExpression != null)
	{
		this.conditionalExpression = conditionalExpression;
		this.condition             = conditionalExpression.getExpression();
		this.thenExpression        = conditionalExpression.getThenExpression();
		this.elseExpression        = conditionalExpression.getElseExpression();
	}
	else
	{
		this.conditionalExpression = null;
		this.condition             = null;
		this.thenExpression        = null;
		this.elseExpression        = null;
	}
}
 
Example #5
Source File: CodeBlock.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
private ConditionalExpr visit(ConditionalExpression node) {
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	ConditionalExpr conditionalExpr = new ConditionalExpr(startLine, endLine, node);
	
	Expr condition = (Expr) process(node.getExpression());
	condition.setParent(conditionalExpr);
	conditionalExpr.setCondition(condition);
	
	Expr first = (Expr) process(node.getThenExpression());
	first.setParent(conditionalExpr);
	conditionalExpr.setFirst(first);
	
	Expr snd = (Expr) process(node.getElseExpression());
	snd.setParent(conditionalExpr);
	conditionalExpr.setSecond(snd);
	
	if(first.getType() != null){
		conditionalExpr.setType(first.getType());
	} else {
		conditionalExpr.setType(snd.getType());
	}
	
	return conditionalExpr;
}
 
Example #6
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public final void endVisit(final ConditionalExpression node) {
	ConstraintVariable2 thenVariable= null;
	ConstraintVariable2 elseVariable= null;
	final Expression thenExpression= node.getThenExpression();
	if (thenExpression != null)
		thenVariable= (ConstraintVariable2) thenExpression.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
	final Expression elseExpression= node.getElseExpression();
	if (elseExpression != null)
		elseVariable= (ConstraintVariable2) elseExpression.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
	ITypeBinding binding= node.resolveTypeBinding();
	if (binding != null) {
		if (binding.isArray())
			binding= binding.getElementType();
		final ConstraintVariable2 ancestor= fModel.createIndependentTypeVariable(binding);
		if (ancestor != null) {
			node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, ancestor);
			if (thenVariable != null)
				fModel.createSubtypeConstraint(thenVariable, ancestor);
			if (elseVariable != null)
				fModel.createSubtypeConstraint(elseVariable, ancestor);
			if (thenVariable != null && elseVariable != null)
				fModel.createConditionalTypeConstraint(ancestor, thenVariable, elseVariable);
		}
	}
}
 
Example #7
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean visit(ConditionalExpression expr) {
	/*
	 * ConditionalExpression: Expression ? Expression : Expression
	 */
	activateDiffStyle(expr);
	handleExpression((Expression) expr.getExpression());
	appendSpace();
	appendQuestionMark();
	appendSpace();
	handleExpression((Expression) expr.getThenExpression());
	appendSpace();
	appendColon();
	appendSpace();
	handleExpression((Expression) expr.getElseExpression());
	deactivateDiffStyle(expr);
	return false;
}
 
Example #8
Source File: ControlVariable.java    From JDeodorant with MIT License 6 votes vote down vote up
private static List<Expression> removeExpressionsInAConditionalExpression(List<Expression> expressions, Statement containingStatement)
{
	ListIterator<Expression> it = expressions.listIterator();
	while (it.hasNext())
	{
		Expression currentUpdater = it.next();
		ASTNode parent = currentUpdater.getParent();
		while (parent != null && !parent.equals(containingStatement))
		{
			if (parent instanceof ConditionalExpression)
			{
				it.remove();
				break;
			}
			parent = parent.getParent();
		}
	}
	return expressions;
}
 
Example #9
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final ConditionalExpression node) {
  if (this.conditionalExpressionsAllowed) {
    String _string = null;
    if (node!=null) {
      _string=node.toString();
    }
    this.appendToBuffer(_string);
  } else {
    this.appendToBuffer("if ");
    boolean _startsWith = node.getExpression().toString().startsWith("(");
    if (_startsWith) {
      node.getExpression().accept(this);
      this.appendToBuffer(" ");
    } else {
      this.appendToBuffer("(");
      node.getExpression().accept(this);
      this.appendToBuffer(") ");
    }
    node.getThenExpression().accept(this);
    this.appendToBuffer(" else ");
    node.getElseExpression().accept(this);
  }
  this.appendSpaceToBuffer();
  return false;
}
 
Example #10
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 6 votes vote down vote up
protected boolean isTypeHolder(Object o) {
	if(o.getClass().equals(MethodInvocation.class) || o.getClass().equals(SuperMethodInvocation.class)			
			|| o.getClass().equals(NumberLiteral.class) || o.getClass().equals(StringLiteral.class)
			|| o.getClass().equals(CharacterLiteral.class) || o.getClass().equals(BooleanLiteral.class)
			|| o.getClass().equals(TypeLiteral.class) || o.getClass().equals(NullLiteral.class)
			|| o.getClass().equals(ArrayCreation.class)
			|| o.getClass().equals(ClassInstanceCreation.class)
			|| o.getClass().equals(ArrayAccess.class) || o.getClass().equals(FieldAccess.class)
			|| o.getClass().equals(SuperFieldAccess.class) || o.getClass().equals(ParenthesizedExpression.class)
			|| o.getClass().equals(SimpleName.class) || o.getClass().equals(QualifiedName.class)
			|| o.getClass().equals(CastExpression.class) || o.getClass().equals(InfixExpression.class)
			|| o.getClass().equals(PrefixExpression.class) || o.getClass().equals(InstanceofExpression.class)
			|| o.getClass().equals(ThisExpression.class) || o.getClass().equals(ConditionalExpression.class))
		return true;
	return false;
}
 
Example #11
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getInverseConditionalExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
	// try to find conditional expression as parent
	while (covering instanceof Expression) {
		if (covering instanceof ConditionalExpression)
			break;
		covering= covering.getParent();
	}
	if (!(covering instanceof ConditionalExpression)) {
		return false;
	}
	ConditionalExpression expression= (ConditionalExpression) covering;
	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}
	//
	AST ast= covering.getAST();
	ASTRewrite rewrite= ASTRewrite.create(ast);
	// prepare new conditional expression
	ConditionalExpression newExpression= ast.newConditionalExpression();
	newExpression.setExpression(getInversedExpression(rewrite, expression.getExpression()));
	newExpression.setThenExpression((Expression) rewrite.createCopyTarget(expression.getElseExpression()));
	newExpression.setElseExpression((Expression) rewrite.createCopyTarget(expression.getThenExpression()));
	// replace old expression with new
	rewrite.replace(expression, newExpression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_inverseConditionalExpression_description;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_CONDITIONAL_EXPRESSION, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example #12
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 #13
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Expression getBooleanExpression(ASTNode node) {
	if (!(node instanceof Expression)) {
		return null;
	}

	// check if the node is a location where it can be negated
	StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
	if (locationInParent == QualifiedName.NAME_PROPERTY) {
		node= node.getParent();
		locationInParent= node.getLocationInParent();
	}
	while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		node= node.getParent();
		locationInParent= node.getLocationInParent();
	}
	Expression expression= (Expression) node;
	if (!isBoolean(expression)) {
		return null;
	}
	if (expression.getParent() instanceof InfixExpression) {
		return expression;
	}
	if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY || locationInParent == MethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY || locationInParent == ConditionalExpression.EXPRESSION_PROPERTY
			|| locationInParent == PrefixExpression.OPERAND_PROPERTY) {
		return expression;
	}
	return null;
}
 
Example #14
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void addMemberCheckNull(Object member, boolean addSeparator) {
	ConditionalExpression cExpression= fAst.newConditionalExpression();

	// member != null ?
	InfixExpression infExpression= fAst.newInfixExpression();
	infExpression.setLeftOperand(createMemberAccessExpression(member, true, true));
	infExpression.setRightOperand(fAst.newNullLiteral());
	infExpression.setOperator(Operator.NOT_EQUALS);
	cExpression.setExpression(infExpression);

	SumExpressionBuilder builder= new SumExpressionBuilder(null);
	String[] arrayString= getContext().getTemplateParser().getBody();
	for (int i= 0; i < arrayString.length; i++) {
		addElement(processElement(arrayString[i], member), builder);
	}
	if (addSeparator)
		addElement(getContext().getTemplateParser().getSeparator(), builder);
	cExpression.setThenExpression(builder.getExpression());

	StringLiteral literal= fAst.newStringLiteral();
	literal.setLiteralValue(getContext().isSkipNulls() ? "" : "null"); //$NON-NLS-1$ //$NON-NLS-2$
	cExpression.setElseExpression(literal);

	ParenthesizedExpression pExpression= fAst.newParenthesizedExpression();
	pExpression.setExpression(cExpression);
	toStringExpressionBuilder.addExpression(pExpression);
}
 
Example #15
Source File: StringConcatenationGenerator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addElement(Object element, SumExpressionBuilder builder) {
	if (element instanceof String) {
		builder.addString((String)element);
	}
	if (element instanceof Expression) {
		Expression expr= (Expression)element;
		if (expr instanceof ConditionalExpression) {
			ParenthesizedExpression expr2= fAst.newParenthesizedExpression();
			expr2.setExpression(expr);
			expr= expr2;
		}
		builder.addExpression(expr);
	}
}
 
Example #16
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getPullNegationUpProposals(IInvocationContext context, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
	if (coveredNodes.size() != 1) {
		return false;
	}
	//
	ASTNode fullyCoveredNode= coveredNodes.get(0);

	Expression expression= getBooleanExpression(fullyCoveredNode);
	if (expression == null || (!(expression instanceof InfixExpression) && !(expression instanceof ConditionalExpression))) {
		return false;
	}
	//  we could produce quick assist
	if (resultingCollections == null) {
		return true;
	}
	//
	AST ast= expression.getAST();
	final ASTRewrite rewrite= ASTRewrite.create(ast);
	// prepared inverted expression
	Expression inversedExpression= getInversedExpression(rewrite, expression);
	// prepare ParenthesizedExpression
	ParenthesizedExpression parenthesizedExpression= ast.newParenthesizedExpression();
	parenthesizedExpression.setExpression(inversedExpression);
	// prepare NOT prefix expression
	PrefixExpression prefixExpression= ast.newPrefixExpression();
	prefixExpression.setOperator(PrefixExpression.Operator.NOT);
	prefixExpression.setOperand(parenthesizedExpression);
	// replace old expression
	rewrite.replace(expression, prefixExpression, null);
	// add correction proposal
	String label= CorrectionMessages.AdvancedQuickAssistProcessor_pullNegationUp;
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PULL_NEGATION_UP, image);
	resultingCollections.add(proposal);
	return true;
}
 
Example #17
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 #18
Source File: AbstractControlStructureUtilities.java    From JDeodorant with MIT License 5 votes vote down vote up
private static Integer getTernaryArgumentIndex(MethodInvocation ternaryMethodInvocation, ConditionalExpression conditionalExpression)
{
	List<Expression> arguments = ternaryMethodInvocation.arguments();
	for (int i = 0; i < arguments.size(); i++)
	{
		if (arguments.get(i).equals(conditionalExpression))
		{
			return i;
		}
	}
	return null;
}
 
Example #19
Source File: ControlDependenceTreeGenerator.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionStatementWithConditionalExpression(PDGNode node) {
	Statement statement = node.getASTStatement();
	if(statement instanceof ExpressionStatement) {
		ExpressionExtractor expressionExtractor = new ExpressionExtractor();
		List<Expression> conditionalExpressions = expressionExtractor.getConditionalExpressions(statement);
		if(conditionalExpressions.size() == 1) {
			ConditionalExpression conditional = (ConditionalExpression)conditionalExpressions.get(0);
			ASTNode parent = conditional.getParent();
			ASTNode grandParent = parent.getParent();
			if(grandParent != null && grandParent.equals(statement)) {
				return true;
			}
			if(parent instanceof ParenthesizedExpression) {
				if(grandParent != null) {
					if(grandParent.getParent() != null && grandParent.getParent().equals(statement)) {
						return true;
					}
				}
			}
		}
	}
	else if (statement instanceof ReturnStatement) {
		ReturnStatement returnStatement = (ReturnStatement)statement;
		if (returnStatement.getExpression() instanceof ConditionalExpression) {
			return true;
		}
	}
	return false;
}
 
Example #20
Source File: AbstractControlStructureUtilities.java    From JDeodorant with MIT License 5 votes vote down vote up
public static ConditionalExpression hasOneConditionalExpression(Statement statement)
{
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	List<Expression> conditionalExpressions = expressionExtractor.getConditionalExpressions(statement);
	if (conditionalExpressions.size() == 1)
	{
		return (ConditionalExpression)conditionalExpressions.get(0);
	}
	return null;
}
 
Example #21
Source File: OperatorPrecedence.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the precedence of the expression. Expression
 * with higher precedence are executed before expressions
 * with lower precedence.
 * i.e. in:
 * <br><code> int a= ++3--;</code></br>
 *
 * the  precedence order is
 * <ul>
 * <li>3</li>
 * <li>++</li>
 * <li>--</li>
 * <li>=</li>
 * </ul>
 * 1. 3 -(++)-> 4<br>
 * 2. 4 -(--)-> 3<br>
 * 3. 3 -(=)-> a<br>
 *
 * @param expression the expression to determine the precedence for
 * @return the precedence the higher to stronger the binding to its operand(s)
 */
public static int getExpressionPrecedence(Expression expression) {
	if (expression instanceof InfixExpression) {
		return getOperatorPrecedence(((InfixExpression)expression).getOperator());
	} else if (expression instanceof Assignment) {
		return ASSIGNMENT;
	} else if (expression instanceof ConditionalExpression) {
		return CONDITIONAL;
	} else if (expression instanceof InstanceofExpression) {
		return RELATIONAL;
	} else if (expression instanceof CastExpression) {
		return TYPEGENERATION;
	} else if (expression instanceof ClassInstanceCreation) {
		return POSTFIX;
	} else if (expression instanceof PrefixExpression) {
		return PREFIX;
	} else if (expression instanceof FieldAccess) {
		return POSTFIX;
	} else if (expression instanceof MethodInvocation) {
		return POSTFIX;
	} else if (expression instanceof ArrayAccess) {
		return POSTFIX;
	} else if (expression instanceof PostfixExpression) {
		return POSTFIX;
	}
	return Integer.MAX_VALUE;
}
 
Example #22
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(ConditionalExpression node) {
	if (skipNode(node))
		return;
	ConditionalFlowInfo info= createConditional();
	setFlowInfo(node, info);
	info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
	info.merge(
		getFlowInfo(node.getThenExpression()),
		getFlowInfo(node.getElseExpression()),
		fFlowContext);
}
 
Example #23
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(ConditionalExpression node) {
	if (skipNode(node))
		return;
	Expression thenPart= node.getThenExpression();
	Expression elsePart= node.getElseExpression();
	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 #24
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean returnsConditionalExpression() {
	ASTNode last= getLastStatement();
	if (last instanceof ReturnStatement) {
		return ((ReturnStatement)last).getExpression() instanceof ConditionalExpression;
	}
	return false;
}
 
Example #25
Source File: InputFlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(ConditionalExpression node) {
	if (skipNode(node)) {
		return;
	}
	Expression thenPart = node.getThenExpression();
	Expression elsePart = node.getElseExpression();
	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 #26
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * We create this access explicitly to catch a boolean variable used in
 * condition. Complicated expressions are handled in
 * {@link #visit(InfixExpression)}
 */
@Override
public boolean visit(ConditionalExpression node) {
	if (importer.topFromContainerStack(Method.class) != null) {
		importer.topFromContainerStack(Method.class).incCyclomaticComplexity();
		importer.createAccessFromExpression((Expression) node.getExpression());
	}
	return true;
}
 
Example #27
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(ConditionalExpression node) {
	if (skipNode(node)) {
		return;
	}
	ConditionalFlowInfo info = createConditional();
	setFlowInfo(node, info);
	info.mergeCondition(getFlowInfo(node.getExpression()), fFlowContext);
	info.merge(getFlowInfo(node.getThenExpression()), getFlowInfo(node.getElseExpression()), fFlowContext);
}
 
Example #28
Source File: Visitor.java    From RefactoringMiner with MIT License 5 votes vote down vote up
public boolean visit(ConditionalExpression node) {
	TernaryOperatorExpression ternary = new TernaryOperatorExpression(cu, filePath, node);
	ternaryOperatorExpressions.add(ternary);
	if(current.getUserObject() != null) {
		AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
		anonymous.getTernaryOperatorExpressions().add(ternary);
	}
	return super.visit(node);
}
 
Example #29
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Expression getBooleanExpression(ASTNode node) {
	if (!(node instanceof Expression)) {
		return null;
	}

	// check if the node is a location where it can be negated
	StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
	if (locationInParent == QualifiedName.NAME_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		node = node.getParent();
		locationInParent = node.getLocationInParent();
	}
	Expression expression = (Expression) node;
	if (!isBoolean(expression)) {
		return null;
	}
	if (expression.getParent() instanceof InfixExpression) {
		return expression;
	}
	if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
			|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
			|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
		return expression;
	}
	return null;
}
 
Example #30
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
 * <code>locationInParent</code> ?
 * 
 * @param expression the expression
 * @param parent the parent node
 * @param locationInParent location of expression in the parent
 * @param leftOperandType the type of the left operand in <code>parent</code> if
 *            <code>parent</code> is an infix expression with no bindings and
 *            <code>expression</code> is the right operand in it, <code>null</code> otherwise
 * @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
 *         otherwise.
 * 
 * @since 3.9
 */
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
	if (!expressionTypeNeedsParentheses(expression))
		return false;

	if (!locationNeedsParentheses(locationInParent)) {
		return false;
	}

	if (parent instanceof Expression) {
		Expression parentExpression= (Expression)parent;
		
		if (expression instanceof PrefixExpression) { // see bug 405096
			return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
		}

		int expressionPrecedence= OperatorPrecedence.getExpressionPrecedence(expression);
		int parentPrecedence= OperatorPrecedence.getExpressionPrecedence(parentExpression);

		if (expressionPrecedence > parentPrecedence)
			//(opEx) opParent and opEx binds more -> parentheses not needed
			return false;

		if (expressionPrecedence < parentPrecedence)
			//(opEx) opParent and opEx binds less -> parentheses needed
			return true;

		//(opEx) opParent binds equal

		if (parentExpression instanceof InfixExpression) {
			return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
		}

		if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
			return true;
		}

		return false;
	}

	return true;
}