本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ASTRewrite.replace方法的典型用法代码示例。如果您正苦于以下问题:Java ASTRewrite.replace方法的具体用法?Java ASTRewrite.replace怎么用?Java ASTRewrite.replace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.rewrite.ASTRewrite
的用法示例。
在下文中一共展示了ASTRewrite.replace方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeVariableWithInitializer
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private void removeVariableWithInitializer(ASTRewrite rewrite, ASTNode initializerNode, ASTNode statementNode, TextEditGroup group) {
boolean performRemove= fForceRemove;
if (!performRemove) {
ArrayList<Expression> sideEffectNodes= new ArrayList<>();
initializerNode.accept(new SideEffectFinder(sideEffectNodes));
performRemove= sideEffectNodes.isEmpty();
}
if (performRemove) {
removeStatement(rewrite, statementNode, group);
fRemovedAssignmentsCount++;
} else {
ASTNode initNode = rewrite.createMoveTarget(initializerNode);
ExpressionStatement statement = rewrite.getAST().newExpressionStatement((Expression) initNode);
rewrite.replace(statementNode, statement, null);
fAlteredAssignmentsCount++;
}
}
示例2: replaceCast
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
boolean castEnclosedInNecessaryParentheses= castExpression.getParent() instanceof ParenthesizedExpression
&& NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());
ASTNode toReplace= castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
ASTNode move;
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (replacement.getParent() instanceof ParenthesizedExpression) {
move= rewrite.createMoveTarget(replacement.getParent());
} else if (castEnclosedInNecessaryParentheses) {
toReplace= castExpression;
move= rewrite.createMoveTarget(replacement);
} else {
ParenthesizedExpression parentheses= replacement.getAST().newParenthesizedExpression();
parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
move= parentheses;
}
} else {
move= rewrite.createMoveTarget(replacement);
}
rewrite.replace(toReplace, move, group);
}
示例3: getInvalidQualificationProposals
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
public static void getInvalidQualificationProposals(IInvocationContext context, IProblemLocation problem,
Collection<CUCorrectionProposal> proposals) {
ASTNode node= problem.getCoveringNode(context.getASTRoot());
if (!(node instanceof Name)) {
return;
}
Name name= (Name) node;
IBinding binding= name.resolveBinding();
if (!(binding instanceof ITypeBinding)) {
return;
}
ITypeBinding typeBinding= (ITypeBinding)binding;
AST ast= node.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null);
String label= CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(),
rewrite, IProposalRelevance.QUALIFY_INNER_TYPE_NAME);
proposals.add(proposal);
}
示例4: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= fContext.getASTRoot();
AST ast= astRoot.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
AbstractTypeDeclaration decl= findTypeDeclaration(astRoot.types(), fOldName);
if (decl != null) {
ASTNode[] sameNodes= LinkedNodeFinder.findByNode(astRoot, decl.getName());
for (int i= 0; i < sameNodes.length; i++) {
rewrite.replace(sameNodes[i], ast.newSimpleName(fNewName), null);
}
}
return rewrite;
}
示例5: removeStatement
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private void removeStatement(ASTRewrite rewrite, ASTNode statementNode, TextEditGroup group) {
if (ASTNodes.isControlStatementBody(statementNode.getLocationInParent())) {
rewrite.replace(statementNode, rewrite.getAST().newBlock(), group);
} else {
rewrite.remove(statementNode, group);
}
}
示例6: createTypeRefChangeFullProposal
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
static CUCorrectionProposal createTypeRefChangeFullProposal(ICompilationUnit cu, ITypeBinding binding, ASTNode node, int relevance) {
ASTRewrite rewrite= ASTRewrite.create(node.getAST());
String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_full_type_description, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_DEFAULT));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance);
ImportRewrite imports= proposal.createImportRewrite((CompilationUnit) node.getRoot());
Type type= imports.addImport(binding, node.getAST());
rewrite.replace(node, type, null);
return proposal;
}
示例7: addStaticImportFavoriteProposals
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private static void addStaticImportFavoriteProposals(IInvocationContext context, SimpleName node, boolean isMethod,
Collection<CUCorrectionProposal> proposals) throws JavaModelException {
IJavaProject project= context.getCompilationUnit().getJavaProject();
if (JavaModelUtil.is50OrHigher(project)) {
String pref = PreferenceManager.getPrefs(context.getCompilationUnit().getResource())
.getFavoriteStaticMembers();
String[] favourites= pref.split(";"); //$NON-NLS-1$
if (favourites.length == 0) {
return;
}
CompilationUnit root= context.getASTRoot();
AST ast= root.getAST();
String name= node.getIdentifier();
String[] staticImports= SimilarElementsRequestor.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites);
for (int i= 0; i < staticImports.length; i++) {
String curr= staticImports[i];
ImportRewrite importRewrite= StubUtility.createImportRewrite(root, true);
ASTRewrite astRewrite= ASTRewrite.create(ast);
String label;
String qualifiedTypeName= Signature.getQualifier(curr);
String elementLabel= BasicElementLabels.getJavaElementName(JavaModelUtil.concatenateName(Signature.getSimpleName(qualifiedTypeName), name));
String res= importRewrite.addStaticImport(qualifiedTypeName, name, isMethod, new ContextSensitiveImportRewriteContext(root, node.getStartPosition(), importRewrite));
int dot= res.lastIndexOf('.');
if (dot != -1) {
String usedTypeName= importRewrite.addImport(qualifiedTypeName);
Name newName= ast.newQualifiedName(ast.newName(usedTypeName), ast.newSimpleName(name));
astRewrite.replace(node, newName, null);
label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_change_to_static_import_description, elementLabel);
} else {
label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_static_import_description, elementLabel);
}
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label,
context.getCompilationUnit(), astRewrite, IProposalRelevance.ADD_STATIC_IMPORT);
proposal.setImportRewrite(importRewrite);
proposals.add(proposal);
}
}
}
示例8: 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;
}
示例9: 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);
}