本文整理汇总了Java中org.eclipse.jdt.core.dom.rewrite.ASTRewrite类的典型用法代码示例。如果您正苦于以下问题:Java ASTRewrite类的具体用法?Java ASTRewrite怎么用?Java ASTRewrite使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ASTRewrite类属于org.eclipse.jdt.core.dom.rewrite包,在下文中一共展示了ASTRewrite类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNewCastTypeNode
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
AST ast= rewrite.getAST();
ImportRewriteContext context= new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);
if (fCastType != null) {
return importRewrite.addImport(fCastType, ast,context, TypeLocation.CAST);
}
ASTNode node= fNodeToCast;
ASTNode parent= node.getParent();
if (parent instanceof CastExpression) {
node= parent;
parent= parent.getParent();
}
while (parent instanceof ParenthesizedExpression) {
node= parent;
parent= parent.getParent();
}
if (parent instanceof MethodInvocation) {
MethodInvocation invocation= (MethodInvocation) node.getParent();
if (invocation.getExpression() == node) {
IBinding targetContext= ASTResolving.getParentMethodOrTypeBinding(node);
ITypeBinding[] bindings= ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
if (bindings.length > 0) {
ITypeBinding first= getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());
Type newTypeNode= importRewrite.addImport(first, ast, context, TypeLocation.CAST);
return newTypeNode;
}
}
}
Type newCastType= ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
return newCastType;
}
示例2: save
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
/**
* Save the AST int he Compilation Unit
*
* @param testInterface
* @param rewrite
* @throws CoreException
*/
public static void save(CompilationUnit unit, ASTRewrite rewrite) throws CoreException {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
IPath path = unit.getJavaElement().getPath();
try {
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
IDocument document = textFileBuffer.getDocument();
TextEdit edit = rewrite.rewriteAST(document, null);
edit.apply(document);
textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */);
} catch (Exception e) {
ResourceManager.logException(e);
} finally {
// disconnect the path
bufferManager.disconnect(path, null);
}
}
示例3: 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);
}
示例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: installLinkedVisibilityProposals
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
public static void installLinkedVisibilityProposals(LinkedProposalModel linkedProposalModel, ASTRewrite rewrite, List<IExtendedModifier> modifiers, boolean inInterface, String groupId) {
ASTNode modifier = findVisibilityModifier(modifiers);
if (modifier != null) {
int selected = ((Modifier) modifier).getKeyword().toFlagValue();
LinkedProposalPositionGroup positionGroup = linkedProposalModel.getPositionGroup(groupId, true);
positionGroup.addPosition(rewrite.track(modifier), false);
positionGroup.addProposal(new ModifierLinkedModeProposal(selected, 10));
// add all others
int[] flagValues = inInterface ? new int[] { Modifier.PUBLIC, 0 } : new int[] { Modifier.PUBLIC, 0, Modifier.PROTECTED, Modifier.PRIVATE };
for (int i = 0; i < flagValues.length; i++) {
if (flagValues[i] != selected) {
positionGroup.addProposal(new ModifierLinkedModeProposal(flagValues[i], 9 - i));
}
}
}
}
示例6: setFieldAnnotation
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
private void setFieldAnnotation(ASTRewrite rewrite, FieldDeclaration fieldDeclaration, String annotation) {
SingleMemberAnnotation newFieldAnnotation = fieldDeclaration.getAST().newSingleMemberAnnotation();
newFieldAnnotation.setTypeName(rewrite.getAST().newSimpleName("Domain"));
StringLiteral newStringLiteral = rewrite.getAST().newStringLiteral();
newStringLiteral.setLiteralValue(annotation);
newFieldAnnotation.setValue(newStringLiteral);
ASTNode modifier = getModifier(fieldDeclaration.modifiers());
if (modifier != null) {
ListRewrite paramRewrite = rewrite.getListRewrite(fieldDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
paramRewrite.insertAfter(newFieldAnnotation, modifier, null);
}
else {
ListRewrite fieldRewrite = rewrite.getListRewrite(fieldDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
fieldRewrite.insertFirst(newFieldAnnotation, null);
}
}
示例7: annotateMethods
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
/**
* @param rewrite
* @param declaration
*/
private void annotateMethods(ASTRewrite rewrite, TypeDeclaration declaration) {
MethodDeclaration[] methods = declaration.getMethods();
for (int i = 0; i < methods.length; i++) {
MethodDeclaration methodDeclaration = methods[i];
annotateMethodReturnType(rewrite, methodDeclaration);
annotateMethodParameters(rewrite, methodDeclaration);
DefaultingVisitor visitor = new DefaultingVisitor();
visitor.rewrite = rewrite;
Block body = methodDeclaration.getBody();
if (body != null) {
body.accept(visitor);
}
}
}
示例8: 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;
}
示例9: addLinkedPositions
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
@Override
protected void addLinkedPositions(final ASTRewrite rewrite, final VariableDeclarationFragment fragment, final LinkedProposalModel positionGroups) {
Assert.isNotNull(rewrite);
Assert.isNotNull(fragment);
final Expression initializer= fragment.getInitializer();
if (initializer != null) {
LinkedProposalPositionGroup group= new LinkedProposalPositionGroup(GROUP_INITIALIZER);
group.addPosition(rewrite.track(initializer), true);
positionGroups.addPositionGroup(group);
}
}
示例10: 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);
}
}
}
}
示例11: 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);
}
示例12: 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);
}
}
示例13: addNewJavadoc
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
private void addNewJavadoc(ASTRewrite rewrite, MethodDeclaration decl, ImportRewriteContext context) throws CoreException {
IType parentType = fField.getDeclaringType();
String typeName = Signature.toString(fField.getTypeSignature());
String accessorName = StubUtility.getBaseName(fField);
String lineDelim = "\n";
String comment = null;
String name = getFunctionName();
if (isGetter) {
comment = CodeGeneration.getGetterComment(fField.getCompilationUnit(), parentType.getTypeQualifiedName('.'), name, fField.getElementName(), typeName, accessorName, lineDelim);
} else {
String argname = getArgumentName();
comment = CodeGeneration.getSetterComment(fField.getCompilationUnit(), parentType.getTypeQualifiedName('.'), name, fField.getElementName(), typeName, argname, accessorName, lineDelim);
}
comment = comment.substring(0, comment.lastIndexOf(lineDelim));
if (comment != null) {
Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
decl.setJavadoc(javadoc);
}
}
示例14: addEdits
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; //导入依赖的package包/类
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
super.addEdits(document, editRoot);
ASTRewrite rewrite= getRewrite();
if (rewrite != null) {
try {
TextEdit edit= rewrite.rewriteAST();
editRoot.addChild(edit);
} catch (IllegalArgumentException e) {
throw new CoreException(StatusFactory.newErrorStatus("Invalid AST rewriter", e));
}
}
if (fImportRewrite != null) {
editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor()));
}
}
示例15: 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;
}