本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ASTRewrite.createCopyTarget方法的典型用法代码示例。如果您正苦于以下问题:Java ASTRewrite.createCopyTarget方法的具体用法?Java ASTRewrite.createCopyTarget怎么用?Java ASTRewrite.createCopyTarget使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.rewrite.ASTRewrite
的用法示例。
在下文中一共展示了ASTRewrite.createCopyTarget方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyTypeAndAddDimensions
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* Creates a {@link ASTRewrite#createCopyTarget(ASTNode) copy} of <code>type</code>
* and adds <code>extraDimensions</code> to it.
*
* @param type the type to copy
* @param extraDimensions the dimensions to add
* @param rewrite the ASTRewrite with which to create new nodes
* @return the copy target with added dimensions
*/
public static Type copyTypeAndAddDimensions(Type type, List<Dimension> extraDimensions, ASTRewrite rewrite) {
AST ast= rewrite.getAST();
if (extraDimensions.isEmpty()) {
return (Type) rewrite.createCopyTarget(type);
}
ArrayType result;
if (type instanceof ArrayType) {
ArrayType arrayType= (ArrayType) type;
Type varElementType= (Type) rewrite.createCopyTarget(arrayType.getElementType());
result= ast.newArrayType(varElementType, 0);
result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
result.dimensions().addAll(copyDimensions(arrayType.dimensions(), rewrite));
} else {
Type elementType= (Type) rewrite.createCopyTarget(type);
result= ast.newArrayType(elementType, 0);
result.dimensions().addAll(copyDimensions(extraDimensions, rewrite));
}
return result;
}
示例2: splitUpDeclarations
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) {
if (sideEffects.size() > 0) {
ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent());
Statement previousStatement= originalStatement;
for (int i= 0; i < sideEffects.size(); i++) {
Expression sideEffect= sideEffects.get(i);
Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit);
statementRewrite.insertAfter(wrapped, previousStatement, group);
previousStatement= wrapped;
}
VariableDeclarationStatement newDeclaration= null;
List<VariableDeclarationFragment> fragments= originalStatement.fragments();
int fragIndex= fragments.indexOf(frag);
ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1);
while (fragmentIterator.hasNext()) {
VariableDeclarationFragment currentFragment= fragmentIterator.next();
VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
if (newDeclaration == null) {
newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType());
newDeclaration.setType(copiedType);
} else {
newDeclaration.fragments().add(movedFragment);
}
}
if (newDeclaration != null){
statementRewrite.insertAfter(newDeclaration, previousStatement, group);
if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){
rewrite.remove(originalStatement, group);
}
}
}
}
示例3: getAssignedValue
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的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;
}
示例4: useExistingParentCastProposal
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的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;
}
示例5: getCopyOrReplacement
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* If the given <code>node</code> has already been rewritten, undo that rewrite and return the
* replacement version of the node. Otherwise, return the result of
* {@link ASTRewrite#createCopyTarget(ASTNode)}.
*
* @param rewrite ASTRewrite for the given node
* @param node the node to get the replacement or to create a copy placeholder for
* @param group the edit group which collects the corresponding text edits, or <code>null</code>
* if ungrouped
* @return the replacement node if the given <code>node</code> has already been rewritten or the
* new copy placeholder node
*/
public static ASTNode getCopyOrReplacement(ASTRewrite rewrite, ASTNode node, TextEditGroup group) {
ASTNode rewrittenNode= (ASTNode) rewrite.get(node.getParent(), node.getLocationInParent());
if (rewrittenNode != node) {
// Undo previous rewrite to avoid the problem that the same node would be inserted in two places:
rewrite.replace(rewrittenNode, node, group);
return rewrittenNode;
}
return rewrite.createCopyTarget(node);
}