当前位置: 首页>>代码示例>>Java>>正文


Java AST.newParenthesizedExpression方法代码示例

本文整理汇总了Java中org.eclipse.jdt.core.dom.AST.newParenthesizedExpression方法的典型用法代码示例。如果您正苦于以下问题:Java AST.newParenthesizedExpression方法的具体用法?Java AST.newParenthesizedExpression怎么用?Java AST.newParenthesizedExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.core.dom.AST的用法示例。


在下文中一共展示了AST.newParenthesizedExpression方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getAssignedValue

import org.eclipse.jdt.core.dom.AST; //导入方法依赖的package包/类
/**
 * Converts an assignment, postfix expression or prefix expression into an
 * assignable equivalent expression using the getter.
 *
 * @param node
 *            the assignment/prefix/postfix node
 * @param astRewrite
 *            the astRewrite to use
 * @param getterExpression
 *            the expression to insert for read accesses or
 *            <code>null</code> if such an expression does not exist
 * @param variableType
 *            the type of the variable that the result will be assigned to
 * @param is50OrHigher
 *            <code>true</code> if a 5.0 or higher environment can be used
 * @return an expression that can be assigned to the type variableType with
 *         node being replaced by a equivalent expression using the getter
 */
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
	InfixExpression.Operator op = null;
	AST ast = astRewrite.getAST();
	if (isNotInBlock(node)) {
		return null;
	}
	if (node.getNodeType() == ASTNode.ASSIGNMENT) {
		Assignment assignment = ((Assignment) node);
		Expression rightHandSide = assignment.getRightHandSide();
		Expression copiedRightOp = (Expression) astRewrite.createCopyTarget(rightHandSide);
		if (assignment.getOperator() == Operator.ASSIGN) {
			ITypeBinding rightHandSideType = rightHandSide.resolveTypeBinding();
			copiedRightOp = createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
			return copiedRightOp;
		}
		if (getterExpression != null) {
			InfixExpression infix = ast.newInfixExpression();
			infix.setLeftOperand(getterExpression);
			infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
			ITypeBinding infixType = infix.resolveTypeBinding();
			if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, infix, variableType)) {
				ParenthesizedExpression p = ast.newParenthesizedExpression();
				p.setExpression(copiedRightOp);
				copiedRightOp = p;
			}
			infix.setRightOperand(copiedRightOp);
			return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
		}
	} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
		PostfixExpression po = (PostfixExpression) node;
		if (po.getOperator() == PostfixExpression.Operator.INCREMENT) {
			op = InfixExpression.Operator.PLUS;
		}
		if (po.getOperator() == PostfixExpression.Operator.DECREMENT) {
			op = InfixExpression.Operator.MINUS;
		}
	} else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
		PrefixExpression pe = (PrefixExpression) node;
		if (pe.getOperator() == PrefixExpression.Operator.INCREMENT) {
			op = InfixExpression.Operator.PLUS;
		}
		if (pe.getOperator() == PrefixExpression.Operator.DECREMENT) {
			op= InfixExpression.Operator.MINUS;
		}
	}
	if (op != null && getterExpression != null) {
		return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
	}
	return null;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:69,代码来源:GetterSetterUtil.java

示例2: createNarrowCastIfNessecary

import org.eclipse.jdt.core.dom.AST; //导入方法依赖的package包/类
/**
 * Checks if the assignment needs a downcast and inserts it if necessary
 *
 * @param expression
 *            the right hand-side
 * @param expressionType
 *            the type of the right hand-side. Can be null
 * @param ast
 *            the AST
 * @param variableType
 *            the Type of the variable the expression will be assigned to
 * @param is50OrHigher
 *            if <code>true</code> java 5.0 code will be assumed
 * @return the casted expression if necessary
 */
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
	PrimitiveType castTo = null;
	if (variableType.isEqualTo(expressionType))
	 {
		return expression; //no cast for same type
	}
	if (is50OrHigher) {
		if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) {
			castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
		}
		if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) {
			castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
		}
		if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) {
			castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
		}
	}
	if (ast.resolveWellKnownType("char").isEqualTo(variableType)) {
		castTo = ast.newPrimitiveType(PrimitiveType.CHAR);
	}
	if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) {
		castTo = ast.newPrimitiveType(PrimitiveType.BYTE);
	}
	if (ast.resolveWellKnownType("short").isEqualTo(variableType)) {
		castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
	}
	if (castTo != null) {
		CastExpression cast = ast.newCastExpression();
		if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
			ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
			parenthesized.setExpression(expression);
			cast.setExpression(parenthesized);
		} else {
			cast.setExpression(expression);
		}
		cast.setType(castTo);
		return cast;
	}
	return expression;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:56,代码来源:GetterSetterUtil.java

示例3: useExistingParentCastProposal

import org.eclipse.jdt.core.dom.AST; //导入方法依赖的package包/类
private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression,
		Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes,
		Collection<CUCorrectionProposal> proposals) {
	ITypeBinding castType= expression.getType().resolveBinding();
	if (castType == null) {
		return false;
	}
	if (paramTypes != null) {
		if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
			return false;
		}
	} else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
		return false;
	}
	ITypeBinding bindingToCast= accessExpression.resolveTypeBinding();
	if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
		return false;
	}

	IMethodBinding res= Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
	if (res != null) {
		AST ast= expression.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		CastExpression newCast= ast.newCastExpression();
		newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
		newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
		ParenthesizedExpression parents= ast.newParenthesizedExpression();
		parents.setExpression(newCast);

		ASTNode node= rewrite.createCopyTarget(expression.getExpression());
		rewrite.replace(expression, node, null);
		rewrite.replace(accessExpression, parents, null);

		String label= CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
		ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite,
				IProposalRelevance.ADD_PARENTHESES_AROUND_CAST);
		proposals.add(proposal);
		return true;
	}
	return false;
}
 
开发者ID:eclipse,项目名称:eclipse.jdt.ls,代码行数:42,代码来源:UnresolvedElementsSubProcessor.java


注:本文中的org.eclipse.jdt.core.dom.AST.newParenthesizedExpression方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。