Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small checks and behavior differences for refactorings and assists #921

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import org.eclipse.text.edits.InsertEdit;

import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextChange;
Expand Down Expand Up @@ -62,7 +64,17 @@ public TextChange createTextChange() throws CoreException {
dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$
return dummyChange;
}
return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
Change o = fRefactoring.createChange(new NullProgressMonitor());;
if(o instanceof TextChange)
return (TextChange) o;
if( o instanceof CompositeChange) {
CompositeChange cc = (CompositeChange)o;
Change[] children = cc.getChildren();
if( children != null && children.length == 1 && children[0] instanceof TextChange) {
return ((TextChange)children[0]);
}
}
return null;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ private IntroduceParameterDescriptor getRefactoringDescriptor() {
ChangeMethodSignatureDescriptor extended= (ChangeMethodSignatureDescriptor) fChangeSignatureProcessor.createDescriptor();
RefactoringContribution contribution= RefactoringCore.getRefactoringContribution(IJavaRefactorings.CHANGE_METHOD_SIGNATURE);

Map<String, String> argumentsMap= contribution.retrieveArgumentMap(extended);
Map<String, String> argumentsMap= contribution == null ? Collections.emptyMap() : contribution.retrieveArgumentMap(extended);

final Map<String, String> arguments= new HashMap<>();
arguments.put(ATTRIBUTE_ARGUMENT, fParameter.getNewName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.eclipse.jdt.core.search.TypeReferenceMatch;

import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
Expand Down Expand Up @@ -127,8 +128,6 @@
import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIdsCore;
import org.eclipse.jdt.ui.refactoring.IRefactoringSaveModes;

import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;

public class RenameTypeProcessor extends JavaRenameProcessor implements ITextUpdating, IReferenceUpdating, IQualifiedNameUpdating, ISimilarDeclarationUpdating, IResourceMapper, IJavaElementMapper {

private static final String ATTRIBUTE_QUALIFIED= "qualified"; //$NON-NLS-1$
Expand Down Expand Up @@ -1084,21 +1083,10 @@ public Change createChange(IProgressMonitor monitor) throws CoreException {
((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
}
}
result.addAll(fChangeManager.getAllChanges());
if (willRenameCU()) {
IResource resource= fType.getCompilationUnit().getResource();
if (resource != null && resource.isLinked()) {
String ext= resource.getFileExtension();
String renamedResourceName;
if (ext == null)
renamedResourceName= getNewElementName();
else
renamedResourceName= getNewElementName() + '.' + ext;
result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
} else {
String renamedCUName= JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
}
createChangeForRenamedCU(fType, fChangeManager, result);
} else {
result.addAll(fChangeManager.getAllChanges());
}
monitor.worked(1);
return result;
Expand All @@ -1107,6 +1095,33 @@ public Change createChange(IProgressMonitor monitor) throws CoreException {
}
}

protected void createChangeForRenamedCU(IType type, TextChangeManager changeManager, DynamicValidationRefactoringChange result) throws CoreException {
IResource resource= type.getCompilationUnit().getResource();
if (resource != null && resource.isLinked()) {
createChangeForRenamedCUNullOrLinkedResource(type, changeManager, resource, result);;
} else {
createChangeForRenamedCUStandardResource(type, changeManager, resource, result);
}
}

protected void createChangeForRenamedCUStandardResource(IType type, TextChangeManager changeManager, IResource resource, DynamicValidationRefactoringChange result) throws CoreException {
result.addAll(changeManager.getAllChanges());

String renamedCUName = JavaModelUtil.getRenamedCUName(type.getCompilationUnit(), getNewElementName());
result.add(new RenameCompilationUnitChange(type.getCompilationUnit(), renamedCUName));
}

protected void createChangeForRenamedCUNullOrLinkedResource(IType type, TextChangeManager changeManager, IResource resource, DynamicValidationRefactoringChange result) {
result.addAll(changeManager.getAllChanges());
String ext= resource.getFileExtension();
String renamedResourceName;
if (ext == null)
renamedResourceName= getNewElementName();
else
renamedResourceName= getNewElementName() + '.' + ext;
result.add(new RenameResourceChange(type.getCompilationUnit().getPath(), renamedResourceName));
}

@Override
public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
if (fQualifiedNameSearchResult != null) {
Expand Down Expand Up @@ -1168,14 +1183,14 @@ private void createChanges(IProgressMonitor pm) throws CoreException {
}
}

private void addTypeDeclarationUpdate(TextChangeManager manager) throws CoreException {
protected void addTypeDeclarationUpdate(TextChangeManager manager) throws CoreException {
String name= RefactoringCoreMessages.RenameTypeRefactoring_update;
int typeNameLength= fType.getElementName().length();
ICompilationUnit cu= fType.getCompilationUnit();
TextChangeCompatibility.addTextEdit(manager.get(cu), name, new ReplaceEdit(fType.getNameRange().getOffset(), typeNameLength, getNewElementName()));
}

private void addConstructorRenames(TextChangeManager manager) throws CoreException {
protected void addConstructorRenames(TextChangeManager manager) throws CoreException {
ICompilationUnit cu= fType.getCompilationUnit();
int typeNameLength= fType.getElementName().length();
for (IMethod method : fType.getMethods()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws
IMethod topmost= getMethod();
if (MethodChecks.isVirtual(topmost))
topmost= MethodChecks.getTopmostMethod(getMethod(), hierarchy, monitor);
if (topmost != null)
initialize(topmost);
if (topmost != null) {
initializeWithTopMostImplementation(topmost);
}
fActivationChecked= true;
}
} finally{
Expand All @@ -138,6 +139,10 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws
return result;
}

protected void initializeWithTopMostImplementation(IMethod topmost) {
initialize(topmost);
}

@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext) throws CoreException {
try{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@

public class SurroundWithTryCatchAnalyzer extends SurroundWithAnalyzer {
private ITypeBinding[] fExceptions;
private boolean excludeRethrown;

public SurroundWithTryCatchAnalyzer(ICompilationUnit unit, Selection selection) throws CoreException {
this(unit, selection, false);
}

public SurroundWithTryCatchAnalyzer(ICompilationUnit unit, Selection selection, boolean excludeRethrown) throws CoreException {
super(unit, selection, true);
this.excludeRethrown = excludeRethrown;
}

public ITypeBinding[] getExceptions() {
Expand All @@ -58,7 +64,7 @@ public void endVisit(CompilationUnit node) {

super.endVisit(node);
if (enclosingNode != null && !getStatus().hasFatalError()) {
fExceptions= ExceptionAnalyzer.perform(enclosingNode, getSelection(), false);
fExceptions= ExceptionAnalyzer.perform(enclosingNode, getSelection(), excludeRethrown);
if (fExceptions == null || fExceptions.length == 0) {
if (enclosingNode instanceof MethodReference) {
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class SurroundWithTryCatchRefactoring extends Refactoring {
private Selection fSelection;
private SurroundWithTryCatchAnalyzer fAnalyzer;
private boolean fLeaveDirty;
private boolean fExcludeRethrown;

private ICompilationUnit fCUnit;
private CompilationUnit fRootNode;
Expand All @@ -120,12 +121,18 @@ public class SurroundWithTryCatchRefactoring extends Refactoring {
private final boolean fIsMultiCatch;

private SurroundWithTryCatchRefactoring(ICompilationUnit cu, Selection selection, boolean isMultiCatch) {
this(cu, selection, isMultiCatch, false);
}

private SurroundWithTryCatchRefactoring(ICompilationUnit cu, Selection selection, boolean isMultiCatch, boolean excludeRethrown) {
fCUnit= cu;
fSelection= selection;
fIsMultiCatch= isMultiCatch;
fLeaveDirty= false;
fExcludeRethrown= excludeRethrown;
}


public static SurroundWithTryCatchRefactoring create(ICompilationUnit cu, int offset, int length) {
return create(cu, offset, length, false);
}
Expand Down Expand Up @@ -161,7 +168,7 @@ public RefactoringStatus checkActivationBasics(CompilationUnit rootNode) throws
RefactoringStatus result= new RefactoringStatus();
fRootNode= rootNode;

fAnalyzer= new SurroundWithTryCatchAnalyzer(fCUnit, fSelection);
fAnalyzer= new SurroundWithTryCatchAnalyzer(fCUnit, fSelection, fExcludeRethrown);
fRootNode.accept(fAnalyzer);
result.merge(fAnalyzer.getStatus());
ITypeBinding[] exceptions= fAnalyzer.getExceptions();
Expand Down Expand Up @@ -251,7 +258,6 @@ private void createTryCatchStatement(org.eclipse.jdt.core.IBuffer buffer, String
TryStatement tryStatement= getAST().newTryStatement();
ITypeBinding[] exceptions= fAnalyzer.getExceptions();
ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fAnalyzer.getEnclosingBodyDeclaration(), fImportRewrite);

if (!fIsMultiCatch) {
for (int i= 0; i < exceptions.length; i++) {
ITypeBinding exception= exceptions[i];
Expand Down
Loading