类org.eclipse.jdt.core.dom.ConditionalExpression源码实例Demo

下面列出了怎么用org.eclipse.jdt.core.dom.ConditionalExpression的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: SimFix   文件: CodeBlock.java
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;
}
 
源代码2 项目: xtext-xtend   文件: JavaASTFlattener.java
@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;
}
 
@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);
		}
	}
}
 
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;
}
 
@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()]);
}
 
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));
	}
 
源代码7 项目: JDeodorant   文件: ASTNodeMatcher.java
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;
}
 
源代码8 项目: JDeodorant   文件: ControlVariable.java
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;
}
 
源代码9 项目: JDeodorant   文件: TernaryControlStructure.java
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;
	}
}
 
源代码10 项目: JDeodorant   文件: StyledStringVisitor.java
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;
}
 
源代码11 项目: eclipse.jdt.ls   文件: InputFlowAnalyzer.java
@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);
	}
}
 
源代码12 项目: eclipse.jdt.ls   文件: FlowAnalyzer.java
@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);
}
 
源代码13 项目: eclipse.jdt.ls   文件: InvertBooleanUtility.java
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;
}
 
源代码14 项目: RefactoringMiner   文件: Visitor.java
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);
}
 
源代码15 项目: jdt2famix   文件: AstVisitor.java
/**
 * 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;
}
 
public boolean returnsConditionalExpression() {
	ASTNode last= getLastStatement();
	if (last instanceof ReturnStatement) {
		return ((ReturnStatement)last).getExpression() instanceof ConditionalExpression;
	}
	return false;
}
 
@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);
	}
}
 
@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);
}
 
/**
 * 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;
}
 
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);
	}
}
 
@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);
}
 
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;
}
 
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;
}
 
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;
}
 
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;
}
 
源代码26 项目: JDeodorant   文件: ASTNodeMatcher.java
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);
}
 
源代码27 项目: JDeodorant   文件: ASTNodeMatcher.java
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;
}
 
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;
}
 
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;
}
 
源代码30 项目: SnowGraph   文件: JavaASTVisitor.java
private void parseExpression(MethodInfo methodInfo, Expression expression) {
    if (expression == null) {
        return;
    }//System.out.println(expression.toString()+" "+Annotation.nodeClassForType(expression.getNodeType()));
    if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) {
        List<Expression> expressions = ((ArrayInitializer) expression).expressions();
        for (Expression expression2 : expressions) {
            parseExpression(methodInfo, expression2);
        }
    }
    if (expression.getNodeType() == ASTNode.CAST_EXPRESSION) {
        parseExpression(methodInfo, ((CastExpression) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.CONDITIONAL_EXPRESSION) {
        parseExpression(methodInfo, ((ConditionalExpression) expression).getExpression());
        parseExpression(methodInfo, ((ConditionalExpression) expression).getElseExpression());
        parseExpression(methodInfo, ((ConditionalExpression) expression).getThenExpression());
    }
    if (expression.getNodeType() == ASTNode.INFIX_EXPRESSION) {
        parseExpression(methodInfo, ((InfixExpression) expression).getLeftOperand());
        parseExpression(methodInfo, ((InfixExpression) expression).getRightOperand());
    }
    if (expression.getNodeType() == ASTNode.INSTANCEOF_EXPRESSION) {
        parseExpression(methodInfo, ((InstanceofExpression) expression).getLeftOperand());
    }
    if (expression.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION) {
        parseExpression(methodInfo, ((ParenthesizedExpression) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
        parseExpression(methodInfo, ((PostfixExpression) expression).getOperand());
    }
    if (expression.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
        parseExpression(methodInfo, ((PrefixExpression) expression).getOperand());
    }
    if (expression.getNodeType() == ASTNode.THIS_EXPRESSION) {
        parseExpression(methodInfo, ((ThisExpression) expression).getQualifier());
    }
    if (expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
        List<Expression> arguments = ((MethodInvocation) expression).arguments();
        IMethodBinding methodBinding = ((MethodInvocation) expression).resolveMethodBinding();
        if (methodBinding != null)
            methodInfo.methodCalls.add(methodBinding);
        for (Expression exp : arguments)
            parseExpression(methodInfo, exp);
        parseExpression(methodInfo, ((MethodInvocation) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
        parseExpression(methodInfo, ((Assignment) expression).getLeftHandSide());
        parseExpression(methodInfo, ((Assignment) expression).getRightHandSide());
    }
    if (expression.getNodeType() == ASTNode.QUALIFIED_NAME) {
        if (((QualifiedName) expression).getQualifier().resolveTypeBinding() != null) {
            String name = ((QualifiedName) expression).getQualifier().resolveTypeBinding().getQualifiedName() + "." + ((QualifiedName) expression).getName().getIdentifier();
            methodInfo.fieldUsesSet.add(name);
        }
        parseExpression(methodInfo, ((QualifiedName) expression).getQualifier());
    }
}
 
 类所在包
 同包方法