use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createCompilationUnitChange.
public CompilationUnitChange createCompilationUnitChange(IProgressMonitor pm) throws CoreException {
final CompilationUnitRewrite rewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode);
final ITypeBinding[] typeParameters = getTypeParameters();
addNestedClass(rewrite, typeParameters);
modifyConstructorCall(rewrite, typeParameters);
return rewrite.createChange(RefactoringCoreMessages.ConvertAnonymousToNestedRefactoring_name, false, pm);
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class IntroduceIndirectionRefactoring method getCachedCURewrite.
private CompilationUnitRewrite getCachedCURewrite(ICompilationUnit unit) {
CompilationUnitRewrite rewrite = fRewrites.get(unit);
if (rewrite == null && fSelectionMethodInvocation != null) {
CompilationUnit cuNode = ASTResolving.findParentCompilationUnit(fSelectionMethodInvocation);
if (cuNode != null && cuNode.getJavaElement().equals(unit)) {
rewrite = new CompilationUnitRewrite(unit, cuNode);
fRewrites.put(unit, rewrite);
}
}
if (rewrite == null) {
rewrite = new CompilationUnitRewrite(unit);
fRewrites.put(unit, rewrite);
}
return rewrite;
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class IntroduceIndirectionRefactoring method createIntermediaryMethod.
private void createIntermediaryMethod() throws CoreException {
CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
AST ast = imRewrite.getAST();
MethodDeclaration intermediary = ast.newMethodDeclaration();
// Intermediary class is non-anonymous
AbstractTypeDeclaration type = (AbstractTypeDeclaration) typeToDeclaration(fIntermediaryType, imRewrite.getRoot());
// Name
intermediary.setName(ast.newSimpleName(fIntermediaryMethodName));
// Flags
List<IExtendedModifier> modifiers = intermediary.modifiers();
if (!fIntermediaryType.isInterface()) {
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD));
}
modifiers.add(imRewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD));
// Parameters
String targetParameterName = StubUtility.suggestArgumentName(getProject(), fIntermediaryFirstParameterType.getName(), fTargetMethod.getParameterNames());
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(type, imRewrite.getImportRewrite());
if (!isStaticTarget()) {
// Add first param
SingleVariableDeclaration parameter = imRewrite.getAST().newSingleVariableDeclaration();
Type t = imRewrite.getImportRewrite().addImport(fIntermediaryFirstParameterType, imRewrite.getAST(), context);
if (fIntermediaryFirstParameterType.isGenericType()) {
ParameterizedType parameterized = imRewrite.getAST().newParameterizedType(t);
ITypeBinding[] typeParameters = fIntermediaryFirstParameterType.getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) parameterized.typeArguments().add(imRewrite.getImportRewrite().addImport(typeParameters[i], imRewrite.getAST()));
t = parameterized;
}
parameter.setType(t);
parameter.setName(imRewrite.getAST().newSimpleName(targetParameterName));
intermediary.parameters().add(parameter);
}
// Add other params
copyArguments(intermediary, imRewrite);
// Add type parameters of declaring type (and enclosing types)
if (!isStaticTarget() && fIntermediaryFirstParameterType.isGenericType())
addTypeParameters(imRewrite, intermediary.typeParameters(), fIntermediaryFirstParameterType);
// Add type params of method
copyTypeParameters(intermediary, imRewrite);
// Return type
intermediary.setReturnType2(imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getReturnType(), ast, context));
// Exceptions
copyExceptions(intermediary, imRewrite);
// Body
MethodInvocation invocation = imRewrite.getAST().newMethodInvocation();
invocation.setName(imRewrite.getAST().newSimpleName(fTargetMethod.getElementName()));
if (isStaticTarget()) {
Type importedType = imRewrite.getImportRewrite().addImport(fTargetMethodBinding.getDeclaringClass(), ast, context);
invocation.setExpression(ASTNodeFactory.newName(ast, ASTNodes.asString(importedType)));
} else {
invocation.setExpression(imRewrite.getAST().newSimpleName(targetParameterName));
}
copyInvocationParameters(invocation, ast);
Statement call = encapsulateInvocation(intermediary, invocation);
final Block body = imRewrite.getAST().newBlock();
body.statements().add(call);
intermediary.setBody(body);
// method comment
ICompilationUnit targetCU = imRewrite.getCu();
if (StubUtility.doAddComments(targetCU.getJavaProject())) {
String comment = CodeGeneration.getMethodComment(targetCU, getIntermediaryTypeName(), intermediary, null, StubUtility.getLineDelimiterUsed(targetCU));
if (comment != null) {
Javadoc javadoc = (Javadoc) imRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
intermediary.setJavadoc(javadoc);
}
}
// Add the completed method to the intermediary type:
ChildListPropertyDescriptor typeBodyDeclarationsProperty = typeToBodyDeclarationProperty(fIntermediaryType, imRewrite.getRoot());
ListRewrite bodyDeclarationsListRewrite = imRewrite.getASTRewrite().getListRewrite(type, typeBodyDeclarationsProperty);
bodyDeclarationsListRewrite.insertAt(intermediary, ASTNodes.getInsertionIndex(intermediary, type.bodyDeclarations()), imRewrite.createGroupDescription(RefactoringCoreMessages.IntroduceIndirectionRefactoring_group_description_create_new_method));
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class IntroduceIndirectionRefactoring method adjustVisibility.
private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {
Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
if (isRewriteKept(whoToAdjust.getCompilationUnit()))
adjustments = fIntermediaryAdjustments;
else
adjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
int existingAdjustments = adjustments.size();
addAdjustment(whoToAdjust, neededVisibility, adjustments);
if (alsoIncreaseEnclosing)
while (whoToAdjust.getDeclaringType() != null) {
whoToAdjust = whoToAdjust.getDeclaringType();
addAdjustment(whoToAdjust, neededVisibility, adjustments);
}
boolean hasNewAdjustments = (adjustments.size() - existingAdjustments) > 0;
if (hasNewAdjustments && ((whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));
RefactoringStatus status = new RefactoringStatus();
// Don't create a rewrite if it is not necessary
if (!hasNewAdjustments)
return status;
try {
monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
rewrite.setResolveBindings(false);
rewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
}
} finally {
monitor.done();
}
return status;
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class InlineConstantRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
//$NON-NLS-1$
pm.beginTask("", 3);
try {
fSelectionCuRewrite.clearASTAndImportRewrites();
fDeclarationCuRewrite.clearASTAndImportRewrites();
List<CompilationUnitChange> changes = new ArrayList<CompilationUnitChange>();
HashSet<SimpleName> staticImportsInInitializer = new HashSet<SimpleName>();
ImportReferencesCollector.collect(getInitializer(), fField.getJavaProject(), null, new ArrayList<SimpleName>(), staticImportsInInitializer);
if (getReplaceAllReferences()) {
SearchResultGroup[] searchResultGroups = findReferences(pm, result);
for (int i = 0; i < searchResultGroups.length; i++) {
if (pm.isCanceled())
throw new OperationCanceledException();
SearchResultGroup group = searchResultGroups[i];
ICompilationUnit cu = group.getCompilationUnit();
CompilationUnitRewrite cuRewrite = getCuRewrite(cu);
Name[] references = extractReferenceNodes(group.getSearchResults(), cuRewrite.getRoot());
InlineTargetCompilationUnit targetCompilationUnit = new InlineTargetCompilationUnit(cuRewrite, references, this, staticImportsInInitializer);
CompilationUnitChange change = targetCompilationUnit.getChange();
if (change != null)
changes.add(change);
}
} else {
Assert.isTrue(!isDeclarationSelected());
InlineTargetCompilationUnit targetForOnlySelectedReference = new InlineTargetCompilationUnit(fSelectionCuRewrite, new Name[] { fSelectedConstantName }, this, staticImportsInInitializer);
CompilationUnitChange change = targetForOnlySelectedReference.getChange();
if (change != null)
changes.add(change);
}
if (result.hasFatalError())
return result;
if (getRemoveDeclaration() && getReplaceAllReferences()) {
boolean declarationRemoved = false;
for (Iterator<CompilationUnitChange> iter = changes.iterator(); iter.hasNext(); ) {
CompilationUnitChange change = iter.next();
if (change.getCompilationUnit().equals(fDeclarationCuRewrite.getCu())) {
declarationRemoved = true;
break;
}
}
if (!declarationRemoved) {
InlineTargetCompilationUnit targetForDeclaration = new InlineTargetCompilationUnit(fDeclarationCuRewrite, new Name[0], this, staticImportsInInitializer);
CompilationUnitChange change = targetForDeclaration.getChange();
if (change != null)
changes.add(change);
}
}
ICompilationUnit[] cus = new ICompilationUnit[changes.size()];
for (int i = 0; i < changes.size(); i++) {
CompilationUnitChange change = changes.get(i);
cus[i] = change.getCompilationUnit();
}
result.merge(Checks.validateModifiesFiles(ResourceUtil.getFiles(cus), getValidationContext()));
pm.worked(1);
fChanges = changes.toArray(new CompilationUnitChange[changes.size()]);
return result;
} finally {
pm.done();
}
}
Aggregations