Skip to content

Commit

Permalink
fix: Remove compilation unit by reference equality if it has no file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen authored Aug 28, 2023
1 parent e623d6f commit a9b3e77
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/main/java/spoon/reflect/factory/CompilationUnitFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ public CompilationUnit getOrCreate(CtPackage ctPackage) {
/**
* remove a type from the list of types to be pretty-printed
*/
public void removeType(CtType type) {
cachedCompilationUnits.remove(type.getPosition().getCompilationUnit().getFile().getAbsolutePath());
public void removeType(CtType<?> type) {
File file = type.getPosition().getCompilationUnit().getFile();
if (file != null) {
cachedCompilationUnits.remove(type.getPosition().getCompilationUnit().getFile().getAbsolutePath());
} else {
// Virtual files do not have a file, so fall back to a slow scan, trying to find them by reference equality
cachedCompilationUnits.values().removeIf(it -> it == type.getPosition().getCompilationUnit());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ public <E extends CtElement> E setParent(CtElement parent) {

@Override
public String toString() {
return this.file.getName();
if (this.file != null) {
return this.file.getName();
}
return "CompilationUnit<unknown file>";
}
}
28 changes: 28 additions & 0 deletions src/test/java/spoon/test/refactoring/RefactoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spoon.test.refactoring;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -35,6 +36,7 @@
import org.junit.jupiter.api.condition.JRE;
import spoon.Launcher;
import spoon.refactoring.Refactoring;
import spoon.reflect.CtModel;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtInvocation;
Expand All @@ -45,6 +47,7 @@
import spoon.reflect.visitor.filter.AbstractFilter;
import spoon.reflect.visitor.filter.AbstractReferenceFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.VirtualFile;
import spoon.test.refactoring.processors.ThisTransformationProcessor;
import spoon.test.refactoring.testclasses.AClass;

Expand Down Expand Up @@ -184,4 +187,29 @@ public void testRemoveDeprecatedMethods() {
// error is kinda okay
}
}

@Test
void renameClassInVirtualFile() {
// contract: Renaming classes defined in virtual files should work
Launcher spoon = new Launcher();
spoon.addInputResource(new VirtualFile(
"public class Test {\n" +
" void foo() {\n" +
" }\n" +
"}\n"));
CtModel model = spoon.buildModel();
CtClass<?> clazz = model.getElements(new TypeFilter<>(CtClass.class)).get(0);

Refactoring.changeTypeName(clazz, "Test2");

assertTrue(
clazz.prettyprint().contains("class Test2 "),
"Class was not renamed: '" + clazz.prettyprint() + "'"
);
assertFalse(
clazz.prettyprint().contains("class Test "),
"Class was not renamed: '" + clazz.prettyprint() + "'"
);
}

}

0 comments on commit a9b3e77

Please sign in to comment.