本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ASTRewrite.create方法的典型用法代码示例。如果您正苦于以下问题:Java ASTRewrite.create方法的具体用法?Java ASTRewrite.create怎么用?Java ASTRewrite.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.jdt.core.dom.rewrite.ASTRewrite
的用法示例。
在下文中一共展示了ASTRewrite.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: postprocessGlobal
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* Performs global post-processing of synthesized code:
* - Adds import declarations
*
* @param ast the owner of the document
* @param env environment that was used for synthesis
* @param document draft program document
* @throws BadLocationException if an error occurred when rewriting document
*/
private void postprocessGlobal(AST ast, Environment env, Document document)
throws BadLocationException {
/* add imports */
ASTRewrite rewriter = ASTRewrite.create(ast);
ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
Set<Class> toImport = new HashSet<>(env.imports);
toImport.addAll(sketch.exceptionsThrown()); // add all catch(...) types to imports
for (Class cls : toImport) {
while (cls.isArray())
cls = cls.getComponentType();
if (cls.isPrimitive() || cls.getPackage().getName().equals("java.lang"))
continue;
ImportDeclaration impDecl = cu.getAST().newImportDeclaration();
String className = cls.getName().replaceAll("\\$", "\\.");
impDecl.setName(cu.getAST().newName(className.split("\\.")));
lrw.insertLast(impDecl, null);
}
rewriter.rewriteAST(document, null).apply(document);
}
示例2: addMethodWithConstrNameProposals
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
public static void addMethodWithConstrNameProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
ICompilationUnit cu= context.getCompilationUnit();
ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof MethodDeclaration) {
MethodDeclaration declaration= (MethodDeclaration) selectedNode;
ASTRewrite rewrite= ASTRewrite.create(declaration.getAST());
rewrite.set(declaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null);
String label= CorrectionMessages.ReturnTypeSubProcessor_constrnamemethod_description;
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_CONSTRUCTOR);
proposals.add(proposal);
}
}
示例3: 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;
}
示例4: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode= fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode= null;
CompilationUnit newRoot= fAstRoot;
if (boundNode != null) {
declNode= boundNode; // is same CU
} else {
newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode= newRoot.findDeclaringNode(fBinding.getKey());
}
ImportRewrite imports= createImportRewrite(newRoot);
if (declNode instanceof TypeDeclaration) {
AST ast= declNode.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(declNode, imports);
Type newInterface= imports.addImport(fNewInterface, ast, importRewriteContext, TypeLocation.OTHER);
ListRewrite listRewrite= rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
listRewrite.insertLast(newInterface, null);
return rewrite;
}
return null;
}
示例5: getRemoveJavadocTagProposals
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
public static void getRemoveJavadocTagProposals(IInvocationContext context, IProblemLocation problem,
Collection<CUCorrectionProposal> proposals) {
ASTNode node= problem.getCoveringNode(context.getASTRoot());
while (node != null && !(node instanceof TagElement)) {
node= node.getParent();
}
if (node == null) {
return;
}
ASTRewrite rewrite= ASTRewrite.create(node.getAST());
rewrite.remove(node, null);
String label= CorrectionMessages.JavadocTagsSubProcessor_removetag_description;
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite,
IProposalRelevance.REMOVE_TAG));
}
示例6: 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);
}
示例7: Visitor
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* Initializes the visitor
*
* @param sketch sketch to be synthesized
* @param document draft program document
* @param cu draft program compilation unit
* @param mode enumeration mode
*/
public Visitor(DSubTree sketch, Document document, CompilationUnit cu, Synthesizer.Mode mode) {
this.sketch = sketch;
this.document = document;
this.cu = cu;
this.rewriter = ASTRewrite.create(this.cu.getAST());
this.currentScope = new ArrayList<>();
this.mode = mode;
}
示例8: addBuilder
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
private void addBuilder(ICompilationUnit iCompilationUnit, BuilderType builderType) {
CompilationUnit compilationUnit = compilationUnitParser.parse(iCompilationUnit);
AST ast = compilationUnit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
CompilationUnitModificationDomain compilationUnitModificationDomain = builderOwnerClassFinder.provideBuilderOwnerClass(compilationUnit, ast, rewriter, iCompilationUnit);
builderGenerators.stream()
.filter(builderGenerator -> builderGenerator.canHandle(builderType))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No builder generator can handle " + builderType))
.generateBuilder(compilationUnitModificationDomain);
compilationUnitSourceSetter.commitCodeChange(iCompilationUnit, rewriter);
}
示例9: addCopyrightsHeader
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* Adds copyright header to the compilation unit
*
* @param compilationUnit
* compilation unit affected
* @return compilation unit change
*/
public CompilationUnitChange addCopyrightsHeader(final CompilationUnit compilationUnit) {
final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
change = new CompilationUnitChange(ADD_COPYRIGHT, unit);
rewriter = ASTRewrite.create(compilationUnit.getAST());
final ListRewrite listRewrite = rewriter.getListRewrite(compilationUnit.getPackage(),
PackageDeclaration.ANNOTATIONS_PROPERTY);
final Comment placeHolder = (Comment) rewriter.createStringPlaceholder(getCopyrightText() + NEW_LINE_SEPARATOR,
ASTNode.BLOCK_COMMENT);
listRewrite.insertFirst(placeHolder, null);
rewriteCompilationUnit(unit, getNewUnitSource(unit, null));
return change;
}
示例10: replaceCopyrightsHeader
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
/**
* Replaces copyright header to the compilation unit
*
* @param compilationUnit
* compilation unit affected
* @return compilation unit change
*/
public CompilationUnitChange replaceCopyrightsHeader(final CompilationUnit compilationUnit) {
final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
change = new CompilationUnitChange(OVERRIDE_COPYRIGHT, unit);
rewriter = ASTRewrite.create(compilationUnit.getAST());
final List<Comment> comments = getCommentList(compilationUnit);
Comment copyrightComment = null;
if (!comments.isEmpty()) {
copyrightComment = comments.get(0);
}
rewriteCompilationUnit(unit, getNewUnitSource(unit, copyrightComment));
return change;
}
示例11: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= (CompilationUnit) fInvocationNode.getRoot();
ASTNode methodDecl= astRoot.findDeclaringNode(fSenderBinding);
ASTNode newMethodDecl= null;
if (methodDecl != null) {
newMethodDecl= methodDecl;
} else {
astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newMethodDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newMethodDecl instanceof MethodDeclaration) {
MethodDeclaration decl= (MethodDeclaration) newMethodDecl;
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
if (fParameterChanges != null) {
modifyParameters(rewrite, decl);
}
if (fExceptionChanges != null) {
modifyExceptions(rewrite, decl);
}
return rewrite;
}
return null;
}
示例12: createChange
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
final String NN = ""; //$NON-NLS-1$
if (pm == null) {
pm = new NullProgressMonitor();
}
pm.beginTask(NN, 2);
try {
final CompilationUnitChange result = new CompilationUnitChange(getName(), fCUnit);
if (fLeaveDirty) {
result.setSaveMode(TextFileChange.LEAVE_DIRTY);
}
MultiTextEdit root = new MultiTextEdit();
result.setEdit(root);
fRewriter = ASTRewrite.create(fAnalyzer.getEnclosingBodyDeclaration().getAST());
fRewriter.setTargetSourceRangeComputer(new SelectionAwareSourceRangeComputer(fAnalyzer.getSelectedNodes(), fCUnit.getBuffer(), fSelection.getOffset(), fSelection.getLength()));
fImportRewrite = StubUtility.createImportRewrite(fRootNode, true);
fLinkedProposalModel = new LinkedProposalModel();
fScope = CodeScopeBuilder.perform(fAnalyzer.getEnclosingBodyDeclaration(), fSelection).findScope(fSelection.getOffset(), fSelection.getLength());
fScope.setCursor(fSelection.getOffset());
fSelectedNodes = fAnalyzer.getSelectedNodes();
createTryCatchStatement(fCUnit.getBuffer(), fCUnit.findRecommendedLineSeparator());
if (fImportRewrite.hasRecordedChanges()) {
TextEdit edit = fImportRewrite.rewriteImports(null);
root.addChild(edit);
result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { edit }));
}
TextEdit change = fRewriter.rewriteAST();
root.addChild(change);
result.addTextEditGroup(new TextEditGroup(NN, new TextEdit[] { change }));
return result;
} finally {
pm.done();
}
}
示例13: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTRewrite rewrite= ASTRewrite.create(fBodyDecl.getAST());
if (fBodyDecl instanceof MethodDeclaration) {
insertAllMissingMethodTags(rewrite, (MethodDeclaration) fBodyDecl);
} else {
insertAllMissingTypeTags(rewrite, (TypeDeclaration) fBodyDecl);
}
return rewrite;
}
示例14: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode);
ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl= null;
boolean isInDifferentCU;
if (typeDecl != null) {
isInDifferentCU= false;
newTypeDecl= typeDecl;
} else {
isInDifferentCU= true;
astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl != null) {
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
MethodDeclaration newStub= getStub(rewrite, newTypeDecl);
ChildListPropertyDescriptor property= ASTNodes.getBodyDeclarationsProperty(newTypeDecl);
List<BodyDeclaration> members= ASTNodes.getBodyDeclarations(newTypeDecl);
int insertIndex;
if (isConstructor()) {
insertIndex= findConstructorInsertIndex(members);
} else if (!isInDifferentCU) {
insertIndex= findMethodInsertIndex(members, fNode.getStartPosition());
} else {
insertIndex= members.size();
}
ListRewrite listRewriter= rewrite.getListRewrite(newTypeDecl, property);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
示例15: getRewrite
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入方法依赖的package包/类
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode);
ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl= null;
if (typeDecl != null) {
newTypeDecl= typeDecl;
} else {
astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration newAnnotationTypeDecl= (AnnotationTypeDeclaration) newTypeDecl;
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AnnotationTypeMemberDeclaration newStub= getStub(rewrite, newAnnotationTypeDecl);
List<BodyDeclaration> members= newAnnotationTypeDecl.bodyDeclarations();
int insertIndex= members.size();
ListRewrite listRewriter= rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}