use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class InlineTempRefactoring method inlineTemp.
private void inlineTemp(CompilationUnitRewrite cuRewrite) throws JavaModelException {
SimpleName[] references = getReferences();
TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_inline_edit_name);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
for (int i = 0; i < references.length; i++) {
SimpleName curr = references[i];
ASTNode initializerCopy = getInitializerSource(cuRewrite, curr);
rewrite.replace(curr, initializerCopy, groupDesc);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class IntroduceFactoryRefactoring method addAllChangesFor.
/**
* Add all changes necessary on the <code>ICompilationUnit</code> in the given
* <code>SearchResultGroup</code> to implement the refactoring transformation
* to the given <code>CompilationUnitChange</code>.
* @param rg the <code>SearchResultGroup</code> for which changes should be created
* @param unitHandle
* @param unitChange the CompilationUnitChange object for the compilation unit in question
* @return <code>true</code> iff a change has been added
* @throws CoreException
*/
private boolean addAllChangesFor(SearchResultGroup rg, ICompilationUnit unitHandle, CompilationUnitChange unitChange) throws CoreException {
// ICompilationUnit unitHandle= rg.getCompilationUnit();
Assert.isTrue(rg == null || rg.getCompilationUnit() == unitHandle);
CompilationUnit unit = getASTFor(unitHandle);
ASTRewrite unitRewriter = ASTRewrite.create(unit.getAST());
MultiTextEdit root = new MultiTextEdit();
boolean someChange = false;
unitChange.setEdit(root);
fImportRewriter = StubUtility.createImportRewrite(unit, true);
// First create the factory method
if (unitHandle.equals(fFactoryUnitHandle)) {
TextEditGroup factoryGD = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_addFactoryMethod);
createFactoryChange(unitRewriter, unit, factoryGD);
unitChange.addTextEditGroup(factoryGD);
someChange = true;
}
// Now rewrite all the constructor calls to use the factory method
if (rg != null)
if (replaceConstructorCalls(rg, unit, unitRewriter, unitChange))
someChange = true;
// Finally, make the constructor private, if requested.
if (shouldProtectConstructor() && isConstructorUnit(unitHandle)) {
TextEditGroup declGD = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_protectConstructor);
if (protectConstructor(unit, unitRewriter, declGD)) {
unitChange.addTextEditGroup(declGD);
someChange = true;
}
}
if (someChange) {
root.addChild(unitRewriter.rewriteAST());
root.addChild(fImportRewriter.rewriteImports(null));
}
return someChange;
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project che by eclipse.
the class ExtractTempRefactoring method createTempType.
private Type createTempType() throws CoreException {
Expression expression = getSelectedExpression().getAssociatedExpression();
Type resultingType = null;
ITypeBinding typeBinding = expression.resolveTypeBinding();
ASTRewrite rewrite = fCURewrite.getASTRewrite();
AST ast = rewrite.getAST();
if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
} else if (expression instanceof CastExpression) {
resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
} else {
if (typeBinding == null) {
typeBinding = ASTResolving.guessBindingForReference(expression);
}
if (typeBinding != null) {
typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
ImportRewrite importRewrite = fCURewrite.getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
resultingType = importRewrite.addImport(typeBinding, ast, context);
} else {
//$NON-NLS-1$
resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
if (fLinkedProposalModel != null) {
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(resultingType), false);
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
}
}
}
return resultingType;
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project bayou by capergroup.
the class Visitor method postprocessGlobal.
/**
* 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);
// add all catch(...) types to imports
toImport.addAll(sketch.exceptionsThrown());
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);
}
use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project eclipse-pmd by acanda.
the class ASTRewriteQuickFix method fixMarker.
@Override
protected boolean fixMarker(final T node, final IDocument document, final Map<?, ?> options) throws JavaModelException {
final ASTRewrite rewrite = ASTRewrite.create(node.getAST());
final boolean isSuccessful = rewrite(node, rewrite);
if (isSuccessful) {
rootTextEdit.addChild(rewrite.rewriteAST(document, options));
}
return isSuccessful;
}
Aggregations