Skip to content

Commit

Permalink
SuperTypeRefactoringProcessor: fix deprecated finalize()
Browse files Browse the repository at this point in the history
tested with System.gc() behind
ExtractInterfaceTests1d8.testExtractInterfaceFromInterface2()
  • Loading branch information
EcljpseB0T authored and jukzi committed Feb 1, 2024
1 parent 2647528 commit 41f215c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.structure.constraints;

import java.lang.ref.Cleaner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -161,10 +162,10 @@ protected static ASTNode createCorrespondingNode(final CompilationUnitRewrite re
*/
protected Map<ICompilationUnit, Collection<CastVariable2>> fObsoleteCasts= null;

private static final Cleaner cleaner= Cleaner.create();

/** The working copy owner */
protected final WorkingCopyOwner fOwner= new WorkingCopyOwner() {
// use default implementation
};
protected final CleanableWorkingCopyOwner fOwner= new CleanableWorkingCopyOwner();

/** Should occurrences of the type be replaced by the supertype? */
protected boolean fReplace= false;
Expand Down Expand Up @@ -192,6 +193,7 @@ protected static ASTNode createCorrespondingNode(final CompilationUnitRewrite re
*/
protected SuperTypeRefactoringProcessor(final CodeGenerationSettings settings) {
fSettings= settings;
cleaner.register(this, fOwner);
}

/**
Expand Down Expand Up @@ -484,11 +486,6 @@ protected final String createTypeTemplate(final ICompilationUnit unit, final Str
return StubUtility.getCompilationUnitContent(unit, buffer.toString(), fileComment, comment, content, delimiter);
}

@Override
protected void finalize() throws Throwable {
resetWorkingCopies();
}

/**
* Returns the field which corresponds to the specified variable declaration
* fragment
Expand Down Expand Up @@ -759,11 +756,21 @@ protected final void performSecondPass(final SuperTypeConstraintsCreator creator
}
}

static class CleanableWorkingCopyOwner extends WorkingCopyOwner implements Runnable {
@Override
public void run() {
resetWorkingCopies(this);
}
}

protected void resetWorkingCopies() {
resetWorkingCopies(fOwner);
}
/**
* Resets the working copies.
*/
protected void resetWorkingCopies() {
for (ICompilationUnit unit : JavaCore.getWorkingCopies(fOwner)) {
private static void resetWorkingCopies(WorkingCopyOwner owner) {
for (ICompilationUnit unit : JavaCore.getWorkingCopies(owner)) {
try {
unit.discardWorkingCopy();
} catch (Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected static ITypeBinding findTypeInHierarchy(final ITypeBinding type, final
private IType fSubType;

/** The supertype as replacement */
private IType fSuperType= null;
private IType fSuperType;

/**
* Creates a new super type processor.
Expand All @@ -133,21 +133,6 @@ public UseSuperTypeProcessor(final IType subType) {
fSubType= subType;
}

/**
* Creates a new super type processor.
*
* @param subType
* the subtype to replace its occurrences
* @param superType
* the supertype as replacement
*/
public UseSuperTypeProcessor(final IType subType, final IType superType) {
super(null);
fReplace= true;
fSubType= subType;
fSuperType= superType;
}

/**
* Creates a new super type processor from refactoring arguments.
*
Expand Down

0 comments on commit 41f215c

Please sign in to comment.